Android开发Android进阶之路Android技术知识

OkHttp 源码学习(四)RetryAndFollowUpIn

2019-02-26  本文已影响6人  Android架构

OkHttp 源码学习(二)基本流程这节中曾带大家认识了Interceptor拦截器的概念,不记得的同学可以回顾一下。从本节开始将从源码带角度逐个介绍其中的5个重要拦截器,首先来认识一下RetryAndFollowUpInterceptor

什么是RetryAndFollowUpInterceptor?

用一句话来说,它是okhttp核心拦截器之一,该拦截器主要用于失败重试以及必要的请求重定向。

整个拦截器流程:
step1 初始化StreamAllocation对象
step2 调用RealInterceptorChain.proceed()方法进行网络请求
step3 根据请求响应结果或者异常结果判断是否需要重新请求
step4 对response处理并返回给上一个拦截器

如何初始化

回到RealCallgetResponseWithInterceptorChain()这个方法里,我们发现这个retryAndFollowUpInterceptor的初始化和其他几个拦截器不太一样。

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));
    ···
    return chain.proceed(originalRequest);
  }

我们找到retryAndFollowUpInterceptor的初始化方法。

private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    this.client = client;
    this.originalRequest = originalRequest;
    this.forWebSocket = forWebSocket;
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
  }

RealCall的构造函数中进行了初始化,可以看到RetryAndFollowUpInterceptor在初始化时持有了client的引用。

核心方法intercept()

RetryAndFollowUpInterceptor的核心就是这个方法,我们一起来看下源码。

  @Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    RealInterceptorChain realChain = (RealInterceptorChain) chain;
    Call call = realChain.call();
    EventListener eventListener = realChain.eventListener();

    streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()),
        call, eventListener, callStackTrace);
    ···
  }

这里都是一些赋值操作,我简单说一下RealInterceptorChainStreamAllocation这两个类,也是okhttp的核心之一。

我们注意到在intercepter()方法中有一段while(true)循环

int followUpCount = 0;
    Response priorResponse = null;
    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(), false, request)) {
          throw e.getLastConnectException();
        }
        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, requestSendStarted, request)) throw e;
        releaseConnection = false;
        continue;
      } finally {
        // We're throwing an unchecked exception. Release any resources.
        if (releaseConnection) {
          streamAllocation.streamFailed(null);
          streamAllocation.release();
        }
      }

      // Attach the prior response if it exists. Such responses never have a body.
      if (priorResponse != null) {
        response = response.newBuilder()
            .priorResponse(priorResponse.newBuilder()
                    .body(null)
                    .build())
            .build();
      }

      Request followUp = followUpRequest(response);

      if (followUp == null) {
        if (!forWebSocket) {
          streamAllocation.release();
        }
        return response;
      }

      closeQuietly(response.body());

      if (++followUpCount > MAX_FOLLOW_UPS) {
        streamAllocation.release();
        throw new ProtocolException("Too many follow-up requests: " + followUpCount);
      }

      if (followUp.body() instanceof UnrepeatableRequestBody) {
        streamAllocation.release();
        throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
      }

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

      request = followUp;
      priorResponse = response;
    }

代码很多,这一部分主要就是执行前面讲到的流程中step2、step3、step4步骤,注意到这样一句

if (++followUpCount > MAX_FOLLOW_UPS) {
        streamAllocation.release();
        throw new ProtocolException("Too many follow-up requests: " + followUpCount);
      }

MAX_FOLLOW_UPS表示我们最多重试的次数。
【附录】

资料图

需要资料的朋友可以加入Android架构交流QQ群聊:513088520

点击链接加入群聊【Android移动架构总群】:加入群聊

获取免费学习视频,学习大纲另外还有像高级UI、性能优化、架构师课程、NDK、混合式开发(ReactNative+Weex)等Android高阶开发资料免费分享。

上一篇下一篇

猜你喜欢

热点阅读