zoukankan      html  css  js  c++  java
  • OkHttp3源码详解(三) 拦截器-RetryAndFollowUpInterceptor

    最大恢复追逐次数:

    private static final int MAX_FOLLOW_UPS = 20;

    处理的业务:

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

    源码

     1 @Override 
     2 public Response intercept(Chain chain) throws IOException {
     3   // 
     4   Request request = chain.request();
     5  // 1. 初始化一个socket连接对象
     6   streamAllocation = new StreamAllocation(
     7       client.connectionPool(), createAddress(request.url()), callStackTrace);
     8 
     9   int followUpCount = 0;
    10   Response priorResponse = null;
    11   while (true) {
    12      // 
    13     if (canceled) {
    14       streamAllocation.release();
    15       throw new IOException("Canceled");
    16     }
    17 
    18     Response response = null;
    19     boolean releaseConnection = true;
    20     try {
    21        // 2. 执行下一个拦截器,即BridgeInterceptor
    22       response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
    23       releaseConnection = false;
    24     } catch (RouteException e) {
    25       // The attempt to connect via a route failed. The request will not have been sent.
    26        // 3. 如果有异常,判断是否要恢复
    27       if (!recover(e.getLastConnectException(), false, request)) {
    28         throw e.getLastConnectException();
    29       }
    30       releaseConnection = false;
    31       continue;
    32     } catch (IOException e) {
    33       // An attempt to communicate with a server failed. The request may have been sent.
    34       boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
    35       if (!recover(e, requestSendStarted, request)) throw e;
    36       releaseConnection = false;
    37       continue;
    38     } finally {
    39       // We're throwing an unchecked exception. Release any resources.
    40       if (releaseConnection) {
    41         streamAllocation.streamFailed(null);
    42         streamAllocation.release();
    43       }
    44     }
    45 
    46     // Attach the prior response if it exists. Such responses never have a body.
    47     if (priorResponse != null) {
    48       response = response.newBuilder()
    49           .priorResponse(priorResponse.newBuilder()
    50                   .body(null)
    51                   .build())
    52           .build();
    53     }
    54  // 4. 检查是否符合要求
    55     Request followUp = followUpRequest(response);
    56 
    57     if (followUp == null) {
    58       if (!forWebSocket) {
    59         streamAllocation.release();
    60       }
    61       // 返回结果
    62       return response;
    63     }
    64  // 5. 不符合,关闭响应流
    65     closeQuietly(response.body());
    66  // 6. 是否超过最大限制
    67     if (++followUpCount > MAX_FOLLOW_UPS) {
    68       streamAllocation.release();
    69       throw new ProtocolException("Too many follow-up requests: " + followUpCount);
    70     }
    71 
    72     if (followUp.body() instanceof UnrepeatableRequestBody) {
    73       streamAllocation.release();
    74       throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
    75     }
    76  // 7. 是否有相同的连接
    77     if (!sameConnection(response, followUp.url())) {
    78       streamAllocation.release();
    79       streamAllocation = new StreamAllocation(
    80           client.connectionPool(), createAddress(followUp.url()), callStackTrace);
    81     } else if (streamAllocation.codec() != null) {
    82       throw new IllegalStateException("Closing the body of " + response
    83           + " didn't close its backing stream. Bad interceptor?");
    84     }
    85 
    86     request = followUp;
    87     priorResponse = response;
    88   }
    89 }

    初始化连接对象

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

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

    继续下一个拦截器

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

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

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

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

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

  • 相关阅读:
    <<软技能,代码之外的生存技能>>读书笔记
    Pma模块详解,对用户登录linux等进行限制,密码修改限制等
    numpy pandas matplotlib
    numpy安装包scipy
    linux python 安装 nose lapack atlas numpy scipy
    kafka搭建
    实例化Bean的方法(基于xml配置)-http://blog.csdn.net/shymi1991/article/details/48153293
    转-Vue.js2.0从入门到放弃---入门实例(一)
    form表单传递对象数组
    使用ajax提交form表单,包括ajax文件上传 转http://www.cnblogs.com/zhuxiaojie/p/4783939.html
  • 原文地址:https://www.cnblogs.com/ganchuanpu/p/6880555.html
Copyright © 2011-2022 走看看