目录
我们知道okhttp是可以通过连接池来减少请求延时的,那么这一点是怎么实现的呢?
系列文章:
OKHttp开源框架学习三:任务调度核心类Dispatcher
OKHttp开源框架学习五:拦截器之RetryAndFollowUpInterceptor
OKHttp开源框架学习六:拦截器之BridgeInterceptor
OKHttp开源框架学习八:拦截器之CacheInterceptor
OKHttp开源框架学习九:拦截器之ConnectInterceptor
OKHttp开源框架学习十:ConnectionPool连接池
OKHttp开源框架学习十一:拦截器之CallServerInterceptor
简介:
在okhttp中,客户端与服务端的连接被抽象为一个个的Connection,实现类是RealConnection。而ConnectionPool就是专门用来管理Connection的类。
Manages reuse of HTTP and HTTP/2 connections for reduced network latency. HTTP requests that
share the same {@link Address} may share a {@link Connection}. This class implements the policy
of which connections to keep open for future use.
以上为英文简介,翻译过来就是:
ConnectionPool用来管理connections的复用,以减少网络的延迟。一些共享一个地址(Address)的HTTP requests可能也会共享一个Connection。ConnectionPool设置这样的策略:让一些connections保持打开状态,以备将来使用。
共享相同的地址就可以复用连接。
以下是几个重要的参数:
/**
* Background threads are used to cleanup expired(到期的) connections. There will be at most a single
* thread running per connection pool. The thread pool executor(执行人) permits the pool itself to be
* garbage collected.
*/
private