基於OkHttp:3.12.0:github.com/square/okht…git
Read The Fucking Source Codegithub
OkHttpClient#newCall(new Request())#enquenue(new Callback(){})
複製代碼
OkHttpClient#newCall(new Request())
:OkHttpClient根據client、request建立RealCall,RealCall裏面包含全部請求的各類配置信息:攔截器、請求鏈接、調度器Dispatcher、ConnectionPool等//OkHttpClient
@Override public Call newCall(Request request) {
return RealCall.newRealCall(this, request, false /* for web socket */);
}
複製代碼
RealCall#enqueue(new Callback(){})
:
//RealCall
@Override public void enqueue(Callback responseCallback) {
//其它信息...
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
複製代碼
// Dispatcher
void enqueue(AsyncCall call) {
synchronized (this) {
readyAsyncCalls.add(call);
}
promoteAndExecute();
}
複製代碼
//Dispatcher
private boolean promoteAndExecute() {
//...
List<AsyncCall> executableCalls = new ArrayList<>();
//僞代碼...
if(知足必定條件){executableCalls.addAll(readyAsyncCalls)}
for (int i = 0, size = executableCalls.size(); i < size; i++) {
AsyncCall asyncCall = executableCalls.get(i);
asyncCall.executeOn(executorService());
}
return isRunning;
}
複製代碼
AsyncCall#executeOn(executorService())
:web
//AsyncCall
void executeOn(ExecutorService executorService) {
//...
executorService.execute(this);//這裏會調用父類的run方法
//...
}
複製代碼
//NamedRunnable, AsyncCall的父類
public abstract class NamedRunnable implements Runnable {
//...
@Override public final void run() {
//...
execute();
}
//子類AsyncCall作對應實現
protected abstract void execute();
}
複製代碼
#AsyncCall
@Override protected void execute() {
boolean signalledCallback = false;
timeout.enter();
try {
//關鍵,去獲取response對象,真正的耗時也就在這
Response response = getResponseWithInterceptorChain();
if (retryAndFollowUpInterceptor.isCanceled()) {
signalledCallback = true;
//失敗,請求被取消
responseCallback.onFailure(RealCall.this, new IOException("Canceled"));
} else {
signalledCallback = true;
//成功請求,拿去結果回調
responseCallback.onResponse(RealCall.this, response);
}
} catch (IOException e) {
e = timeoutExit(e);
if (signalledCallback) {
// Do not signal the callback twice!
Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
} else {
eventListener.callFailed(RealCall.this, e);
//失敗,回調
responseCallback.onFailure(RealCall.this, e);
}
} finally {
//完整結束當前請求,作一些收尾操做
client.dispatcher().finished(this);
}
}
}
複製代碼
RealCall#getResponseWithInterceptorChain
:
chain.proceed(requestBuilder.build())
,注意Chain是上一個建立的RealInterceptorChain,因此又跑到了第二步,同時index這個時候已經+1,如此完成遞歸操做!#RealCall
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);
}
複製代碼
#RealInterceptorChain
@Override public Response proceed(Request request) throws IOException {
return proceed(request, streamAllocation, httpCodec, connection);
}
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
//...
//注意這裏又構造了一個RealInterceptorChain,重點index+1
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
//取當前index對應的攔截器
Interceptor interceptor = interceptors.get(index);
//執行取到的攔截器
Response response = interceptor.intercept(next);
//...
return response;
}
複製代碼
# BridgeInterceptor
@Override public Response intercept(Chain chain) throws IOException {
//根據上游chain繼續處理當前攔截器。注意:chain的源頭:RealInterceptorChain#proceed中建立的next,一個新的RealInterceptorChain
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
//各類邏輯,跳過只看核心...
//核心,這裏又回到了RealInterceptorChain#proceed
Response networkResponse = chain.proceed(requestBuilder.build());
//後續邏輯...
return responseBuilder.build();
}
複製代碼