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

相关文章

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

SpringBoot项目Web拦截器使用的多种方式

《SpringBoot项目Web拦截器使用的多种方式》在SpringBoot应用中,Web拦截器(Interceptor)是一种用于在请求处理的不同阶段执行自定义逻辑的机制,下面给大家介绍Sprin... 目录一、实现 HandlerInterceptor 接口1、创建HandlerInterceptor实

使用JavaConfig配置Spring的流程步骤

《使用JavaConfig配置Spring的流程步骤》JavaConfig是Spring框架提供的一种基于Java的配置方式,它通过使用@Configuration注解标记的类来替代传统的XML配置文... 目录一、什么是 JavaConfig?1. 核心注解2. 与 XML 配置的对比二、JavaConf

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Python多进程、多线程、协程典型示例解析(最新推荐)

《Python多进程、多线程、协程典型示例解析(最新推荐)》:本文主要介绍Python多进程、多线程、协程典型示例解析(最新推荐),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 目录一、multiprocessing(多进程)1. 模块简介2. 案例详解:并行计算平方和3. 实现逻

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

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实