OkHttpClient源碼分析(二) —— RetryAndFollowUpInterceptor和BridgeInterceptor

OkHttp攔截器

??攔截器是OkHttp中提供的一種強大機制,它可以實現(xiàn)網絡監(jiān)聽、請求以及響應重寫、請求失敗重試等功能。

image

如上圖所示,這就OkHttp內部提供給我們的攔截器,就是當我們發(fā)起一個http請求的時候,OkHttp就會通過這個攔截器鏈來執(zhí)行http請求。其中包括:

  • RetryAndFollowUpInterceptor 重試和重定向攔截器
  • BridgeInterceptor :橋接和適配攔截器
  • CacheInterceptor :緩存攔截器
  • ConnectInterceptor :鏈接攔截器
  • CallServerInterceptor :請求和處理響應攔截器

BridgeInterceptorCacheInterceptor 主要是用來補充用戶請求創(chuàng)建當中缺少的一些必需的http請求頭和處理緩存的功能。

ConnectInterceptor 主要是負責建立可用的鏈接,CallServerInterceptor 主要是負責將http請求寫進網絡的IO流當中,并且從網絡IO流當中讀取服務端返回給客戶端的數(shù)據(jù)。

源碼分析

getResponseWithInterceptorChain()

上篇文章 OkHttpClient源碼分析(一)——同步、異步請求的執(zhí)行流程和源碼分析 有提及到一個很重要的方法getResponseWithInterceptorChain(),同步請求的話,是在RealCall類中的excute()方法中調用到該方法,而異步請求是在RealCall的內部類AsyncCal中的excute()方法中調用,查看該方法的源碼:

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));

    Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
        originalRequest, this, eventListener, client.connectTimeoutMillis(),
        client.readTimeoutMillis(), client.writeTimeoutMillis());

    return chain.proceed(originalRequest);
}

一、方法里初始化了一個Interceptor的集合,添加了OkHttpClient中配置的攔截器集合,然后依次添加了上述提及到的那五個攔截器;

二、創(chuàng)建一個攔截器鏈RealInterceptorChain,并執(zhí)行攔截器鏈的proceed()方法;

查看RealInterceptorChain類的proceed()方法:

 public Response proceed(Request request, StreamAllocation streamAllocation, HttpStream httpStream,
      Connection connection) throws IOException {
      
      ...
      
      // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(
        interceptors, streamAllocation, httpStream, connection, index + 1, request);
    Interceptor interceptor = interceptors.get(index);
    Response response = interceptor.intercept(next);
    
    ...
 }

其中,核心的代碼就在這里,這里再次創(chuàng)建了RealInterceptorChain對象,此時創(chuàng)建的是下一個攔截器鏈,傳入的是index + 1,并通過調用當前Interceptor的intercept()方法,將下一個攔截器鏈傳入,得到Response對象,至于攔截器的intercept()方法,下面將會分析。

RetryAndFollowUpInterceptor

主要作用是負責網絡請求失敗重連,需要注意的是,并不是所有的網絡請求失敗以后都可以進行重連,它是有一定的限制范圍的,OkHttp內部會幫我們檢測網絡異常和響應碼的判斷,如果都在它的限制范圍內的話,就會進行網絡重連。

源碼主要看intercept()方法:

@Override public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();

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

    int followUpCount = 0;
    Response priorResponse = null;
    ...
}

這里創(chuàng)建了一個StreamAllocation對象,StreamAllocation對象是用來建立執(zhí)行Http請求所需要的網絡組件的,從它名字可以看出,它是用來分配stream的,主要是用于獲取連接服務端的connection和用于與服務端進行數(shù)據(jù)傳輸?shù)妮斎胼敵隽?。

詳細的邏輯都在intercept()方法中的while循環(huán)中,這里不做詳細介紹,主要是介紹其中的這個:

 while (true) {
    ...
    
    try {
        response = ((RealInterceptorChain) chain).proceed(request, streamAllocation, null, null);
        releaseConnection = false;
      }
      
      ...
    
    if (++followUpCount > MAX_FOLLOW_UPS) {
        streamAllocation.release();
        throw new ProtocolException("Too many follow-up requests: " + followUpCount);
    }
    
    ...
 }

這里我們可以發(fā)現(xiàn),RealInterceptorChain調用proceed()方法,方法里又創(chuàng)建了一個RealInterceptorChain對象(下一個攔截器鏈 index + 1),然后通過index獲取到當前執(zhí)行到的攔截器,調用攔截器的intercept()方法,這里intercept()方法中,再次調用了RealInterceptorChain的proceed()方法,形成了遞歸。

以上代碼是對重試的次數(shù)進行判斷,由此可知,并不是無限次的進行網絡重試,而是有一定的重試次數(shù)的,MAX_FOLLOW_UPS 是一個常量,值為20,也就是說最多進行20次重試,如果還不成功的話,就會釋放StreamAllocation對象和拋出ProtocolException異常。

總結:

  1. 創(chuàng)建StreamAllocation對象
  2. 調用RealInterceptorChain.proceed()進行網絡請求
  3. 根據(jù)異常結果或響應結果判斷是否要進行重新請求
  4. 調用下一個攔截器,對response進行處理,返回給上一個攔截器

BridgeInterceptor

同樣也是看核心方法intercept():

@Override public Response intercept(Chain chain) throws IOException {
    Request userRequest = chain.request();
    Request.Builder requestBuilder = userRequest.newBuilder();

    ...

    if (userRequest.header("Host") == null) {
      requestBuilder.header("Host", hostHeader(userRequest.url(), false));
    }

    if (userRequest.header("Connection") == null) {
      requestBuilder.header("Connection", "Keep-Alive");
    }

    ...
  
    Response networkResponse = chain.proceed(requestBuilder.build());

    ...

    if (transparentGzip
        && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
        && HttpHeaders.hasBody(networkResponse)) {
      GzipSource responseBody = new GzipSource(networkResponse.body().source());
      Headers strippedHeaders = networkResponse.headers().newBuilder()
          .removeAll("Content-Encoding")
          .removeAll("Content-Length")
          .build();
      responseBuilder.headers(strippedHeaders);
      String contentType = networkResponse.header("Content-Type");
      responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
    }

    return responseBuilder.build();
  }

這里并沒有貼上整個方法的代碼,省略了部分,主要的操作就是為發(fā)起的Request請求添加請求頭信息,其中同樣也調用了proceed()方法遞歸調用下個攔截器,最后面是針對經過gzip壓縮過的Response進行解壓處理,這里通過判斷是否支持gzip壓縮且請求頭里面的"Content-Encoding"的value是否是"gzip"來判斷是否需要進行gzip解壓。

總結:

  1. 負責將用戶構建的Request請求轉化為能夠進行網絡訪問的請求;
  2. 將這個符合網絡請求的Request進行網絡請求;
  3. 將網絡請求回來的的相應Response轉化為用戶可用的Response

下一篇將為大家介紹OkHttp的緩存機制,感興趣的朋友可以繼續(xù)閱讀:

OkHttpClient源碼分析(三)—— 緩存機制介紹

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容