Shit!我的host到底怎麼了?

技術頑家發表於2018-08-22

背景

那是今年五月的某一天,我的mac在切換系統host後,chrome遲遲不生效,依然訪問的是原來的環境.
開發和測試同學在做專案時,需要經常進行 日常環境 預發環境 線上環境的切換,其實切的就是系統的host.如果瀏覽器總是延遲,或者死活不生效的活,基本上你就在那不停的重新整理重新整理重新整理. 或者重新啟動瀏覽器.心情會很煩躁的有木有.

解決方案

遇到這個問題的時候,一般我們有以下幾個搞法(只針對chrome哈,重啟瀏覽器就不說了,我標籤一般開啟好幾十,輕易不願意重啟的)

第0招: 強清快取思路. ctrl + shift + r 或者 重新整理按鈕 右鍵彈出一下選單.

image.png
正常重新載入 Ctrl+R:正常重新載入。 

硬性重新載入:Ctrl+Shift+R:淺層次的清除歷史記錄,但不一定快取完全清除。(Ctrl+F5)同理。 
清空快取並硬性重新載入:可以深層次的清除所有的快取。(建議用這個)

但其實這種做法對切換host沒實際作用!

第一招: 開啟 chrome://net-internals/#dns 頁面, 點選 Clear host cache. 一般來講, 有些同學使用這招可以解決host切換的不生效問題了.但是我的chrome使用此招還是無果.

image.png

Click on the button to Clear the host resolver cache entries. You would find that the entries in the list would be immediately cleared.By clearing the host resolver cache, you can force Google Chrome to resolve the domains again.

第二招: 開啟 chrome://net-internals/#sockets 頁面, 點選 Flush socket pools. 這招非常狠.終極殺手!絕對解決問題.

image.png

終極工具

零: 開啟 https://zoom.alibaba-inc.com/ 安裝ZOOM外掛.

一. 在頁面右鍵選單上會發現[徹底清host快取]的工具, 當然我還做了放置http強制跳轉https

image.png

二. 點選[徹底清host快取]選單,彈出提示,讓你去開啟一個許可權. 點選[前往設定]哈.開啟許可權後,瀏覽器會重啟以啟用新的配置.

image.png

三. 瀏覽器重啟後,以後需要清dns,右鍵點選[徹底清host快取]後,就ok了.成功後,會給你一個提示

image.png

這個提示裡面,有個貼心的小功能,發現沒.

漲點姿勢

DNS快取
感覺下面這段英語描述其實很簡單易懂的,所以我就不放中文了.
A DNS cache (sometimes called a DNS resolver cache) is a temporary database, maintained by a computer`s operating system, that contains records of all the recent visits and attempted visits to websites and other internet domains.

In other words, a DNS cache is just a memory of recent DNS lookups that your computer can quickly refer to when it`s trying to figure out how to load a website.

Most people only hear the phrase “DNS cache” when it refers to flushing/clearing the DNS cache in order to help fix an internet connectivity issue. There`s more on that at the bottom of this page.

為什麼要設定DNS 本地快取?
domain –> [DNS server] –> IP

Even though there are tons of public DNS servers your network can use to try to speed up the conversion/resolution process, it`s still quicker to have a local copy of the map list, which is where DNS caches come into play.

How a DNS Cache Works
Before a browser issues its requests to the outside network, the computer intercepts each one and looks up the domain name in the DNS cache database. The database contains a list of all recently accessed domain names and the addresses that DNS calculated for them the first time a request was made.

Chrome 監聽host變動
Note that Chrome now monitors the hosts file and autoclears the dnscache whenever there are any changes to the hosts file. —You can easily test if that works on your system by adding a blank line after your hosts file, and the list at chrome://net-internals/#dns will be autoupdated.— Windows` dnscache service will also (at least on win 8.1) monitor the hosts file for changes, so after you have updated your hosts file, simply clicking on the button “Flush socket pools” will work. Nothing else is needed. 

僅僅清DNS Cache為何還不足夠?
Chrome使用DNS cache和Socket Pool來加速訪問.其實Chrome的DNS cache會在切換hosts檔案之後會自動清理,但是Socket Pool不會.(Chrome can`t use the correct hosts after modifying the hosts file because of Chrome using socket pools.)

Chrome maintains long connections in the connection pool to speed up. When Chrome finds that a request can reuse a connection in the connection pool, it won`t go through the DNS again, thus, we always perceive that Chrome won`t respond to a hosts change immediately.

為什麼我要做這個外掛
Chrome has provided an interface for flushing the connection pool in the chrome://net-internals/#sockets tab, but I think it`s annoying that I have to go to the tab first, and click so a small button with a trackpad.

I found that when clicking the button of flush socket pool, Chrome executes two key JavaScript methods.

g_browser.sendFlushSocketPools();
g_browser.checkForUpdatedInfo(false);

在chrome://* 頁面下,可以發現 window下有個g_browser的物件,其中包含了很多方法.

image.png

根據這些搜尋chrome的原始碼,找到了源頭: https://searchcode.com/codesearch/view/86220162/. g_browser 的一堆方法都在這個BrowserBridge類裡面.

image.png
image.png

每次都開啟chrome://net-internals/#sockets頁面去點選flush按鈕是很煩的.於是我在想, 通過chrome外掛自動開啟這個頁面,然後自動執行 g_browser.sendFlushSocketPools() 豈不是很完美?!

qutos


相關文章