开局一张图剩下全靠编

  • 1绿色部分表示共有流程
  • 2红色部分表示异步请求
  • 蓝色部分表示同步请求

OkHttp(零)整体流程分析 基于okhttp:3.13.1
Dispatcher调度过程 Call 出入栈 过程 流程分析 ->就是上图了
可以看到最终都是栈中移除掉Call 这是次级重点 重点是下面
同步异步都执行了getRespinseWithIntercepotrChain(); 这是什么?-> 通过拦截器链 来获取response

那我们看看这里做了什么操作

Response getResponseWithInterceptorChain() throws IOException {
    // Build a full stack of interceptors.
   List<Interceptor> interceptors = new ArrayList<>();
    interceptors.addAll(client.interceptors()); // 加入用户自定义的拦截器
    interceptors.add(retryAndFollowUpInterceptor); // 重试和重定向拦截器
    interceptors.add(new BridgeInterceptor(client.cookieJar())); // 加入转化请求响应的拦截器
    interceptors.add(new CacheInterceptor(client.internalCache())); // 加入缓存拦截器
    interceptors.add(new ConnectInterceptor(client)); // 加入连接拦截器
    if (!forWebSocket) {
        interceptors.addAll(client.networkInterceptors()); // 加入用户自定义的网络拦截器
    }
    interceptors.add(new CallServerInterceptor(forWebSocket)); // 加入发出请求和读取响应的拦截器
    
    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
            originalRequest, this, eventListener, client.readTimeoutMillis());
    // 利用 chain 来链式调用拦截器,最后的返回结果就是 Response 对象
    return chain.proceed(originalRequest);
  }
}

整合了这些拦截器利用RealInterceptorChain链式调用这些拦截器获取response,利用的就是责任链模式。
通过chain.proceed(originrequest)这个方法
好那我们看下proceed方法里做了什么

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {
    if (index >= interceptors.size()) throw new AssertionError();
////....
    calls++;
    // Call the next interceptor in the chain.调用下一个拦截器
    // 得到下一次对应的 RealInterceptorChain
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
            connection, index + 1, request, call, eventListener, readTimeout);
    // 当前次数的 interceptor
    Interceptor interceptor = interceptors.get(index);
    // 进行拦截处理,并且在 interceptor 链式调用 next 的 proceed 方法
    Response response = interceptor.intercept(next);

    // 确认下一次的 interceptor 调用过 chain.proceed()
    if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
        throw new IllegalStateException("network interceptor " + interceptor
                + " must call proceed() exactly once");
    }
///.....
    return response;
  }
}

ok 在方法里面又创建了一个RealInterceptorChain(index+1)就是下一个拦截器
又调用了当前index 拦截器的intercept(next)的方法 传进去的是下一个拦截器我们
如果用户不添加自定的拦截器那第一个拦截器就是retryAndFollowUpInterceptor
在这里调用了retryAndFollowUpInterceptor的intercept方法 我们看Intercept方法内部

public Response intercept(Chain chain) throws IOException {
      ···
      try {
      //2 调用proceed方法 方法内又创建一个RealInterceptorChain(index+1) 调用 intercep方法
      //执行下一个拦截器链
        response = realChain.proceed(request, streamAllocation, null, null);
        releaseConnection = false;
      } 
      ···
  }

直接看重点
intercept方法内部又调用了proceed方法 执行下一个拦截器
proceed方法内又创建了一个拦截器链RealInterceptorChain(index+1)
当前index的拦截器链又会执行intercept(next)传入下一个拦截器index+1的就是上面新创建的拦截器RealInterceptorChain(index+1)
intercept方法内部又调用了proceed方法执行下一个拦截器 构成了一个递归的调用。。一直到最后一个拦截器 看下图
OkHttp(零)整体流程分析 基于okhttp:3.13.1
getResponseWithInterceptorChain 内部拦截器的具体分析请看下面文章
OkHttp(一) 拦截器之开篇 内部拦截器简介以及 retryAndFollowUpInterceptor BridgeInterceptor CacheInterceptor

相关文章: