RetryAndFollowUpInterceptor 实现原理和功能分析

RetryAndFollowUpInterceptor 实现原理和功能分析

Overview
该拦截器功能有
* 创建Address
* 创建StreamAllocation
* 处理响应结果,例如重定向或者重试

这个类最核心的方法就是它的
```java
@Override public Response intercept(Chain chain) throws IOException
```
这个Chain是在RealCall中的 getResponseWithInterceptorChain()构造并传入的。

下面是该方法的实现

1.创建Address和StreamAllocation
```java
private Address createAddress(HttpUrl url) {
    SSLSocketFactory sslSocketFactory = null;
    HostnameVerifier hostnameVerifier = null;
    CertificatePinner certificatePinner = null;
    if (url.isHttps()) {
      sslSocketFactory = client.sslSocketFactory();
      hostnameVerifier = client.hostnameVerifier();
      certificatePinner = client.certificatePinner();
    }

    return new Address(url.host(), url.port(), client.dns(), client.socketFactory(),
        sslSocketFactory, hostnameVerifier, certificatePinner, client.proxyAuthenticator(),
        client.proxy(), client.protocols(), client.connectionSpecs(), client.proxySelector());
  }
 ```

 通过此代码创建一个Address,然后创建一个StreamAllocation,这个类算是OkHttp的核心类,创建Socket和连接Socket都在这个类当中。
2.执行chain.proceed()
```
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
releaseConnection = false;
```

将Request和构建好的streamAllocation传递给后面的interceptor,并返回Response
3.处理响应结果,例如重定向或者重试
得到Response,根据响应状态做响应的处理

怎么实现重定向和重试的?

这是其 public Response intercept(Chain chain) 的一段大致流程代码,
这段代码不是源码,是我在源码的基础上认为比较重要的复制出来拼在一起的
很佩服作者的思路,真的很巧妙。
用一个死循环来做的,重定向是根据结果返回的的来决定是否需要跳出死循环,
重试是根据抛出的IOException的时候判断是将异常抛出还是继续执行循环来做的

```
 while (true){

    //proceed
    try {

        response = chain.proceed(request, streamAllocation, null, null)

    }catch (IOException e) {
         boolean requestSendStarted = !(e instanceof ConnectionShutdownException);
    //这是其中的一个异常处理 ,recover是判断时候需要重试,若需要重试继续循环,不需要则抛出异常退出循环
         if (!recover(e, requestSendStarted, request)) throw e;
         releaseConnection = false;
         continue;
    }
    //根据response判断是否需要重定向 ,不要返回null 需要返回重定向的Request
    Request followUp = followUpRequest(response)

    //不需要重定向 返回response
    if (followUp == null){
        return response
    }

    //是否达到最大允许的重定向次数
    if (++followUpCount > MAX_FOLLOW_UPS) {
        streamAllocation.release()
        throw ProtocolException("Too many follow-up requests: " + followUpCount)
    }

    //重新构建streamAllocation
    if (!sameConnection(response, followUp.url())) {
        streamAllocation.release();
        streamAllocation = new StreamAllocation(
                client.connectionPool(), createAddress(followUp.url()), callStackTrace);
    } else if (streamAllocation.codec() != null) {
        throw new IllegalStateException("Closing the body of " + response
                + " didn't close its backing stream. Bad interceptor?");
    }

    接着循环
}
```

End 到此这个类的功能原理就很清楚了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 简介 目前在HTTP协议请求库中,OKHttp应当是非常火的,使用也非常的简单。网上有很多文章写了关于OkHttp...
    第八区阅读 1,441评论 1 5
  • OkHttp解析系列 OkHttp解析(一)从用法看清原理OkHttp解析(二)网络连接OkHttp解析(三)关于...
    Hohohong阅读 21,119评论 4 58
  • 关于okhttp是一款优秀的网络请求框架,关于它的源码分析文章有很多,这里分享我在学习过程中读到的感觉比较好的文章...
    蕉下孤客阅读 3,724评论 2 38
  • 前言:对于OkHttp我接触的时间其实不太长,一直都是使用Retrofit + OkHttp 来做网络请求的,但是...
    mecury阅读 41,465评论 23 178
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,485评论 19 139

友情链接更多精彩内容