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