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计划它连接到您的网络服务器需要使用三种类型: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和互联网的基础。除了网络所有事物普遍的、分散的命名方案之外,他们还指定如何访问网络资源.
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 是抽象的:
- 他们指定的调用可能是明文的(http)或加密的(https),但不使用任何加密算法。他们也不指定如何验证对等的证书(HostnameVerifier)或哪个证书可以被信任(SSLSocketFactory)。
- 他们没有指定是否应该使用一个特定的代理服务器或使用该代理服务器进行身份验证。
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.
他们也很具体:每个URL标识一个特定的路径(比如/square/okhttp)和询问(? q = sharks&lang = en).每一个网络服务器主导很多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).
地址指定一个网络服务器(比如github.com)和连接到服务器必要的所有的静态配置:端口号,HTTPS设置和首选网络协议(如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连接。共享一个连接有巨大的性能优势:低延迟、高吞吐量(由于TCP慢启动)和保守的电量。OkHttp使用连接池自动重用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中一些地址的字段来自URL(计划、主机名、端口),其余来自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).
线路提供动态信息需要实时连接到网络服务器。这是尝试特定的IP地址(如一个DNS访问被发现),准确的使用代理服务器(如果使用ProxySelector),和哪个版本的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.
可能有很多路线共用一个地址。例如,一个网络服务器驻留在多个数据中心可能在DNS的回应中产生多个IP地址。
Connections 连接
When you request a URL with OkHttp, here's what it does:
- It uses the URL and configured OkHttpClient to create an address. This address specifies how we'll connect to the webserver.
- It attempts to retrieve a connection with that address from the connection pool.
- 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.
- 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.
- It sends the HTTP request and reads the response.
当你用OkHttp请求URL,它所做的:
- 它使用URL和配置好的OkHttpClient创建一个地址.这个地址指定我们将如何连接到网络服务器.
- 它试图从连接池中检索是否存在该地址的连接.
- 如果在连接池中没有找到一个连接,它尝试选择一条路线.这通常意味着一个DNS请求服务器的IP地址.然后选择一个TLS版本和在必要时使用代理服务器.
- 如果这是一个新的线路,它通过建立一个直接的socket连接,TLS隧道(HTTP代理上的HTTPS),或直接TLS连接.必要时与TLS握手。
- 它发送HTTP请求并读取响应.
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.
如果连接发生问题,OkHttp会选择另一条路线,并再试一次.这允许OkHttp当服务器地址的子设备无法访问时重新访问.在某个依赖的连接失效或者尝试TLS版本是不支持的时候同样非常有用.
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.
只有在接收到响应后,连接将会返回到连接池中,因此它可以被之后的请求重用.经过一段时间不活动的连接会被连接池清除.
对OkHttp感兴趣的朋友可以看一看Okhttp-wiki系列,可以帮助你理解Okhttp的使用方法及原理:
- Okhttp-wiki 之 Home 主页
- Okhttp-wiki 之 Calls 调用
- Okhttp-wiki 之 Connections 连接
- Okhttp-wiki 之 Recipes 秘诀(食谱)
- Okhttp-wiki 之 Interceptors 拦截器
- Okhttp-wiki 之 HTTPS