ConnectInterceptor拦截器分析

在分析完CacheInterceptor拦截器后,我们再来看下ConnectInterceptor这个拦截器,其主要作用就是与服务器建立连接。
直接看它的intercept方法:

public Response intercept(Chain chain) throws IOException { RealInterceptorChain realChain = (RealInterceptorChain) chain; Request request = realChain.request(); // 获取StreamAllocation对象 StreamAllocation streamAllocation = realChain.streamAllocation(); // We need the network to satisfy this request. Possibly for validating a conditional GET. boolean doExtensiveHealthChecks = !request.method().equals("GET"); // 初始化HttpCodec对象 HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks); // 通过StreamAllocation获取连接对象 RealConnection connection = streamAllocation.connection(); // 传入参数,调用下一个拦截器处理 return realChain.proceed(request, streamAllocation, httpCodec, connection); }

该方法主要做了一下几个操作:
1、获取StreamAllocation对象,通过该对象来初始化HttpCodec对象,这个HttpCodec对象用于编码request和解码response,并且对不同http协议(http1.1和http/2)的请求和响应做处理。
2、通过StreamAllocation获取连接对象RealConnection,RealConnection对象负责实际进行与服务器交互的类。
3、将这几个对象作为参数,传入到下一个拦截器CallServerIntercept(实际与服务器进行交互)中处理。
初始化HttpCodec对象 看下StreamAllocation的newStream方法的实现细节:
public HttpCodec newStream(OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) {// 获取连接超时时间、读写超时时间等信息 int connectTimeout = chain.connectTimeoutMillis(); int readTimeout = chain.readTimeoutMillis(); int writeTimeout = chain.writeTimeoutMillis(); int pingIntervalMillis = client.pingIntervalMillis(); boolean connectionRetryEnabled = client.retryOnConnectionFailure(); try { // 查找健康可用连接 RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks); // 通过连接初始化resultCodec对象 HttpCodec resultCodec = resultConnection.newCodec(client, chain, this); synchronized (connectionPool) { codec = resultCodec; return resultCodec; } } catch (IOException e) { throw new RouteException(e); } }

查找一个健康可用的连接,通过RealConnection的newCodec来初始化HttpCodec对象,我们来看看查找可用连接的逻辑(findHealthyConnection方法):
private RealConnection findHealthyConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks) throws IOException { while (true) { // 查找连接,使用已存在的可用连接或从连接池中取,没有则创建一条连接 RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis, connectionRetryEnabled); // If this is a brand new connection, we can skip the extensive health checks. synchronized (connectionPool) { // 如果是新建的连接,则直接返回,跳过健康检查 if (candidate.successCount == 0) { return candidate; } }// Do a (potentially slow) check to confirm that the pooled connection is still good. If it // isn't, take it out of the pool and start again. // 判断连接是否健康可用 if (!candidate.isHealthy(doExtensiveHealthChecks)) { // 禁止新流创建 noNewStreams(); continue; }return candidate; } }

【ConnectInterceptor拦截器分析】通过一个while循环查找可用连接,如果当前是新建的连接,则直接返回,跳过健康检查,否则,需要判断当前连接是否健康可用,如果不是,则禁止新流的创建,并重新进入循环查找连接。
实际查找连接的方法(findConnection):
private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException { // 是否从连接池中找到连接 boolean foundPooledConnection = false; // 需要返回的连接对象 RealConnection result = null; // 对应找到的路由 Route selectedRoute = null; // 可释放的连接 Connection releasedConnection; // 需要关闭的socket Socket toClose; synchronized (connectionPool) { // 异常判断,已释放、codec不为空、请求已取消 if (released) throw new IllegalStateException("released"); if (codec != null) throw new IllegalStateException("codec != null"); if (canceled) throw new IOException("Canceled"); // Attempt to use an already-allocated connection. We need to be careful here because our // already-allocated connection may have been restricted from creating new streams. // 使用已存在的connection对象,需要注意到是该连接可能已经不能创建新的流 releasedConnection = this.connection; // 如果不能创建新的流,则释放并返回对应的要关闭的socket对象(此时,connection已置为null),否则,返回null toClose = releaseIfNoNewStreams(); if (this.connection != null) { // We had an already-allocated connection and it's good. // 使用已存在的“健康”的连接 result = this.connection; releasedConnection = null; } if (!reportedAcquired) { // If the connection was never reported acquired, don't report it as released! releasedConnection = null; }// 已存在的连接是不可用的 if (result == null) { // Attempt to get a connection from the pool. //从连接池中取,将获取到的引用赋值给connection变量 Internal.instance.acquire(connectionPool, address, this, null); if (connection != null) { // 从连接池中找到可使用的连接 foundPooledConnection = true; result = connection; } else { // 连接池中没有连接,赋值给对应路由 selectedRoute = route; } } } // 关闭socket closeQuietly(toClose); // 释放连接的回调 if (releasedConnection != null) { eventListener.connectionReleased(call, releasedConnection); }// 从连接池中获取到连接,则回调 if (foundPooledConnection) { eventListener.connectionAcquired(call, result); }// 找到已存在可用的或从连接池中取出的connection对象,则直接返回该变量 if (result != null) { // If we found an already-allocated or pooled connection, we're done. return result; }// If we need a route selection, make one. This is a blocking operation. // 切换路由 boolean newRouteSelection = false; if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) { newRouteSelection = true; routeSelection = routeSelector.next(); }synchronized (connectionPool) { if (canceled) throw new IOException("Canceled"); if (newRouteSelection) { // Now that we have a set of IP addresses, make another attempt at getting a connection from // the pool. This could match due to connection coalescing. List routes = routeSelection.getAll(); // 遍历路由集合,再从连接池中取 for (int i = 0, size = routes.size(); i < size; i++) { Route route = routes.get(i); Internal.instance.acquire(connectionPool, address, this, route); if (connection != null) { // 找到可用的连接 foundPooledConnection = true; result = connection; this.route = route; break; } } }// 如果没有找到可用的连接,则创建一条连接 if (!foundPooledConnection) { if (selectedRoute == null) { selectedRoute = routeSelection.next(); }// Create a connection and assign it to this allocation immediately. This makes it possible // for an asynchronous cancel() to interrupt the handshake we're about to do. // 创建一条连接 route = selectedRoute; refusedStreamCount = 0; result = new RealConnection(connectionPool, selectedRoute); // 往连接中添加流 acquire(result, false); } }// If we found a pooled connection on the 2nd time around, we're done. // 如果第二次找到可用的连接,则返回可用连接 if (foundPooledConnection) { eventListener.connectionAcquired(call, result); return result; }// Do TCP + TLS handshakes. This is a blocking operation. // 连接,开始三次握手 result.connect(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis, connectionRetryEnabled, call, eventListener); // RouteDatabase:A blacklist of failed routes to avoid when creating a new connection to a target address. // 将该路由从错误缓存记录中移除 routeDatabase().connected(result.route()); Socket socket = null; synchronized (connectionPool) { reportedAcquired = true; // Pool the connection. // 将这条连接放入连接池 Internal.instance.put(connectionPool, result); // If another multiplexed connection to the same address was created concurrently, then // release this connection and acquire that one. // 如果有其他复数连接到相同地址, 则删除重复连接 if (result.isMultiplexed()) { socket = Internal.instance.deduplicate(connectionPool, address, this); result = connection; } } // 关闭socket closeQuietly(socket); eventListener.connectionAcquired(call, result); return result; }

整理下该方法的逻辑:
1、拿到已存在的Connection对象,判断其是否可用(该连接可能不能创建新的流了),如果不能创建新流,则释放该Connection对象。
2、如果已存在的连接是不可用的,就从连接池中找可用连接(有可能找不到对应的可用连接)。
3、如果已存在的Connection对象可用或从连接池中可以取出connection对象,则直接返回。
4、都找不到可用连接,则切换路由重新从线程池中找可用连接,如果这次可以找到可用的连接,则返回该连接,否则,则创建一条连接。
5、创建一条连接后,开始连接,进行三次握手,并将这条连接放入连接池中。
在此,我们就成功找到了一条可用连接,然后我们回到newStream方法,通过连接来初始化HttpCodec对象:
public HttpCodec newCodec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation streamAllocation) throws SocketException { if (http2Connection != null) { return new Http2Codec(client, chain, streamAllocation, http2Connection); } else { socket.setSoTimeout(chain.readTimeoutMillis()); source.timeout().timeout(chain.readTimeoutMillis(), MILLISECONDS); sink.timeout().timeout(chain.writeTimeoutMillis(), MILLISECONDS); return new Http1Codec(client, streamAllocation, source, sink); } }

通过判断不同http协议(http1.1和http/2)来创建不同的HttpCodec对象。
通过StreamAllocation获取连接对象 在初始化HttpCodec对象的过程中,就已经通过findConnection方法查找到健康可用的连接对象了,所以我们可以通过StreamAllocation对象的connection方法直接获取到连接对象。
调用下一个拦截器 将初始化得到的HttpCodec和连接对象传入到下一拦截器进行处理,在此拦截器,我们就对RealInterceptorChain的几个属性都进行了初始化,用于在最后一个与服务器进行交互的拦截器中使用。
总结 该拦截器主要是与服务器建立连接,从而获取一个可以与服务器端进行连接的连接对象,而实际与服务器进行交互的逻辑则在CallServerInterceptor里。

    推荐阅读