前文講解了整體流程,今天進入第一個攔截器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() 方法就是子類用來處理,自己的業務邏輯,所以我們只要分析此方法即可。
處理的業務
- 實例化StreamAllocation,初始化一個Socket連接對象,獲取到輸入/輸出流()基于Okio
- 開啟循環,執行下一個調用鏈(攔截器),等待返回結果(Response)
- 如果發生錯誤,判斷是否繼續請求,否:退出
- 檢查響應是否符合要求,是:返回
- 關閉響應結果
- 判斷是否達到最大限制數,是:退出
- 檢查是否有相同連接,是:釋放,重建連接
- 重復以上流程
源碼
@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);
拋出異常
如果拋出異常,將判斷是否能夠繼續連接,以下情況不在,重試:
應用層配置不在連接,默認為true
請求Request出錯不能繼續使用
-
是否可以恢復的
3.1、協議錯誤(ProtocolException)
3.2、中斷異常(InterruptedIOException)
3.3、SSL握手錯誤(SSLHandshakeException && CertificateException)
3.4、certificate pinning錯誤(SSLPeerUnverifiedException) 沒用更多線路可供選擇
?
/**
* 不在繼續連接的情況:
* 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?");
}
// 略
其他
此攔截器主要的工作是:
- 初始化一個連接對象
- 處理異常,判斷是否需要繼續發起請求
總結
此攔截器是第一個攔截器,也是貫穿整個請求過程的攔截器,業務比較簡單,對照源碼幾本都能看懂