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 DTO(最新推荐)

《深度解析JavaDTO(最新推荐)》DTO(DataTransferObject)是一种用于在不同层(如Controller层、Service层)之间传输数据的对象设计模式,其核心目的是封装数据,... 目录一、什么是DTO?DTO的核心特点:二、为什么需要DTO?(对比Entity)三、实际应用场景解析

深度解析Java项目中包和包之间的联系

《深度解析Java项目中包和包之间的联系》文章浏览阅读850次,点赞13次,收藏8次。本文详细介绍了Java分层架构中的几个关键包:DTO、Controller、Service和Mapper。_jav... 目录前言一、各大包1.DTO1.1、DTO的核心用途1.2. DTO与实体类(Entity)的区别1

Java中的雪花算法Snowflake解析与实践技巧

《Java中的雪花算法Snowflake解析与实践技巧》本文解析了雪花算法的原理、Java实现及生产实践,涵盖ID结构、位运算技巧、时钟回拨处理、WorkerId分配等关键点,并探讨了百度UidGen... 目录一、雪花算法核心原理1.1 算法起源1.2 ID结构详解1.3 核心特性二、Java实现解析2.

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

深度解析Spring AOP @Aspect 原理、实战与最佳实践教程

《深度解析SpringAOP@Aspect原理、实战与最佳实践教程》文章系统讲解了SpringAOP核心概念、实现方式及原理,涵盖横切关注点分离、代理机制(JDK/CGLIB)、切入点类型、性能... 目录1. @ASPect 核心概念1.1 AOP 编程范式1.2 @Aspect 关键特性2. 完整代码实