OkHttp3源码详解(三) 拦截器-RetryAndFollowUpInterceptor

最大恢复追逐次数:

private static final int MAX_FOLLOW_UPS = ;

处理的业务:

  1. 实例化StreamAllocation,初始化一个Socket连接对象,获取到输入/输出流()基于Okio
  2. 开启循环,执行下一个调用链(拦截器),等待返回结果(Response)
  3. 如果发生错误,判断是否继续请求,否:退出
  4. 检查响应是否符合要求,是:返回
  5. 关闭响应结果
  6. 判断是否达到最大限制数,是:退出
  7. 检查是否有相同连接,是:释放,重建连接
  8. 重复以上流程

源码

 @Override
public Response intercept(Chain chain) throws IOException {
//
Request request = chain.request();
// 1. 初始化一个socket连接对象
streamAllocation = new StreamAllocation(
client.connectionPool(), createAddress(request.url()), callStackTrace); int followUpCount = ;
Response priorResponse = null;
while (true) {
//
if (canceled) {
streamAllocation.release();
throw new IOException("Canceled");
} Response response = null;
boolean releaseConnection = true;
try {
// 2. 执行下一个拦截器,即BridgeInterceptor
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
releaseConnection = false;
} catch (RouteException e) {
// The attempt to connect via a route failed. The request will not have been sent.
// 3. 如果有异常,判断是否要恢复
if (!recover(e.getLastConnectException(), false, request)) {
throw e.getLastConnectException();
}
releaseConnection = false;
continue;
} catch (IOException e) {
// An attempt to communicate with a server failed. The request may have been sent.
boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
if (!recover(e, requestSendStarted, request)) throw e;
releaseConnection = false;
continue;
} finally {
// We're throwing an unchecked exception. Release any resources.
if (releaseConnection) {
streamAllocation.streamFailed(null);
streamAllocation.release();
}
} // Attach the prior response if it exists. Such responses never have a body.
if (priorResponse != null) {
response = response.newBuilder()
.priorResponse(priorResponse.newBuilder()
.body(null)
.build())
.build();
}
// 4. 检查是否符合要求
Request followUp = followUpRequest(response); if (followUp == null) {
if (!forWebSocket) {
streamAllocation.release();
}
// 返回结果
return response;
}
// 5. 不符合,关闭响应流
closeQuietly(response.body());
// 6. 是否超过最大限制
if (++followUpCount > MAX_FOLLOW_UPS) {
streamAllocation.release();
throw new ProtocolException("Too many follow-up requests: " + followUpCount);
} if (followUp.body() instanceof UnrepeatableRequestBody) {
streamAllocation.release();
throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
}
// 7. 是否有相同的连接
if (!sameConnection(response, followUp.url())) {
streamAllocation.release();
streamAllocation = new StreamAllocation(
client.connectionPool(), createAddress(followUp.url()), callStackTrace);
} else if (streamAllocation.codec() != null) {
throw new IllegalStateException("Closing the body of " + response
+ " didn't close its backing stream. Bad interceptor?");
} request = followUp;
priorResponse = response;
}
}

初始化连接对象

streamAllocation = new StreamAllocation(
client.connectionPool(), createAddress(request.url()), callStackTrace);

注意:此处还没有真正的去建立连接,只是初始化一个连接对象

继续下一个拦截器

上面一步初始化好后,将继续执行下一个连接器BridgeInterceptor,

// 这里有个很重的信息,即会将初始化好的连接对象传递给下一个拦截器,也是贯穿整个请求的连击对象,
// 上文我们说过,在拦截器执行过程中,RealInterceptorChain的几个属性字段会一步一步赋值
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);

抛出异常
如果抛出异常,将判断是否能够继续连接,以下情况不在,重试:

 /**
* 不在继续连接的情况:
* 1. 应用层配置不在连接,默认为true
* 2. 请求Request出错不能继续使用
* 3. 是否可以恢复的
* 3.1、协议错误(ProtocolException)
3.2、中断异常(InterruptedIOException)
3.3、SSL握手错误(SSLHandshakeException && CertificateException)
3.4、certificate pinning错误(SSLPeerUnverifiedException)
* 4. 没用更多线路可供选择
*/
private boolean recover(IOException e, boolean requestSendStarted, Request userRequest) {
streamAllocation.streamFailed(e);
// 1. 应用层配置不在连接,默认为true
// The application layer has forbidden retries.
if (!client.retryOnConnectionFailure()) return false; // 2. 请求Request出错不能继续使用
// We can't send the request body again.
if (requestSendStarted && userRequest.body() instanceof UnrepeatableRequestBody) return false; // 是否可以恢复的
// This exception is fatal.
if (!isRecoverable(e, requestSendStarted)) return false; // 4. 没用更多线路可供选择
// No more routes to attempt.
if (!streamAllocation.hasMoreRoutes()) return false; // For failure recovery, use the same route selector with a new connection.
return true;
}

http://lowett.com/categories/Android/Okhttp3/

上一篇:java.lang.NoSuchMethodError: org.apache.tomcat.util.res.StringManager.getManager(Ljava/lang/Class;)Lorg/apache/tomcat/util/res/StringManager


下一篇:MapReduce入门例子