Okhttp-wiki 之 Connections 連接

Although you provide only the URL, OkHttp plans its connection to your webserver using three types: URL, Address, and Route.

雖然你只需要提供URL,但OkHttp計(jì)劃它連接到您的網(wǎng)絡(luò)服務(wù)器需要使用三種類型:URL,地址和路線.

URLs

URLs (like https://github.com/square/okhttp) are fundamental to HTTP and the Internet. In addition to being a universal, decentralized naming scheme for everything on the web, they also specify how to access web resources.

URLs(例如https://github.com/square/okhttp) 是HTTP和互聯(lián)網(wǎng)的基礎(chǔ)。除了網(wǎng)絡(luò)所有事物普遍的、分散的命名方案之外,他們還指定如何訪問(wèn)網(wǎng)絡(luò)資源.

URLs are abstract:

  • They specify that the call may be plaintext (http) or encrypted (https), but not which cryptographic algorithms should be used. Nor do they specify how to verify the peer's certificates (the HostnameVerifier) or which certificates can be trusted (the SSLSocketFactory).
  • They don't specify whether a specific proxy server should be used or how to authenticate with that proxy server.

URLs 是抽象的:

  • 他們指定的調(diào)用可能是明文的(http)或加密的(https),但不使用任何加密算法。他們也不指定如何驗(yàn)證對(duì)等的證書(shū)(HostnameVerifier)或哪個(gè)證書(shū)可以被信任(SSLSocketFactory)。
  • 他們沒(méi)有指定是否應(yīng)該使用一個(gè)特定的代理服務(wù)器或使用該代理服務(wù)器進(jìn)行身份驗(yàn)證。

They're also concrete: each URL identifies a specific path (like /square/okhttp) and query (like ?q=sharks&lang=en). Each webserver hosts many URLs.

他們也很具體:每個(gè)URL標(biāo)識(shí)一個(gè)特定的路徑(比如/square/okhttp)和詢問(wèn)(? q = sharks&lang = en).每一個(gè)網(wǎng)絡(luò)服務(wù)器主導(dǎo)很多url.

Addresses 地址

Addresses specify a webserver (like github.com) and all of the static configuration necessary to connect to that server: the port number, HTTPS settings, and preferred network protocols (like HTTP/2 or SPDY).

地址指定一個(gè)網(wǎng)絡(luò)服務(wù)器(比如github.com)和連接到服務(wù)器必要的所有的靜態(tài)配置:端口號(hào),HTTPS設(shè)置和首選網(wǎng)絡(luò)協(xié)議(如HTTP/2 or SPDY)。

URLs that share the same address may also share the same underlying TCP socket connection. Sharing a connection has substantial performance benefits: lower latency, higher throughput (due to TCP slow start) and conserved battery. OkHttp uses a ConnectionPool that automatically reuses HTTP/1.x connections and multiplexes HTTP/2 and SPDY connections.

URLs共享相同的地址也可以共享相同的底層TCP socket連接。共享一個(gè)連接有巨大的性能優(yōu)勢(shì):低延遲、高吞吐量(由于TCP慢啟動(dòng))和保守的電量。OkHttp使用連接池自動(dòng)重用HTTP/1.x連接和多樣的HTTP/2 and SPDY 連接.

In OkHttp some fields of the address come from the URL (scheme, hostname, port) and the rest come from the OkHttpClient.

在OkHttp中一些地址的字段來(lái)自URL(計(jì)劃、主機(jī)名、端口),其余來(lái)自OkHttpClient.

Routes 線路

Routes supply the dynamic information necessary to actually connect to a webserver. This is the specific IP address to attempt (as discovered by a DNS query), the exact proxy server to use (if a ProxySelector is in use), and which version of TLS to negotiate (for HTTPS connections).

線路提供動(dòng)態(tài)信息需要實(shí)時(shí)連接到網(wǎng)絡(luò)服務(wù)器。這是嘗試特定的IP地址(如一個(gè)DNS訪問(wèn)被發(fā)現(xiàn)),準(zhǔn)確的使用代理服務(wù)器(如果使用ProxySelector),和哪個(gè)版本的TLS交涉(HTTPS連接)。

There may be many routes for a single address. For example, a webserver that is hosted in multiple datacenters may yield multiple IP addresses in its DNS response.

可能有很多路線共用一個(gè)地址。例如,一個(gè)網(wǎng)絡(luò)服務(wù)器駐留在多個(gè)數(shù)據(jù)中心可能在DNS的回應(yīng)中產(chǎn)生多個(gè)IP地址。

Connections 連接

When you request a URL with OkHttp, here's what it does:

  1. It uses the URL and configured OkHttpClient to create an address. This address specifies how we'll connect to the webserver.
  2. It attempts to retrieve a connection with that address from the connection pool.
  3. If it doesn't find a connection in the pool, it selects a route to attempt. This usually means making a DNS request to get the server's IP addresses. It then selects a TLS version and proxy server if necessary.
  4. If it's a new route, it connects by building either a direct socket connection, a TLS tunnel (for HTTPS over an HTTP proxy), or a direct TLS connection. It does TLS handshakes as necessary.
  5. It sends the HTTP request and reads the response.

當(dāng)你用OkHttp請(qǐng)求URL,它所做的:

  1. 它使用URL和配置好的OkHttpClient創(chuàng)建一個(gè)地址.這個(gè)地址指定我們將如何連接到網(wǎng)絡(luò)服務(wù)器.
  2. 它試圖從連接池中檢索是否存在該地址的連接.
  3. 如果在連接池中沒(méi)有找到一個(gè)連接,它嘗試選擇一條路線.這通常意味著一個(gè)DNS請(qǐng)求服務(wù)器的IP地址.然后選擇一個(gè)TLS版本和在必要時(shí)使用代理服務(wù)器.
  4. 如果這是一個(gè)新的線路,它通過(guò)建立一個(gè)直接的socket連接,TLS隧道(HTTP代理上的HTTPS),或直接TLS連接.必要時(shí)與TLS握手。
  5. 它發(fā)送HTTP請(qǐng)求并讀取響應(yīng).

If there's a problem with the connection, OkHttp will select another route and try again. This allows OkHttp to recover when a subset of a server's addresses are unreachable. It's also useful when a pooled connection is stale or if the attempted TLS version is unsupported.

如果連接發(fā)生問(wèn)題,OkHttp會(huì)選擇另一條路線,并再試一次.這允許OkHttp當(dāng)服務(wù)器地址的子設(shè)備無(wú)法訪問(wèn)時(shí)重新訪問(wèn).在某個(gè)依賴的連接失效或者嘗試TLS版本是不支持的時(shí)候同樣非常有用.

Once the response has been received, the connection will be returned to the pool so it can be reused for a future request. Connections are evicted from the pool after a period of inactivity.

只有在接收到響應(yīng)后,連接將會(huì)返回到連接池中,因此它可以被之后的請(qǐng)求重用.經(jīng)過(guò)一段時(shí)間不活動(dòng)的連接會(huì)被連接池清除.


對(duì)OkHttp感興趣的朋友可以看一看Okhttp-wiki系列,可以幫助你理解Okhttp的使用方法及原理:

  1. Okhttp-wiki 之 Home 主頁(yè)
  2. Okhttp-wiki 之 Calls 調(diào)用
  3. Okhttp-wiki 之 Connections 連接
  4. Okhttp-wiki 之 Recipes 秘訣(食譜)
  5. Okhttp-wiki 之 Interceptors 攔截器
  6. Okhttp-wiki 之 HTTPS
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容