class ConnectionPool internal constructor(
internal val delegate: RealConnectionPool
) {
constructor(
maxIdleConnections: Int,
keepAliveDuration: Long,
timeUnit: TimeUnit
) : this(RealConnectionPool(
taskRunner = TaskRunner.INSTANCE,
maxIdleConnections = maxIdleConnections,
keepAliveDuration = keepAliveDuration,
timeUnit = timeUnit
))
constructor() : this(5, 5, TimeUnit.MINUTES) //默认最大闲置连接数5,连接有效时长为5分钟
/** Returns the number of idle connections in the pool. */
fun idleConnectionCount(): Int = delegate.idleConnectionCount()
/** Returns total number of connections in the pool. */
fun connectionCount(): Int = delegate.connectionCount()
/** Close and remove all idle connections in the pool. */
fun evictAll() {
delegate.evictAll()
}
}
1.Okhttp3请求的过程分析
今天读了一下kotlin版本的okhttp3的源码,就不码字了,用一张图简单梳理下整个过程的调用:
2.Okhttp3 TCP连接复用分析
HTTP持久连接(英语:HTTP persistent connection,也称作HTTP keep-alive或HTTP connection reuse)是一种复用TCP连接的手段。主要解决短时内的连接复用问题,避免短时间内不断创建和关闭连接带来的开销。 具体的连接复用时间的长短,通常是由web服务器控制的,通常配置都是在几十秒左右。在http1.1之后默认都是开启keep-alive机制的。
在Okhttp中是通过连接池ConnectionPool的方式实现的TCP连接的复用,最终通过委托RealConnectionPool类来实现连接池的业务逻辑。
整个TCP连接复用的逻辑如下: