OkHttp源码解析(二)- Interceptors 拦截器链工作流程

OkHttp的配置、使用步骤这里就不展开描述了,网络上有很多优秀的文章,这里主要是对学习源码中理解到的知识进行概括总结。

该文章根据OkHttp-3.11.0版本进行分析,并且强烈建议自己跟着源码配合文章的思路一起阅读

拦截器链是如何在OkHttp请求过程中被调用的请看我的另一篇文章:
OkHttp源码解析(一)- Dispatcher

  • 拦截器链

    意义:采用责任链模式,链中的每一个拦截器只做自己所负责的事情,将对Request的处理结果交给下一个拦截器,从最后一个拦截器开始,将返回结果依次返回给上一个拦截器,最终返回完整的Response结果。

    拦截器类型
    1. 用户自定义拦截器
    2. RetryAndFollowUpInterceptor:处理重试、重定向机制的拦截器。
    3. BridgeInterceptor:处理流大小、压缩、编码的拦截器。
    4. CacheInterceptor:处理缓存的拦截器。
    5. ConnectInterceptor:处理连接的拦截器。
    6. NetworkInterceptors:用户自定义的处理网络请求与响应的拦截器。
    7. CallServerInterceptor:处理与服务器交互细节的拦截器。

    拦截器定义的源码如下:

// Build a full stack of interceptors.
List<Interceptorinterceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(new RetryAndFollowUpInterceptor(client));
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));

我们用流程图的形式来描述拦截器链的工作过程:
Interceptors工作流程
从我的另一篇文章可以知道,拦截器链开始工作是通过 RealCall 当中的 getResponseWithInterceptorChain() 方法,我们看一下这个方法的源码:

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.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
}

可以看到各个不同的拦截器是按顺序添加进ArrayList中,在最后几行代码中构建了一个 RealInterceptorChain 对象,重点关注构造方法传递的第一个和第五个参数,第一个参数是拦截器的List集合, 第五个参数这里传的是0 ,这里加粗是需要注意这里传的是0。RealInterceptorChain 对象构建完之后立刻调用的自己的 proceed() 方法,我们跟踪进去看:

//构造方法
public RealInterceptorChain(List<Interceptor> interceptors, StreamAllocation streamAllocation,
      HttpCodec httpCodec, RealConnection connection, int index, Request request, Call call,
      EventListener eventListener, int connectTimeout, int readTimeout, int writeTimeout) {
    this.interceptors = interceptors;
    this.connection = connection;
    this.streamAllocation = streamAllocation;
    this.httpCodec = httpCodec;
    this.index = index;
    this.request = request;
    this.call = call;
    this.eventListener = eventListener;
    this.connectTimeout = connectTimeout;
    this.readTimeout = readTimeout;
    this.writeTimeout = writeTimeout;
}


//proceed方法
@Override public Response proceed(Request request) throws IOException {
    return proceed(request, streamAllocation, httpCodec, connection);
  }

public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
      RealConnection connection) throws IOException {
    if (index >= interceptors.size()) throw new AssertionError();

    calls++;

    // If we already have a stream, confirm that the incoming request will use it.
    if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must retain the same host and port");
    }

    // If we already have a stream, confirm that this is the only call to chain.proceed().
    if (this.httpCodec != null && calls > 1) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must call proceed() exactly once");
    }

    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
        connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
        writeTimeout);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);

    // Confirm that the next interceptor made its required call to chain.proceed().
    if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) {
      throw new IllegalStateException("network interceptor " + interceptor
          + " must call proceed() exactly once");
    }

    // Confirm that the intercepted response isn't null.
    if (response == null) {
      throw new NullPointerException("interceptor " + interceptor + " returned null");
    }

    if (response.body() == null) {
      throw new IllegalStateException(
          "interceptor " + interceptor + " returned a response with no body");
    }

    return response;
}

在构造方法当中可以看到只做了赋值操作,把index赋给了全局变量index,然后在proceed中调用了自己的4个参数的重载方法。在这个重载方法中我们忽略掉那些判断,重点关注其中的几行代码:

// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
        connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
        writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);

第一行就 new 了自己,又构建了一个对象,但是这次跟之前不同的是第5个参数 index+1,这里让下标增加了1,紧接着从拦截器的List集合中获取到当前下标的拦截器,这个时候我们应该能知道会发生什么,在 RealCall 中第一次构建 RealInterceptorChain 对象时,index为0,等到新的 RealInterceptorChain 对象执行它的 proceed() 方法时,index会+1,以此类推,直到index的值超过拦截器List集合的大小为止。

但是看到这里我们也只是看到取出了拦截器,下一次 RealInterceptorChain 调用 proceed() 是什么时候呢?我们再看下一行代码,将当前下标的拦截器取出之后,立即执行的它(拦截器)的 intercept() 方法,并且将 第五个参数为index+1RealInterceptorChain 对象作为参数传递进去,联系到文章之前添加拦截器的顺序,我们假设没有添加自定义的拦截器,那么现在第一个拦截器就是 RetryAndFollowUpInterceptor ,我们跟踪到这个拦截器的 intercept() 方法中去,记住,这个方法中的参数index是进行了+1操作的:

//这里的源码会忽略跟本篇文章分析拦截器工作流程无关的一部分代码
@Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Call call = realChain.call();
    EventListener eventListener = realChain.eventListener();

	//忽略部分代码

    while (true) {
      if (canceled) {
        streamAllocation.release();
        throw new IOException("Canceled");
      }

      Response response;
      boolean releaseConnection = true;
      try {
        response = realChain.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.
        if (!recover(e.getLastConnectException(), streamAllocation, false, request)) {
          throw e.getFirstConnectException();
        }
        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, streamAllocation, requestSendStarted, request)) throw e;
        releaseConnection = false;
        continue;
      } finally {
        // We're throwing an unchecked exception. Release any resources.
        if (releaseConnection) {
          streamAllocation.streamFailed(null);
          streamAllocation.release();
        }
      }

	  //忽略部分代码
    }
}

我们可以看到,首先将参数 chain 强转为 RealInterceptorChain ,然后在 try代码块 中又调用了 **proceed()**方法,这时我们再回到 RealInterceptorChain 的 **proceed()**方法中,此时index+1,取出的拦截器就是集合中第二次添加的 BridgeInterceptor 了,如此反复该项操作。

最后能够发现,在每一个拦截器的 proceed() 方法中都会对之前拦截器处理过的 request 再做自己的处理,处理之后返回 response 给上一个拦截器,这也印证了文章开头的介绍拦截器链意义的那句话以及流程图中 requestresponse 的传递顺序。

至于OkHttp中已经定义好的拦截器做了什么工作、我们自定义拦截器能够做什么工作,我找到了一篇讲解的比较好的文章:okhttp3源码分析之拦截器

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值