OkHttp源碼解讀總結(七)—>RetryAndFollowUpInterceptor攔截器
標籤(空格分隔): OkHttp源碼 學習筆記
前言
- 以下的相關知識總結是通過慕課網的相關學習和自己的相關看法,如果有需要的可以去查看一下慕課網的相關教學,感覺還可以。
主要作用
//官網介紹
This interceptor recovers from failures and follows redirects as necessary. It may throw an
* {@link IOException} if the call was canceled.
主要負責失敗重連,並不是所有的網絡請求都會去失敗重連的,它是有一個範圍的,在okhttp內部進行檢測網絡請求的異常和響應碼的判斷,如果都在限制範圍內,那麼就可以進行失敗重連。
intercept()方法源碼
@Override public Response intercept(Chain chain) throws IOException {
//獲取request對象
Request request = chain.request();
RealInterceptorChain realChain = (RealInterceptorChain) chain;
//獲取當前請求的call對象
Call call = realChain.call();
//監聽器
EventListener eventListener = realChain.eventListener();
//創建一個StreamAllocation對象 用來建立http請求的網絡組件 用於獲取服務端的conn和數據傳輸的輸入輸出流 在這裏並沒有用到
//// 三個參數分別對應,全局的連接池僅對http/2有用,連接線路Address, 堆棧對象
streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()),
call, eventListener, callStackTrace);
int followUpCount = 0;
Response priorResponse = null;
while (true) {
if (canceled) {
streamAllocation.release();
throw new IOException("Canceled");
}
Response response;
boolean releaseConnection = true;
try {
//執行下一個攔截器 即BridgeInterceptor
response = realChain.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.
//如果有異常 判斷是否需要恢復
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();
}
Request followUp = followUpRequest(response);
if (followUp == null) {
if (!forWebSocket) {
streamAllocation.release();
}
return response;
}
closeQuietly(response.body());
//對重試的次數進行判斷 MAX_FOLLOW_UPS = 20 次
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()), call, eventListener, 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;
}
}
- 1、創建StreamAllocation對象
- 2、調用RealInterceptorChainrealChain.proceed(request, streamAllocation, null, null); 進行網絡請求和調用下一個攔截器
- 3、根據異常和響應結果判斷是否重連pan
- 4、調用下一個攔截器,對response進行處理,返回給上一個攔截器
繼續下一個攔截器
將繼續執行下一個連接器BridgeInterceptor