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

2024-02-18 17:32

本文主要是介绍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源码分析之拦截器。

这篇关于OkHttp源码解析(二)- Interceptors 拦截器链工作流程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/721912

相关文章

将Java项目提交到云服务器的流程步骤

《将Java项目提交到云服务器的流程步骤》所谓将项目提交到云服务器即将你的项目打成一个jar包然后提交到云服务器即可,因此我们需要准备服务器环境为:Linux+JDK+MariDB(MySQL)+Gi... 目录1. 安装 jdk1.1 查看 jdk 版本1.2 下载 jdk2. 安装 mariadb(my

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

Java字符串处理全解析(String、StringBuilder与StringBuffer)

《Java字符串处理全解析(String、StringBuilder与StringBuffer)》:本文主要介绍Java字符串处理全解析(String、StringBuilder与StringBu... 目录Java字符串处理全解析:String、StringBuilder与StringBuffer一、St

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

C#中async await异步关键字用法和异步的底层原理全解析

《C#中asyncawait异步关键字用法和异步的底层原理全解析》:本文主要介绍C#中asyncawait异步关键字用法和异步的底层原理全解析,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录C#异步编程一、异步编程基础二、异步方法的工作原理三、代码示例四、编译后的底层实现五、总结C#异步编程

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

Go 语言中的select语句详解及工作原理

《Go语言中的select语句详解及工作原理》在Go语言中,select语句是用于处理多个通道(channel)操作的一种控制结构,它类似于switch语句,本文给大家介绍Go语言中的select语... 目录Go 语言中的 select 是做什么的基本功能语法工作原理示例示例 1:监听多个通道示例 2:带

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++