關于Okhttp3(四)-RetryAndFollowUpInterceptor

前文講解了整體流程,今天進入第一個攔截器RetryAndFollowUpInterceptor。

官網解釋如下:

This interceptor recovers from failures and follows redirects as necessary. It may throw an {@link IOException} if the call was canceled.

最大恢復重試次數:

private static final int MAX_FOLLOW_UPS = 20;

前文我們知道每個攔截器都實現了接口Interceptor,Interceptor.intercept() 方法就是子類用來處理,自己的業務邏輯,所以我們只要分析此方法即可。

處理的業務

  1. 實例化StreamAllocation,初始化一個Socket連接對象,獲取到輸入/輸出流()基于Okio
  2. 開啟循環,執行下一個調用鏈(攔截器),等待返回結果(Response)
  3. 如果發生錯誤,判斷是否繼續請求,否:退出
  4. 檢查響應是否符合要求,是:返回
  5. 關閉響應結果
  6. 判斷是否達到最大限制數,是:退出
  7. 檢查是否有相同連接,是:釋放,重建連接
  8. 重復以上流程

源碼

@Override public Response intercept(Chain chain) throws IOException {
  // 
  Request request = chain.request();
 // 1. 初始化一個socket連接對象
  streamAllocation = new StreamAllocation(
      client.connectionPool(), createAddress(request.url()), callStackTrace);

  int followUpCount = 0;
  Response priorResponse = null;
  while (true) {
     // 
    if (canceled) {
      streamAllocation.release();
      throw new IOException("Canceled");
    }

    Response response = null;
    boolean releaseConnection = true;
    try {
       // 2. 執行下一個攔截器,即BridgeInterceptor
      response = ((RealInterceptorChain) chain).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.
       // 3. 如果有異常,判斷是否要恢復
      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();
    }
 // 4. 檢查是否符合要求
    Request followUp = followUpRequest(response);

    if (followUp == null) {
      if (!forWebSocket) {
        streamAllocation.release();
      }
      // 返回結果
      return response;
    }
 // 5. 不符合,關閉響應流
    closeQuietly(response.body());
 // 6. 是否超過最大限制
    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());
    }
 // 7. 是否有相同的連接
    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?");
    }

    request = followUp;
    priorResponse = response;
  }
}

初始化連接對象

// 初始化一個Socket連接對象,此處是第一步,然后獲取輸入/輸出流
streamAllocation = new StreamAllocation(
    client.connectionPool(), createAddress(request.url()), callStackTrace);
// 三個參數分別對應,全局的連接池僅對http/2有用,連接線路Address, 堆棧對象(個人認為沒什么用)

注意:此處還沒有真正的去建立連接,只是初始化一個連接對象

繼續下一個攔截器

上面一步初始化好后,將繼續執行下一個連接器BridgeInterceptor,后文將繼續分析,此處暫略

// 這里有個很重的信息,即會將初始化好的連接對象傳遞給下一個攔截器,也是貫穿整個請求的連擊對象,
// 上文我們說過,在攔截器執行過程中,RealInterceptorChain的幾個屬性字段會一步一步賦值
response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);

拋出異常

如果拋出異常,將判斷是否能夠繼續連接,以下情況不在,重試:

  1. 應用層配置不在連接,默認為true

  2. 請求Request出錯不能繼續使用

  3. 是否可以恢復的

    3.1、協議錯誤(ProtocolException)
    3.2、中斷異常(InterruptedIOException)
    3.3、SSL握手錯誤(SSLHandshakeException && CertificateException)
    3.4、certificate pinning錯誤(SSLPeerUnverifiedException)

  4. 沒用更多線路可供選擇
    ?

/**
* 不在繼續連接的情況:
* 1. 應用層配置不在連接,默認為true
* 2. 請求Request出錯不能繼續使用
* 3. 是否可以恢復的
*   3.1、協議錯誤(ProtocolException)
    3.2、中斷異常(InterruptedIOException)
    3.3、SSL握手錯誤(SSLHandshakeException && CertificateException)
    3.4、certificate pinning錯誤(SSLPeerUnverifiedException)
* 4. 沒用更多線路可供選擇
*/
private boolean recover(IOException e, boolean requestSendStarted, Request userRequest) {
  streamAllocation.streamFailed(e);
  // 1. 應用層配置不在連接,默認為true
  // The application layer has forbidden retries.
  if (!client.retryOnConnectionFailure()) return false;

  // 2. 請求Request出錯不能繼續使用
  // We can't send the request body again.
  if (requestSendStarted && userRequest.body() instanceof UnrepeatableRequestBody) return false;

  //  是否可以恢復的
  // This exception is fatal.
  if (!isRecoverable(e, requestSendStarted)) return false;

  // 4. 沒用更多線路可供選擇
  // No more routes to attempt.
  if (!streamAllocation.hasMoreRoutes()) return false;

  // For failure recovery, use the same route selector with a new connection.
  return true;
}

正常響應

根據響應碼(code),處理響應頭(header),比如重定向,超時等如果一切正常將直接返回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()), callStackTrace);
} else if (streamAllocation.codec() != null) {
  throw new IllegalStateException("Closing the body of " + response
      + " didn't close its backing stream. Bad interceptor?");
}
// 略

其他

此攔截器主要的工作是:

  1. 初始化一個連接對象
  2. 處理異常,判斷是否需要繼續發起請求

總結

此攔截器是第一個攔截器,也是貫穿整個請求過程的攔截器,業務比較簡單,對照源碼幾本都能看懂

系列文章

  1. 關于Okhttp(一)-基本使用
  2. 關于Okhttp(二)-如何下載查看源碼
  3. 關于Okhttp3(三)-請求流程
  4. 關于Okhttp3(四)-RetryAndFollowUpInterceptor
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,923評論 18 139
  • 簡介 目前在HTTP協議請求庫中,OKHttp應當是非?;鸬?,使用也非常的簡單。網上有很多文章寫了關于OkHttp...
    第八區閱讀 1,397評論 1 5
  • 1.OkHttp源碼解析(一):OKHttp初階2 OkHttp源碼解析(二):OkHttp連接的"前戲"——HT...
    隔壁老李頭閱讀 12,032評論 31 62
  • 一.網絡通信概念理解 1.http協議概述 當我們在自己電腦的web瀏覽器地址欄敲入網址url,點擊enter,...
    銅雀春深鎖不住閱讀 4,978評論 0 3
  • 小荷才露尖尖角,早有蜻蜓立上頭。通過小學朗朗上口的詩句,我們就在遙想著荷花,一直都堅信初開的荷花一定是很美的,后來...
    洪甫閱讀 360評論 8 11