RetryAndFollowUpInterceptor是重试重定向拦截器。它主要是负责失败重连的。因为在 OKHTTP 中的拦截器的执行过程是一个递归的过程,也就是它内部会通过 RealInterceptorChain 这个类去负责将所有的拦截器进行串起来。只有所有的拦截器执行完毕之后,一个网络请求的响应 Response 才会被返回。
但是,在执行这个过程中,难免会出现一些问题,例如连接中断,握手失败或者服务器检测到未认证等,那么这个 resposne 的返回码就不是正常的 200 了,因此说这个 response 并不一定是可用的,或者说在请求过程就已经抛出异常了,例如超时异常等,那么 RetryAndFollowUpInterceptor 需要依据这些问题进行判断是否可以进行重新连接。
主要我们还是分析intercept方法
首先,它创建了一个StreamAllocation对象
StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()), call, eventListener, callStackTrace); this.streamAllocation = streamAllocation;
这个对象是用来建立http请求需要的网络组件的,从这个类名我们可以看出,是来分配stream的。虽然它是在这里创建的,但并未在这个拦截器中使用。
异常检测
下面进入了一个while(true)循环,通过proceed方法,去获取下一个拦截器的response。这边会捕获RouteException这个异常,下图我们可以看到这个异常会在哪些地方抛出
对抛出的异常代码里用recover方法进行了检测
catch (RouteException e) { // The attempt to connect via a route failed. The request will not have been sent. if (!recover(e.getLastConnectException(), streamAllocation, false, request)) { throw e.getLastConnectException(); } releaseConnection = false; continue; }
看下面的recover方法,注释里已经做了解释
private boolean recover(IOException e, boolean routeException, Request userRequest) { streamAllocation.streamFailed(e); //1.判断 OkHttpClient 是否支持失败重连的机制 // The application layer has forbidden retries. if (!client.retryOnConnectionFailure()) return false; // 在该方法中传入的 routeException值 为 true // We can't send the request body again. if (!routeException && userRequest.body() instanceof UnrepeatableRequestBody) return false; //2.isRecoverable 检测该异常是否是致命的。 // This exception is fatal. if (!isRecoverable(e, routeException)) return false; // No more routes to attempt. //3.是否有更多的路线 if (!streamAllocation.hasMoreRoutes()) return false; // For failure recovery, use the same route selector with a new connection. return true; }
private boolean isRecoverable(IOException e, boolean routeException) { //ProtocolException 这种异常属于严重异常,不能进行重新连接 // If there was a protocol problem, don't recover. if (e instanceof ProtocolException) { return false; } //当异常为中断异常时 // If there was an interruption don't recover, but if there was a timeout connecting to a route // we should try the next route (if there is one). if (e instanceof InterruptedIOException) { return e instanceof SocketTimeoutException && routeException; } // Look for known client-side or negotiation errors that are unlikely to be fixed by trying // again with a different route. //握手异常 if (e instanceof SSLHandshakeException) { // If the problem was a CertificateException from the X509TrustManager, // do not retry. if (e.getCause() instanceof CertificateException) { return false; } } //验证异常 if (e instanceof SSLPeerUnverifiedException) { // e.g. a certificate pinning error. return false; } // An example of one we might want to retry with a different route is a problem connecting to a // proxy and would manifest as a standard IOException. Unless it is one we know we should not // retry, we return true and try a new route. return true;
响应码检测
158行,下面的代码是对响应码进行检测,能走到这,说明请求是成功的,但服务器返回不是200的情况,具体代码就不贴了,注释很详细
Request followUp = followUpRequest(response, streamAllocation.route());
重试次数判断
再看169行
if (++followUpCount > MAX_FOLLOW_UPS) { streamAllocation.release(); throw new ProtocolException("Too many follow-up requests: " + followUpCount); }
上面的代码对最大重试次数做了限制,通过阅读代码,我们知道这个数是20,如果超过了重试次数,便会释放连接。