okhttp的流程圖
1.對okhttpClient做初始化
2. 創建新的Call對象,
Call call = client.newCall(request);
public class OkHttpClient implements Cloneable, Call.Factory, WebSocket.Factory {
@Override
public Call newCall(Request request) {
return new RealCall(this, request, false /* for web socket */);
}
}
RealCall實現了Call.Factory接口創建了一個RealCall的實例
final class RealCall implements Call {
@Override
public void enqueue(Callback responseCallback) {
synchronized (this) {
if (executed) throw new IllegalStateException("Already Executed");
executed = true;
}
captureCallStackTrace();
client.dispatcher().enqueue(new AsyncCall(responseCallback));
}
}
1) 檢查這個 call 是否已經被執行了,每個 call 只能被執行一次,如果想要一個完全一樣的 call,可以利用 call#clone 方法進行克隆。
2)利用 client.dispatcher().enqueue(this) 來進行實際執行
3)AsyncCall是RealCall的子類
final class AsyncCall extends NamedRunnable {
private final Callback responseCallback;
AsyncCall(Callback responseCallback) {
super("OkHttp %s", redactedUrl());
this.responseCallback = responseCallback;
}
@Override protected void execute() {
boolean signalledCallback = false;
try {
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) {
......
responseCallback.onFailure(RealCall.this, e);
} finally {
client.dispatcher().finished(this);
}
}
AsyncCall繼承了NamedRunnable實現了execute方法,首先是調用getResponseWithInterceptorChain()方法獲取響應,然後獲取成功後,就調用回調的onReponse方法,如果失敗,就調用回調的onFailure方法。最後,調用Dispatcher的finished方法。
public abstract class NamedRunnable implements Runnable {
......
@Override
public final void run() {
......
try {
execute();
}
......
}
protected abstract void execute();
}
可以看到NamedRunnable實現了Runnbale接口並且是個抽象類,其抽象方法是execute(),該方法是在run方法中被調用的,這也就意味着NamedRunnable是一個任務,並且其子類AsyncCall實現了execute方法
Dispatcher(調度器)介紹
public synchronized ExecutorService executorService() {
if (executorService == null) {
executorService = new ThreadPoolExecutor(
//corePoolSize 最小併發線程數,如果是0的話,空閒一段時間後所有線程將全部被銷燬
0,
//maximumPoolSize: 最大線程數,當任務進來時可以擴充的線程最大值,當大於了這個值就會根據丟棄處理機制來處理
Integer.MAX_VALUE,
//keepAliveTime: 當線程數大於corePoolSize時,多餘的空閒線程的最大存活時間
60,
//單位秒
TimeUnit.SECONDS,
//工作隊列,先進先出
new SynchronousQueue<Runnable>(),
//單個線程的工廠
Util.threadFactory("OkHttp Dispatcher", false));
}
return executorService;
}
OkHttp,如上構造了單例線程池ExecutorService:
在Okhttp中,構建了一個核心爲[0, Integer.MAX_VALUE]的線程池,它不保留任何最小線程數,隨時創建更多的線程數,當線程空閒時只能活60秒,它使用了一個不存儲元素的阻塞工作隊列,一個叫做"OkHttp Dispatcher"的線程工廠
synchronized void enqueue(AsyncCall call) {
if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
runningAsyncCalls.add(call);
executorService().execute(call);
} else {
readyAsyncCalls.add(call);
}
}
realCall中的調用看出當前還能執行併發請求,則加入 runningAsyncCalls ,立即執行,否則加入 readyAsyncCalls 隊列。
針對同步請求,Dispatcher使用了一個Deque保存了同步任務;針對異步請求,Dispatcher使用了兩個Deque,一個保存準備執行的請求,一個保存正在執行的請求
1.運行隊列中立即異步執行
2.先進先出的順序緩存隊列
//Dispatcher的finished函數
void finished(AsyncCall call) {
finished(runningAsyncCalls, call, true);
}
private <T> void finished(Deque<T> calls, T call, boolean promoteCalls) {
int runningCallsCount;
Runnable idleCallback;
synchronized (this) {
if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!");
if (promoteCalls) promoteCalls();
runningCallsCount = runningCallsCount();
idleCallback = this.idleCallback;
}
if (runningCallsCount == 0 && idleCallback != null) {
idleCallback.run();
}
}
//打開源碼,發現它將正在運行的任務Call從隊列runningAsyncCalls中移除後,獲取運行數量判斷是否進入了Idle狀態,接着執行promoteCalls()函數,下面是promoteCalls()方法
private void promoteCalls() {
if (runningAsyncCalls.size() >= maxRequests) return; // Already running max capacity.
if (readyAsyncCalls.isEmpty()) return; // No ready calls to promote.
for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
AsyncCall call = i.next();
if (runningCallsForHost(call) < maxRequestsPerHost) {
i.remove();
runningAsyncCalls.add(call);
executorService().execute(call);
}
if (runningAsyncCalls.size() >= maxRequests) return; // Reached max capacity.
}
}
攔截器鏈
RealCall的execute方法有這麼一段代碼:Response result = getResponseWithInterceptorChain();
流程圖:先是自定義攔截器
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、retryAndFollowUpInterceptor、BridgeInterceptor、CacheInterceptor、ConnectInterceptor、 networkInterceptors、CallServerInterceptor,並將這些攔截器傳遞給了這個RealInterceptorChain。攔截器之所以可以依次調用,並最終再從後先前返回Response,都依賴於RealInterceptorChain的proceed方法
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec,
RealConnection connection) throws IOException {
if (index >= interceptors.size()) throw new AssertionError();
......
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
......
return response;
執行當前攔截器的Intercept方法,並調用下一個(index+1)攔截器。下一個(index+1)攔截器的調用依賴於當前攔截器的Intercept方法中,對RealInterceptorChain的proceed方法的調用
重試及 followup攔截器
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();//獲取Request對象
RealInterceptorChain realChain = (RealInterceptorChain) chain;//獲取攔截器鏈對象,用於後面的chain.proceed(...)方法
Call call = realChain.call();
EventListener eventListener = realChain.eventListener();//監聽器
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 {
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 = response.newBuilder()
.priorResponse(priorResponse.newBuilder()
.body(null)
.build())
.build();
}
//Figures out the HTTP request to make in response to receiving {@code userResponse}. This will
//either add authentication headers, follow redirects or handle a client request timeout. If a
//follow-up is either unnecessary or not applicable, this returns null.
// followUpRequest方法的主要作用就是爲新的重試Request添加驗證頭等內容
Request followUp = followUpRequest(response);
if (followUp == null) {//如果一個請求得到的響應code是200,則followUp是爲null的。
if (!forWebSocket) { streamAllocation.release(); } return 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()), 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;//得到處理之後的Request,以用來繼續請求,在哪繼續請求?肯定還是沿着攔截器鏈繼續搞唄
priorResponse = response;//由priorResponse持有
}
}
}
該攔截器主要的作用就是重試及followup
當一個請求由於各種原因失敗了,如果是路由或者連接異常,則嘗試恢復,否則,根據響應碼(ResponseCode),followup方法會對Request進行再處理以得到新的Request,然後沿着攔截器鏈繼續新的Request。當然,如果responseCode是200的話,這些過程就結束了
BridgeInterceptor
BridgeInterceptor的主要作用就是爲請求(request before)添加請求頭,爲響應(Response Before)添加響應頭。看源碼:
@Override public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
//----------------------request----------------------------------------------
RequestBody body = userRequest.body();
if (body != null) {
MediaType contentType = body.contentType();
if (contentType != null) {//添加Content-Type請求頭
requestBuilder.header("Content-Type", contentType.toString());
}
long contentLength = body.contentLength();
if (contentLength != -1) {
requestBuilder.header("Content-Length", Long.toString(contentLength));
requestBuilder.removeHeader("Transfer-Encoding");
} else {
requestBuilder.header("Transfer-Encoding", "chunked");//分塊傳輸
requestBuilder.removeHeader("Content-Length");
}
}
if (userRequest.header("Host") == null) {
requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}
if (userRequest.header("Connection") == null) {
requestBuilder.header("Connection", "Keep-Alive");
}
// If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
// the transfer stream.
boolean transparentGzip = false;
if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
transparentGzip = true;
requestBuilder.header("Accept-Encoding", "gzip");
}
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
if (!cookies.isEmpty()) {
requestBuilder.header("Cookie", cookieHeader(cookies));
}
if (userRequest.header("User-Agent") == null) {
requestBuilder.header("User-Agent", Version.userAgent());
}
Response networkResponse = chain.proceed(requestBuilder.build());
//----------------------------------response----------------------------------------------
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());//保存cookie
Response.Builder responseBuilder = networkResponse.newBuilder()
.request(userRequest);
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")//Content-Encoding、Content-Length不能用於Gzip解壓縮
.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();
}
CacheInterceptor
服務器收到請求時,會在200 OK中回送該資源的Last-Modified和ETag頭(服務器支持緩存的情況下纔會有這兩個頭哦),客戶端將該資源保存在cache中,並記錄這兩個屬性。當客戶端需要發送相同的請求時,根據Date + Cache-control來判斷是否緩存過期,如果過期了,會在請求中攜帶If-Modified-Since和If-None-Match兩個頭。兩個頭的值分別是響應中Last-Modified和ETag頭的值。服務器通過這兩個頭判斷本地資源未發生變化,客戶端不需要重新下載,返回304響應
CacheStrategy是一個緩存策略類,該類告訴CacheInterceptor是使用緩存還是使用網絡請求;
Cache是封裝了實際的緩存操作;
DiskLruCache:Cache基於DiskLruCache;
@Override public Response intercept(Chain chain) throws IOException {
Response cacheCandidate = cache != null
? cache.get(chain.request())//以request的url而來key,獲取緩存
: null;
long now = System.currentTimeMillis();
//緩存策略類,該類決定了是使用緩存還是進行網絡請求
CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
Request networkRequest = strategy.networkRequest;//網絡請求,如果爲null就代表不用進行網絡請求
Response cacheResponse = strategy.cacheResponse;//緩存響應,如果爲null,則代表不使用緩存
if (cache != null) {//根據緩存策略,更新統計指標:請求次數、使用網絡請求次數、使用緩存次數
cache.trackResponse(strategy);
}
//緩存不可用,關閉
if (cacheCandidate != null && cacheResponse == null) {
closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
}
//如果既無網絡請求可用,又沒有緩存,則返回504錯誤
// If we're forbidden from using the network and the cache is insufficient, fail.
if (networkRequest == null && cacheResponse == null) {
return new Response.Builder()
.request(chain.request())
.protocol(Protocol.HTTP_1_1)
.code(504)
.message("Unsatisfiable Request (only-if-cached)")
.body(Util.EMPTY_RESPONSE)
.sentRequestAtMillis(-1L)
.receivedResponseAtMillis(System.currentTimeMillis())
.build();
}
// If we don't need the network, we're done.緩存可用,直接返回緩存
if (networkRequest == null) {
return cacheResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.build();
}
Response networkResponse = null;
try {
networkResponse = chain.proceed(networkRequest);//進行網絡請求,得到網絡響應
} finally {
// If we're crashing on I/O or otherwise, don't leak the cache body.
if (networkResponse == null && cacheCandidate != null) {
closeQuietly(cacheCandidate.body());
}
}
//HTTP_NOT_MODIFIED緩存有效,合併網絡請求和緩存
// If we have a cache response too, then we're doing a conditional get.
if (cacheResponse != null) {
if (networkResponse.code() == HTTP_NOT_MODIFIED) {
Response response = cacheResponse.newBuilder()
.headers(combine(cacheResponse.headers(), networkResponse.headers()))
.sentRequestAtMillis(networkResponse.sentRequestAtMillis())
.receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
networkResponse.body().close();
// Update the cache after combining headers but before stripping the
// Content-Encoding header (as performed by initContentStream()).
cache.trackConditionalCacheHit();
cache.update(cacheResponse, response);//更新緩存
return response;
} else {
closeQuietly(cacheResponse.body());
}
}
Response response = networkResponse.newBuilder()
.cacheResponse(stripBody(cacheResponse))
.networkResponse(stripBody(networkResponse))
.build();
if (cache != null) {
//有響應體 & 可緩存
if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
// Offer this request to the cache.
CacheRequest cacheRequest = cache.put(response);
return cacheWritingResponse(cacheRequest, response);//寫緩存
}
if (HttpMethod.invalidatesCache(networkRequest.method())) {//判斷緩存的有效性
try {
cache.remove(networkRequest);
} catch (IOException ignored) {
// The cache cannot be written.
}
}
}
return response;
}
根據緩存策略類返回的結果:
1、如果網絡不可用並且無可用的有效緩存,則返回504錯誤;
2、繼續,如果不需要網絡請求,則直接使用緩存;
3、繼續,如果需要網絡可用,則進行網絡請求;
4、繼續,如果有緩存,並且網絡請求返回HTTP_NOT_MODIFIED,說明緩存還是有效的,則合併網絡響應和緩存結果。同時更新緩存;
5、繼續,如果沒有緩存,則寫入新的緩存;
我們可以看到,CacheStrategy在CacheInterceptor中起到了很關鍵的作用。該類決定了是網絡請求還是使用緩存。該類最關鍵的代碼是getCandidate()方法:
private CacheStrategy getCandidate() {
// No cached response.
if (cacheResponse == null) {//沒有緩存,直接網絡請求
return new CacheStrategy(request, null);
}
// Drop the cached response if it's missing a required handshake.
if (request.isHttps() && cacheResponse.handshake() == null) {//https,但沒有握手,直接網絡請求
return new CacheStrategy(request, null);
}
// If this response shouldn't have been stored, it should never be used
// as a response source. This check should be redundant as long as the
// persistence store is well-behaved and the rules are constant.
if (!isCacheable(cacheResponse, request)) {//不可緩存,直接網絡請求
return new CacheStrategy(request, null);
}
CacheControl requestCaching = request.cacheControl();
if (requestCaching.noCache() || hasConditions(request)) {
//請求頭nocache或者請求頭包含If-Modified-Since或者If-None-Match
//請求頭包含If-Modified-Since或者If-None-Match意味着本地緩存過期,需要服務器驗證
//本地緩存是不是還能繼續使用
return new CacheStrategy(request, null);
}
CacheControl responseCaching = cacheResponse.cacheControl();
if (responseCaching.immutable()) {//強制使用緩存
return new CacheStrategy(null, cacheResponse);
}
long ageMillis = cacheResponseAge();
long freshMillis = computeFreshnessLifetime();
if (requestCaching.maxAgeSeconds() != -1) {
freshMillis = Math.min(freshMillis, SECONDS.toMillis(requestCaching.maxAgeSeconds()));
}
long minFreshMillis = 0;
if (requestCaching.minFreshSeconds() != -1) {
minFreshMillis = SECONDS.toMillis(requestCaching.minFreshSeconds());
}
long maxStaleMillis = 0;
if (!responseCaching.mustRevalidate() && requestCaching.maxStaleSeconds() != -1) {
maxStaleMillis = SECONDS.toMillis(requestCaching.maxStaleSeconds());
}
//可緩存,並且ageMillis + minFreshMillis < freshMillis + maxStaleMillis
// (意味着雖過期,但可用,只是會在響應頭添加warning)
if (!responseCaching.noCache() && ageMillis + minFreshMillis < freshMillis + maxStaleMillis) {
Response.Builder builder = cacheResponse.newBuilder();
if (ageMillis + minFreshMillis >= freshMillis) {
builder.addHeader("Warning", "110 HttpURLConnection \"Response is stale\"");
}
long oneDayMillis = 24 * 60 * 60 * 1000L;
if (ageMillis > oneDayMillis && isFreshnessLifetimeHeuristic()) {
builder.addHeader("Warning", "113 HttpURLConnection \"Heuristic expiration\"");
}
return new CacheStrategy(null, builder.build());//使用緩存
}
// Find a condition to add to the request. If the condition is satisfied, the response body
// will not be transmitted.
String conditionName;
String conditionValue;
//流程走到這,說明緩存已經過期了
//添加請求頭:If-Modified-Since或者If-None-Match
//etag與If-None-Match配合使用
//lastModified與If-Modified-Since配合使用
//前者和後者的值是相同的
//區別在於前者是響應頭,後者是請求頭。
//後者用於服務器進行資源比對,看看是資源是否改變了。
// 如果沒有,則本地的資源雖過期還是可以用的
if (etag != null) {
conditionName = "If-None-Match";
conditionValue = etag;
} else if (lastModified != null) {
conditionName = "If-Modified-Since";
conditionValue = lastModifiedString;
} else if (servedDate != null) {
conditionName = "If-Modified-Since";
conditionValue = servedDateString;
} else {
return new CacheStrategy(request, null); // No condition! Make a regular request.
}
Headers.Builder conditionalRequestHeaders = request.headers().newBuilder();
Internal.instance.addLenient(conditionalRequestHeaders, conditionName, conditionValue);
Request conditionalRequest = request.newBuilder()
.headers(conditionalRequestHeaders.build())
.build();
return new CacheStrategy(conditionalRequest, cacheResponse);
}
大致流程如下:(if-else的關係呀)
1、沒有緩存,直接網絡請求;
2、如果是https,但沒有握手,直接網絡請求;
3、不可緩存,直接網絡請求;
4、請求頭nocache或者請求頭包含If-Modified-Since或者If-None-Match,則需要服務器驗證本地緩存是不是還能繼續使用,直接網絡請求;
5、可緩存,並且ageMillis + minFreshMillis < freshMillis + maxStaleMillis(意味着雖過期,但可用,只是會在響應頭添加warning),則使用緩存;
6、緩存已經過期,添加請求頭:If-Modified-Since或者If-None-Match,進行網絡請求;
ConnectInterceptor(核心,連接池)
未完的後面再補充