關(guān)于Okhttp3(四)-RetryAndFollowUpInterceptor

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

官網(wǎng)解釋如下:

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

最大恢復(fù)重試次數(shù):

private static final int MAX_FOLLOW_UPS = 20;

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

處理的業(yè)務(wù)

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

源碼

@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. 執(zhí)行下一個攔截器,即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. 如果有異常,判斷是否要恢復(fù)
      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();
      }
      // 返回結(jié)果
      return response;
    }
 // 5. 不符合,關(guān)閉響應(yīng)流
    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);
// 三個參數(shù)分別對應(yīng),全局的連接池僅對http/2有用,連接線路Address, 堆棧對象(個人認(rèn)為沒什么用)

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

繼續(xù)下一個攔截器

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

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

拋出異常

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

  1. 應(yīng)用層配置不在連接,默認(rèn)為true

  2. 請求Request出錯不能繼續(xù)使用

  3. 是否可以恢復(fù)的

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

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

/**
* 不在繼續(xù)連接的情況:
* 1. 應(yīng)用層配置不在連接,默認(rèn)為true
* 2. 請求Request出錯不能繼續(xù)使用
* 3. 是否可以恢復(fù)的
*   3.1、協(xié)議錯誤(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. 應(yīng)用層配置不在連接,默認(rèn)為true
  // The application layer has forbidden retries.
  if (!client.retryOnConnectionFailure()) return false;

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

  //  是否可以恢復(fù)的
  // 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;
}

正常響應(yīng)

根據(jù)響應(yīng)碼(code),處理響應(yīng)頭(header),比如重定向,超時等如果一切正常將直接返回Response停止循環(huán)。

響應(yīng)不符合要求

如果響應(yīng)不符合要求,將關(guān)閉響應(yīng),接續(xù)處理

// 略
closeQuietly(response.body());
 // 超過最大限制,拋出異常停止循環(huán)
if (++followUpCount > MAX_FOLLOW_UPS) {
  streamAllocation.release();
  throw new ProtocolException("Too many follow-up requests: " + followUpCount);
}
// 請求已破壞掉,拋出異常停止循環(huán)
if (followUp.body() instanceof UnrepeatableRequestBody) {
  streamAllocation.release();
  throw new HttpRetryException("Cannot retry streamed HTTP body", response.code());
}
// 如果響應(yīng)線路,和請求相同,復(fù)用,否則,關(guān)閉重建響應(yīng)
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. 處理異常,判斷是否需要繼續(xù)發(fā)起請求

總結(jié)

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

系列文章

  1. 關(guān)于Okhttp(一)-基本使用
  2. 關(guān)于Okhttp(二)-如何下載查看源碼
  3. 關(guān)于Okhttp3(三)-請求流程
  4. 關(guān)于Okhttp3(四)-RetryAndFollowUpInterceptor
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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

友情鏈接更多精彩內(nèi)容