- 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 static final Executor executor = new ThreadPoolExecutor(0 / corePoolSize /,
Integer.MAX_VALUE / maximumPoolSize /, 60L / keepAliveTime */, TimeUnit.SECONDS,
new SynchronousQueue(), Util.threadFactory(“OkHttp ConnectionPool”, true));
/** The maximum number of idle connections for each address. */
private final int maxIdleConnections;
private final Deque connections = new ArrayDeque<>();
final RouteDatabase routeDatabase = new RouteDatabase();
boolean cleanupRunning;
excutor : 线程池,用来检测闲置socket并对其进行清理。
connections : connection缓存池。Deque是一个双端列表,支持在头尾插入元素,这里用作LIFO(后进先出)堆栈,多用于缓存数据。
routeDatabase :用来记录连接失败router
2.1 缓存操作
ConnectionPool提供对Deque进行操作的方法分别为put、get、connectionBecameIdle、evictAll几个操作。分别对应放入连接、获取连接、移除连接、移除所有连接操作。
put操作
void put(RealConnection connection) {
assert (Thread.holdsLock(this));
if (!cleanupRunning) {
cleanupRunning = true;
executor.execute(cleanupRunnable);
}
connections.add(connection);
}
可以看到在新的connection 放进列表之前执行清理闲置连接的线程。
既然是复用,那么看下他获取连接的方式。
/** Returns a recycled connection to {@code address}, or null if no such connection exists. */
RealConnection get(Address address, StreamAllocation streamAllocation) {
assert (Thread.holdsLock(this));
for (RealConnection connection : connections) {