Although you provide only the URL, OkHttp plans its connection to your webserver using three types: URL, Address, and Route.
尽管你只提供了一个 URL,但是 OkHttp 计划使用三种方式连接到你的服务器:URL,Address 以及 Route.
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
) 是组成 internet 和 HTTP 的基础.它除了是 web 上所有事物基础的,分散的命名规则外,还指定如何访问 web 资源.
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
).每个服务器都有多个 URLs.
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).
Addresses 指定了一个网络服务器(比如 github.com
)以及需要连接到这个服务器的所有静态配置:端口号,HTTPS 配置,以及首选的网络协议(比如 HTTP/2 或者 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 套接字连接。共享一个连接有巨大的性能优势:低延迟,高吞吐量(由于 TCP 慢启动)和守恒的电池(注:守恒的电池是什么鬼?我猜是更小的资源开销?).OkHttp 使用连接池自动重用 HTTP/1.x 连接和多路传输的 HTTP/2 以及 SPDY 连接.
In OkHttp some fields of the address come from the URL (scheme, hostname, port) and the rest come from the OkHttpClient.
在 OkHttp 中,address 中的一些字段来自于 URL(scheme,hostname,port(注:拿 http://www.baidu.com:80
来举例:scheme 为 http,hostname 为 www.baidu.com
,port 为 80,一般浏览器中 http 请求默认为 80 端口,可省略,同理 https 默认请求端口为 443,通常情况下也可省略)),剩下的部分由 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).
Routes 提供真实连接到一个网络服务器所需的动态信息:用来尝试访问的明确的 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.
一个地址或许拥有多个路由,举例来说,一个网络服务器拥有多个数据中心,或许会有多个 IP 地址对 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 传建一个 address.这个 address 指定我们如何连接到网络服务器.
- 尝试从连接池中重新取回一个请求此 address 的连接
- 如果第二步没有找到可用的请求,就会选择一个路由进行尝试,这通常意味着进行一次 DNS 请求来获取这个网络服务器的真实 IP 地址,然后如果有必要的话,选择一个 TLS 协议版本和代理服务器.
- 如果是一个新的路线(注:意味着从第三步过来的),就通过构建一个直接的 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.
当响应被接收后,连接将会被返回至连接池中,以便被后续可能的请求重复使用.如果连接池中的连接在一段时间内不被使用,则将会被移除.
译:洗澡狂魔,原文 wiki 地址:https://github.com/square/okhttp/wiki/Connections
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于