最大恢复追逐次数:

private static final int MAX_FOLLOW_UPS = 20;

处理的业务:

  1. 实例化StreamAllocation,初始化一个Socket连接对象,获取到输入/输出流()基于Okio
  2. 开启循环,执行下一个调用链(拦截器),等待返回结果(Response)
  3. 如果发生错误,判断是否继续请求,否:退出
  4. 检查响应是否符合要求,是:返回
  5. 关闭响应结果
  6. 判断是否达到最大限制数,是:退出
  7. 检查是否有相同连接,是:释放,重建连接
  8. 重复以上流程

源码

 1 @Override
 2 public Response intercept(Chain chain) throws IOException {
 3   //
 4   Request request = chain.request();
 5  // 1. 初始化一个socket连接对象
 6   streamAllocation = new StreamAllocation(
 7       client.connectionPool(), createAddress(request.url()), callStackTrace);
 8
 9   int followUpCount = 0;
10   Response priorResponse = null;
11   while (true) {
12      //
13     if (canceled) {
14       streamAllocation.release();
15       throw new IOException("Canceled");
16     }
17
18     Response response = null;
19     boolean releaseConnection = true;
20     try {
21        // 2. 执行下一个拦截器,即BridgeInterceptor
22       response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
23       releaseConnection = false;
24     } catch (RouteException e) {
25       // The attempt to connect via a route failed. The request will not have been sent.
26        // 3. 如果有异常,判断是否要恢复
27       if (!recover(e.getLastConnectException(), false, request)) {
28         throw e.getLastConnectException();
29       }
30       releaseConnection = false;
31       continue;
32     } catch (IOException e) {
33       // An attempt to communicate with a server failed. The request may have been sent.
34       boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
35       if (!recover(e, requestSendStarted, request)) throw e;
36       releaseConnection = false;
37       continue;
38     } finally {
39       // We're throwing an unchecked exception. Release any resources.
40       if (releaseConnection) {
41         streamAllocation.streamFailed(null);
42         streamAllocation.release();
43       }
44     }
45
46     // Attach the prior response if it exists. Such responses never have a body.
47     if (priorResponse != null) {
48       response = response.newBuilder()
49           .priorResponse(priorResponse.newBuilder()
50                   .body(null)
51                   .build())
52           .build();
53     }
54  // 4. 检查是否符合要求
55     Request followUp = followUpRequest(response);
56
57     if (followUp == null) {
58       if (!forWebSocket) {
59         streamAllocation.release();
60       }
61       // 返回结果
62       return response;
63     }
64  // 5. 不符合,关闭响应流
65     closeQuietly(response.body());
66  // 6. 是否超过最大限制
67     if (++followUpCount > MAX_FOLLOW_UPS) {
68       streamAllocation.release();
69       throw new ProtocolException("Too many follow-up requests: " + followUpCount);
70     }
71
72     if (followUp.body() instanceof UnrepeatableRequestBody) {
73       streamAllocation.release();
74       throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
75     }
76  // 7. 是否有相同的连接
77     if (!sameConnection(response, followUp.url())) {
78       streamAllocation.release();
79       streamAllocation = new StreamAllocation(
80           client.connectionPool(), createAddress(followUp.url()), callStackTrace);
81     } else if (streamAllocation.codec() != null) {
82       throw new IllegalStateException("Closing the body of " + response
83           + " didn't close its backing stream. Bad interceptor?");
84     }
85
86     request = followUp;
87     priorResponse = response;
88   }
89 }

初始化连接对象

streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()), callStackTrace);

注意:此处还没有真正的去建立连接,只是初始化一个连接对象

继续下一个拦截器

上面一步初始化好后,将继续执行下一个连接器BridgeInterceptor,

// 这里有个很重的信息,即会将初始化好的连接对象传递给下一个拦截器,也是贯穿整个请求的连击对象,
// 上文我们说过,在拦截器执行过程中,RealInterceptorChain的几个属性字段会一步一步赋值
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);

抛出异常
如果抛出异常,将判断是否能够继续连接,以下情况不在,重试:

 1 /**
 2 * 不在继续连接的情况:
 3 * 1. 应用层配置不在连接,默认为true
 4 * 2. 请求Request出错不能继续使用
 5 * 3. 是否可以恢复的
 6 *   3.1、协议错误(ProtocolException)
 7     3.2、中断异常(InterruptedIOException)
 8     3.3、SSL握手错误(SSLHandshakeException && CertificateException)
 9     3.4、certificate pinning错误(SSLPeerUnverifiedException)
10 * 4. 没用更多线路可供选择
11 */
12 private boolean recover(IOException e, boolean requestSendStarted, Request userRequest) {
13   streamAllocation.streamFailed(e);
14   // 1. 应用层配置不在连接,默认为true
15   // The application layer has forbidden retries.
16   if (!client.retryOnConnectionFailure()) return false;
17
18   // 2. 请求Request出错不能继续使用
19   // We can't send the request body again.
20   if (requestSendStarted && userRequest.body() instanceof UnrepeatableRequestBody) return false;
21
22   //  是否可以恢复的
23   // This exception is fatal.
24   if (!isRecoverable(e, requestSendStarted)) return false;
25
26   // 4. 没用更多线路可供选择
27   // No more routes to attempt.
28   if (!streamAllocation.hasMoreRoutes()) return false;
29
30   // For failure recovery, use the same route selector with a new connection.
31   return true;
32 }

http://lowett.com/categories/Android/Okhttp3/

转载于:https://www.cnblogs.com/ganchuanpu/p/6880555.html

OkHttp3源码详解(三) 拦截器-RetryAndFollowUpInterceptor相关推荐

  1. OkHttp3源码详解

    前言:为什么有些人宁愿吃生活的苦也不愿吃学习的苦,大概是因为懒惰吧,学习的苦是需要自己主动去吃的,而生活的苦,你躺着不动它就会来找你了. 一.概述 OKHttp是一个非常优秀的网络请求框架,已经被谷歌 ...

  2. Java源码详解三:Hashtable源码分析--openjdk java 11源码

    文章目录 注释 哈希算法与映射 线程安全的实现方法 put 操作 get操作 本系列是Java详解,专栏地址:Java源码分析 Hashtable官方文档:Hashtable (Java Platfo ...

  3. OkHttp3源码详解(五) okhttp连接池复用机制

    1.概述 提高网络性能优化,很重要的一点就是降低延迟和提升响应速度. 通常我们在浏览器中发起请求的时候header部分往往是这样的 keep-alive 就是浏览器和服务端之间保持长连接,这个连接是可 ...

  4. okhttp3 请求html页面,OkHttp3源码详解(二) 整体流程

    1.简单使用 同步:@Override public Response execute() throws IOException { synchronized (this) { if (execute ...

  5. Tensorflow 2.x(keras)源码详解之第九章:模型训练和预测的三种方法(fittf.GradientTapetrain_steptf.data)

      大家好,我是爱编程的喵喵.双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中.从事机器学习以及相关的前后端开发工作.曾在阿里云.科大讯飞.CCF等比赛获得多次Top名次.现 ...

  6. Tensorflow 2.x源码详解之第三章:导数(梯度/GradientTape)

      大家好,我是爱编程的喵喵.双985硕士毕业,现担任全栈工程师一职,热衷于将数据思维应用到工作与生活中.从事机器学习以及相关的前后端开发工作.曾在阿里云.科大讯飞.CCF等比赛获得多次Top名次.现 ...

  7. Spring事务源码详解

    一. 简介 事务: 事务是逻辑上的一组操作,要么都执行,要么都不执行,关于事务的基本知识可以看我的这篇文章:事务的基础知识 Spring事务: Spring 支持两种方式的事务管理:编程式事务管理.声 ...

  8. 【Live555】live555源码详解(八):testRTSPClient

    [Live555]live555源码详解系列笔记 继承协作关系图 下面红色表示本博客将要介绍的testRTSPClient实现的三个类所在的位置: ourRTSPClient.StreamClient ...

  9. 封装成jar包_通用源码阅读指导mybatis源码详解:io包

    io包 io包即输入/输出包,负责完成 MyBatis中与输入/输出相关的操作. 说到输入/输出,首先想到的就是对磁盘文件的读写.在 MyBatis的工作中,与磁盘文件的交互主要是对 xml配置文件的 ...

最新文章

  1. 计算机组装人员的职责,自控设备组装员
  2. 看AI如何改变医疗影像学
  3. windows常见的运行命令以及各快捷键组合
  4. 置springboot自带tomcat的最大连接数和最大并发数
  5. cocos2d-x游戏开发(二)开始菜单续
  6. 360 自动打开word_Word文档高手的组合键用法,你知道几个?
  7. HTML+CSS+JS实现 ❤️贪吃蛇游戏、你能吃过我?❤️【源码送给你一起来玩-建议收藏】
  8. 在CSDN写文章头部生成标题目录
  9. Python学习札记(十一) Function2 函数定义
  10. hadoop 如何连beeline_关于hadoop:将日期函数设置为变量并在beeline和hql文件中使用(hive)...
  11. 螺杆泵matlab,基于Matlab与VC++混合编程的螺杆泵转子型线设计
  12. 已为此响应调用getwriter()_远程过程调用(RPC)
  13. html中如何把两行合并单元格,怎么把表格上下两行合并单元格合并
  14. 《自己动手写CPU》学习记录(1)——第1章
  15. 苹果手机电池健康怎么看_打开手机这个功能,就能知道手机电池的健康度,安卓苹果都可以!...
  16. 学习笔记之MyEclipse里各个文件名前的小标记都代表的意思
  17. docker安装kong和konga详细说明
  18. Python 爬金十数据
  19. linux运行php文件
  20. inline-block中居中元素

热门文章

  1. 【机器学习】无监督学习
  2. 《数学之美》第7章 贾里尼克和现代语言处理
  3. 与微信、APP正面刚?三大运营商联合发布5G消息白皮书
  4. 你认识的世界与客观世界间差了N光年
  5. 智能家居市场年增速近30%!苹果看齐亚马逊、谷歌,欲开辟三足鼎立
  6. 100年前没人信他,但他仅1项研究便远程摧毁万架飞机……
  7. 寿命能推算吗?加州大学科学家提出“预测方法”
  8. 特朗普即将主持AI会议 中美竞争成焦点
  9. NASA投资有远景技术,有望改变未来人类和机器人的勘探任务
  10. 2017全球教育机器人行业研究报告(附PDF下载)