在日常開(kāi)發(fā)中網(wǎng)絡(luò)請(qǐng)求是很常見(jiàn)的功能。OkHttp作為Android開(kāi)發(fā)中最常用的網(wǎng)絡(luò)請(qǐng)求框架,在A(yíng)ndroid開(kāi)發(fā)中我們經(jīng)常結(jié)合retrofit一起使用,俗話(huà)說(shuō)得好:“知其然知其所以然”,所以這篇文章我們通過(guò)源碼來(lái)深入理解OKHttp3(基于3.12版本)
-
常規(guī)使用
- 在了解源碼前,我們先了解如何使用OKHttp這個(gè)框架(框架地址)
//框架引入項(xiàng)目 implementation("com.squareup.okhttp3:okhttp:3.12.0") //引用官方Demo的例子 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //主線(xiàn)程不能進(jìn)行耗時(shí)操作 new Thread(){ @Override public void run() { super.run(); /** * 同步請(qǐng)求 */ GetExample getexample = new GetExample(); String syncresponse = null; try { syncresponse = getexample.run("https://raw.github.com/square/okhttp/master/README.md"); Log.i("maoqitian","異步請(qǐng)求返回參數(shù)"+syncresponse); } catch (IOException e) { e.printStackTrace(); } } }.start(); /** * 異步請(qǐng)求 */ PostExample postexample = new PostExample(); String json = postexample.bowlingJson("Jesse", "Jake"); try { postexample.post("http://www.roundsapp.com/post", json); } catch (IOException e) { e.printStackTrace(); } } /** * 異步請(qǐng)求 */ class PostExample { final MediaType JSON = MediaType.get("application/json; charset=utf-8"); //獲取 OkHttpClient 對(duì)象 OkHttpClient client = new OkHttpClient(); void post(String url, String json) throws IOException { RequestBody body = RequestBody.create(JSON, json); Request request = new Request.Builder() .url(url) .post(body) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.i("maoqitian","異步請(qǐng)求返回參數(shù)"+e.toString()); } @Override public void onResponse(Call call, Response response) throws IOException { String asynresponse= response.body().string(); Log.i("maoqitian","異步請(qǐng)求返回參數(shù)"+asynresponse); } }); } String bowlingJson(String player1, String player2) { return "{'winCondition':'HIGH_SCORE'," + "'name':'Bowling'," + "'round':4," + "'lastSaved':1367702411696," + "'dateStarted':1367702378785," + "'players':[" + "{'name':'" + player1 + "','history':[10,8,6,7,8],'color':-13388315,'total':39}," + "{'name':'" + player2 + "','history':[6,10,5,10,10],'color':-48060,'total':41}" + "]}"; } } /** * 同步請(qǐng)求 */ class GetExample { OkHttpClient client = new OkHttpClient(); String run(String url) throws IOException { Request request = new Request.Builder() .url(url) .build(); try (Response response = client.newCall(request).execute()) { return response.body().string(); } } }
- 由例子我們可以看到,client.newCall(request).execute()執(zhí)行的是異步請(qǐng)求,我們可以加入Callback來(lái)異步獲取返回值,Response response = client.newCall(request).execute()執(zhí)行的是同步請(qǐng)求,更多post請(qǐng)求方式例子可以查看官方sample項(xiàng)目
-
源碼分析
-
首先看一個(gè)流程圖,對(duì)于接下來(lái)的源碼分析有個(gè)大體印象
OKHttp網(wǎng)絡(luò)請(qǐng)求流程圖.png -
通過(guò)上面的例子可以看到,不管是同步請(qǐng)求還是異步請(qǐng)求,首先調(diào)用的OkHttpClient的newCall(request)方法,先來(lái)看看這個(gè)方法
/** * Prepares the {@code request} to be executed at some point in the future. */ @Override public Call newCall(Request request) { return RealCall.newRealCall(this, request, false /* for web socket */); }
通過(guò)newCall方法的源碼可以看到該方法返回值是Call,Call是一個(gè)接口,他的實(shí)現(xiàn)類(lèi)是RealCall,所以我們執(zhí)行的同步execute()方法或者異步enqueue()方法都是RealCall的方法。newCall方法接收了的網(wǎng)絡(luò)請(qǐng)求參數(shù),接下來(lái)我們看看execute()和enqueue()方法
/** * 同步請(qǐng)求 */ @Override public Response execute() throws IOException { synchronized (this) { if (executed) throw new IllegalStateException("Already Executed"); executed = true; } captureCallStackTrace(); timeout.enter(); eventListener.callStart(this); try { client.dispatcher().executed(this); Response result = getResponseWithInterceptorChain(); if (result == null) throw new IOException("Canceled"); return result; } catch (IOException e) { e = timeoutExit(e); eventListener.callFailed(this, e); throw e; } finally { client.dispatcher().finished(this); } } /** * 異步請(qǐng)求 */ @Override public void enqueue(Callback responseCallback) { synchronized (this) { if (executed) throw new IllegalStateException("Already Executed"); executed = true; } captureCallStackTrace(); eventListener.callStart(this); client.dispatcher().enqueue(new AsyncCall(responseCallback)); }
- 這里先看異步的enqueue方法,很直觀(guān)可以看到真正執(zhí)行網(wǎng)絡(luò)請(qǐng)求的是最后一句代碼,而它是怎么做的呢,我們還得先弄明白dispatcher,Dispatcher的本質(zhì)是異步請(qǐng)求的調(diào)度器,它內(nèi)部持有一個(gè)線(xiàn)程池,結(jié)合線(xiàn)程池調(diào)配并發(fā)請(qǐng)求。官方文檔描述也說(shuō)了這一點(diǎn)。
OKhttp Dispatcher文檔描述.png
/**最大并發(fā)請(qǐng)求數(shù)*/ private int maxRequests = 64; /**每個(gè)主機(jī)最大請(qǐng)求數(shù)*/ private int maxRequestsPerHost = 5; /** Ready async calls in the order they'll be run. 準(zhǔn)備要執(zhí)行的異步請(qǐng)求隊(duì)列*/ private final Deque<AsyncCall> readyAsyncCalls = new ArrayDeque<>(); /** Running asynchronous calls. Includes canceled calls that haven't finished yet. 正在執(zhí)行的異步請(qǐng)求隊(duì)列*/ private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>(); /** Running synchronous calls. Includes canceled calls that haven't finished yet. 正在執(zhí)行的同步請(qǐng)求隊(duì)列*/ private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>(); /** Dispatcher 構(gòu)造方法 */ public Dispatcher(ExecutorService executorService) { this.executorService = executorService; } public Dispatcher() { } public synchronized ExecutorService executorService() { if (executorService == null) { executorService = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp Dispatcher", false)); } return executorService; }
- 通過(guò)Dispatcher的構(gòu)造方法我們知道我們可以使用自己的線(xiàn)程池,也可以使用Dispatcher默認(rèn)的線(xiàn)程池,默認(rèn)的線(xiàn)程池相當(dāng)于CachedThreadPool線(xiàn)程池,這個(gè)線(xiàn)程池比較適合執(zhí)行大量的耗時(shí)較少的任務(wù)(線(xiàn)程池介紹)。
- 了解了Dispatcher之后,我們繼續(xù)探究Dispatcher的enqueue方法
void enqueue(AsyncCall call) { synchronized (this) { readyAsyncCalls.add(call); } promoteAndExecute(); } /** * Promotes eligible calls from {@link #readyAsyncCalls} to {@link #runningAsyncCalls} and runs * them on the executor service. Must not be called with synchronization because executing calls * can call into user code. * * @return true if the dispatcher is currently running calls. */ private boolean promoteAndExecute() { assert (!Thread.holdsLock(this)); List<AsyncCall> executableCalls = new ArrayList<>(); boolean isRunning; synchronized (this) { for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) { AsyncCall asyncCall = i.next(); if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity. if (runningCallsForHost(asyncCall) >= maxRequestsPerHost) continue; // Host max capacity. i.remove(); executableCalls.add(asyncCall); runningAsyncCalls.add(asyncCall); } isRunning = runningCallsCount() > 0; } for (int i = 0, size = executableCalls.size(); i < size; i++) { AsyncCall asyncCall = executableCalls.get(i); asyncCall.executeOn(executorService()); } return isRunning; }
- 這里分三步走,首先將傳入的AsyncCall(這其實(shí)是一個(gè)Runnable對(duì)象)加入準(zhǔn)備要執(zhí)行的異步請(qǐng)求隊(duì)列,其次調(diào)用promoteAndExecute()方法,變量準(zhǔn)備要執(zhí)行的異步請(qǐng)求隊(duì)列,如果隊(duì)列任務(wù)數(shù)超過(guò)最大并發(fā)請(qǐng)求數(shù),則直接退出遍歷,則不會(huì)進(jìn)行下面的操作;如果超過(guò)每個(gè)主機(jī)最大請(qǐng)求數(shù),則跳過(guò)這次循環(huán),繼續(xù)下一次遍歷,否則將異步任務(wù)加入到正在執(zhí)行的異步請(qǐng)求隊(duì)列,最后遍歷保存異步任務(wù)的隊(duì)列,執(zhí)行AsyncCall.executeOn(executorService())方法,并且傳入了Dispatcher的默認(rèn)線(xiàn)程池。
/** * Attempt to enqueue this async call on {@code executorService}. This will attempt to clean up * if the executor has been shut down by reporting the call as failed. */ void executeOn(ExecutorService executorService) { assert (!Thread.holdsLock(client.dispatcher())); boolean success = false; try { executorService.execute(this); success = true; } catch (RejectedExecutionException e) { InterruptedIOException ioException = new InterruptedIOException("executor rejected"); ioException.initCause(e); eventListener.callFailed(RealCall.this, ioException); responseCallback.onFailure(RealCall.this, ioException); } finally { if (!success) { client.dispatcher().finished(this); // This call is no longer running! } } }
- 通過(guò)執(zhí)行AsyncCall.executeOn()方法的源碼,我們看到Dispatcher的線(xiàn)程池執(zhí)行了execute(this)方法,執(zhí)行異步任務(wù),并且指向的是this,也就是當(dāng)前的AsyncCall對(duì)象(RealCall的內(nèi)部類(lèi)),而AsyncCall實(shí)現(xiàn)了抽象類(lèi)NamedRunnable
/** * Runnable implementation which always sets its thread name. */ public abstract class NamedRunnable implements Runnable { protected final String name; public NamedRunnable(String format, Object... args) { this.name = Util.format(format, args); } @Override public final void run() { String oldName = Thread.currentThread().getName(); Thread.currentThread().setName(name); try { execute(); } finally { Thread.currentThread().setName(oldName); } } protected abstract void execute(); }
- 可以看到NamedRunnable中run()方法調(diào)用了抽象方法execute(),也就說(shuō)明execute()的實(shí)現(xiàn)在A(yíng)syncCall對(duì)象中,而上面線(xiàn)程池執(zhí)行的異步任務(wù)也是調(diào)用這個(gè)execute()方法,我們看看AsyncCall對(duì)象中execute()方法的實(shí)現(xiàn)
@Override protected void execute() { boolean signalledCallback = false; timeout.enter(); 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) { 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); } } /**Dispatcher的finished方法*/ /** Used by {@code AsyncCall#run} to signal completion. */ void finished(AsyncCall call) { finished(runningAsyncCalls, call); } /** Used by {@code Call#execute} to signal completion. */ void finished(RealCall call) { finished(runningSyncCalls, call); } private <T> void finished(Deque<T> calls, T call) { Runnable idleCallback; synchronized (this) { if (!calls.remove(call)) throw new AssertionError("Call wasn't in-flight!"); idleCallback = this.idleCallback; } boolean isRunning = promoteAndExecute(); if (!isRunning && idleCallback != null) { idleCallback.run(); } }
- 我們可以先關(guān)注最后一行,不管前面請(qǐng)求如何,最后finally代碼塊中都執(zhí)行了Dispatcher的finished方法,要是要將當(dāng)前任務(wù)從runningAsyncCalls或runningSyncCalls 中移除, 同時(shí)把readyAsyncCalls的任務(wù)調(diào)度到runningAsyncCalls中并執(zhí)行而finished方法中執(zhí)行了promoteAndExecute()方法,經(jīng)過(guò)前面對(duì)該方法分析,說(shuō)明不管當(dāng)前執(zhí)行的任務(wù)如何,都會(huì)OkHttp都會(huì)去readyAsyncCalls(準(zhǔn)備要執(zhí)行的異步請(qǐng)求隊(duì)列)取出下一個(gè)請(qǐng)求繼續(xù)執(zhí)行。接下來(lái)我們繼續(xù)回到AsyncCall對(duì)象中的execute()方法,可以發(fā)現(xiàn)getResponseWithInterceptorChain()的方法返回了Response,說(shuō)明在該方法中執(zhí)行了我們的網(wǎng)絡(luò)請(qǐng)求。而不管同步還是異步請(qǐng)求,都是通過(guò)getResponseWithInterceptorChain()完成網(wǎng)絡(luò)請(qǐng)求。
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); }
-
由getResponseWithInterceptorChain()方法我們看到添加了很多Interceptor(攔截器),首先要了解每個(gè)Interceptor的作用,也能大致了解OKHttp完成網(wǎng)絡(luò)請(qǐng)求的過(guò)程。
- 1.首先加入我們自定義的interceptors
- 2.通過(guò)RetryAndFollowUpInterceptor處理網(wǎng)絡(luò)請(qǐng)求重試
- 3.通過(guò)BridgeInterceptor處理請(qǐng)求對(duì)象轉(zhuǎn)換,應(yīng)用層到網(wǎng)絡(luò)層
- 4.通過(guò)CacheInterceptor處理緩存
- 5.通過(guò)ConnectInterceptor處理網(wǎng)絡(luò)請(qǐng)求鏈接
- 6.通過(guò)CallServerInterceptor處理讀寫(xiě),和服務(wù)器通信,進(jìn)行真正的網(wǎng)絡(luò)請(qǐng)求
-
責(zé)任鏈模式
在閱讀接下來(lái)源碼之前,我們先要了解責(zé)任鏈模式。通俗化的講在責(zé)任鏈模式中有很多對(duì)象,這些對(duì)象可以理解為上面列出的攔截器,而每個(gè)對(duì)象之間都通過(guò)一條鏈子連接,網(wǎng)絡(luò)請(qǐng)求在這條鏈子上傳遞,直到某一個(gè)對(duì)象處理了這個(gè)網(wǎng)絡(luò)請(qǐng)求,也就是完成了網(wǎng)絡(luò)請(qǐng)求。使用這個(gè)模式的好處就是不管你用多少攔截器處理什么操作,最終都不會(huì)影響我們的發(fā)出請(qǐng)求的目的,就是完成網(wǎng)絡(luò)請(qǐng)求,攔截過(guò)程你可以任意添加分配責(zé)任。
-
接著繼續(xù)看Interceptor.Chain,他是Interceptor的內(nèi)部接口,前面添加的每一個(gè)攔截器都實(shí)現(xiàn)了Interceptor接口,而RealInterceptorChain是Interceptor.Chain接口的實(shí)現(xiàn)類(lèi)。先看RealInterceptorChain的proceed方法源碼
public Response proceed(Request request, StreamAllocation streamAllocation, HttpCodec httpCodec, RealConnection connection) throws IOException { if (index >= interceptors.size()) throw new AssertionError(); calls++; ...... // 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); // Confirm that the next interceptor made its required call to chain.proceed(). if (httpCodec != null && index + 1 < interceptors.size() && next.calls != 1) { throw new IllegalStateException("network interceptor " + interceptor + " must call proceed() exactly once"); } ..... return response; }
通過(guò)源碼可以注意到interceptor.intercept(next),RetryAndFollowUpInterceptor作為默認(rèn)攔截器的第一個(gè)攔截器,也就是執(zhí)行了它的intercept方法
-
RetryAndFollowUpInterceptor
前面說(shuō)過(guò)RetryAndFollowUpInterceptor攔截器執(zhí)行OKHttp網(wǎng)絡(luò)重試,先看它的intercept方法
/**RetryAndFollowUpInterceptor的intercept方法 **/ @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); RealInterceptorChain realChain = (RealInterceptorChain) chain; Call call = realChain.call(); EventListener eventListener = realChain.eventListener(); StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(), createAddress(request.url()), call, eventListener, callStackTrace); this.streamAllocation = streamAllocation; int followUpCount = 0; Response priorResponse = null; while (true) { if (canceled) { streamAllocation.release(); throw new IOException("Canceled"); } //將請(qǐng)求通過(guò)鏈子chain傳遞到下一個(gè)攔截器 Response response; boolean releaseConnection = true; try { response = realChain.proceed(request, streamAllocation, null, null); releaseConnection = false; } catch (RouteException e) { // 線(xiàn)路異常,連接失敗,檢查是否可以重新連接 if (!recover(e.getLastConnectException(), streamAllocation, false, request)) { throw e.getFirstConnectException(); } releaseConnection = false; continue; } catch (IOException e) { // An attempt to communicate with a server failed. The request may have been sent. // IO異常,連接失敗,檢查是否可以重新連接 boolean requestSendStarted = !(e instanceof ConnectionShutdownException); if (!recover(e, streamAllocation, 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; try { //效驗(yàn)狀態(tài)碼、身份驗(yàn)證頭信息、跟蹤重定向或處理客戶(hù)端請(qǐng)求超時(shí) followUp = followUpRequest(response, streamAllocation.route()); } catch (IOException e) { streamAllocation.release(); throw e; } if (followUp == null) { streamAllocation.release(); // 不需要重定向,正常返回結(jié)果 return response; } closeQuietly(response.body()); //超過(guò)重試次數(shù) 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); this.streamAllocation = streamAllocation; } 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; } }
- 通過(guò)RetryAndFollowUpInterceptor攔截器intercept方法源碼,能夠理解到OKHttp的重試機(jī)制
- 1.首先創(chuàng)建StreamAllocation對(duì)象(稍后分析),在一個(gè)死循環(huán)中將通過(guò)鏈子chain傳遞到下一個(gè)攔截器,如果捕獲異常,則判斷異常是否恢復(fù)連接,不能連接則拋出異常,跳出循環(huán)并是否創(chuàng)建的連接池資源
- 2.第一步?jīng)]有異常,還要返回值效驗(yàn)狀態(tài)碼、頭部信息、是否需要重定向、連接超時(shí)等信息,捕獲異常則拋出并退出循環(huán)
-
3.如果如果重定向,循環(huán)超出RetryAndFollowUpInterceptor攔截器的最大重試次數(shù),也拋出異常,退出循環(huán)
RetryAndFollowUpInterceptor攔截器重試機(jī)制流程圖.png
- 通過(guò)攔截器RetryAndFollowUpInterceptor調(diào)用(RealInterceptorChain) chain.proceed()方法,又再次回到了我們剛剛分析proceed()方法,而該方法繼續(xù)調(diào)用下一個(gè)攔截器的intercept()方法,這個(gè)攔截器就是默認(rèn)的第二個(gè)攔截器BridgeInterceptor
/** * Bridges from application code to network code. First it builds a network request from a user * request. Then it proceeds to call the network. Finally it builds a user response from the network * response. * BridgeInterceptor的intercept方法 */ @Override public Response intercept(Chain chain) throws IOException { Request userRequest = chain.request(); Request.Builder requestBuilder = userRequest.newBuilder(); RequestBody body = userRequest.body(); ...... Response networkResponse = chain.proceed(requestBuilder.build()); ...... }
- 該攔截器主要實(shí)現(xiàn)了網(wǎng)絡(luò)請(qǐng)求中應(yīng)用層到網(wǎng)絡(luò)層之間的數(shù)據(jù)編碼橋梁。根據(jù)用戶(hù)請(qǐng)求建立網(wǎng)絡(luò)連接,根據(jù)網(wǎng)絡(luò)響應(yīng)建立網(wǎng)絡(luò)響應(yīng),也可以看到該方法 繼續(xù)調(diào)用了chain.proceed()方法,同理,根據(jù)前面分析會(huì)調(diào)用第三個(gè)默認(rèn)攔截器CacheInterceptor的intercept方法。
-
CacheInterceptor
- 前面我們說(shuō)過(guò)這個(gè)攔截器是處理緩存的,接下來(lái)看看源碼是如何實(shí)現(xiàn)的
/** * 攔截器CacheInterceptor的intercept方法 */ @Override public Response intercept(Chain chain) throws IOException { Response cacheCandidate = cache != null ? cache.get(chain.request()) : null; long now = System.currentTimeMillis(); //獲取策略,假設(shè)當(dāng)前可以使用網(wǎng)絡(luò) CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get(); Request networkRequest = strategy.networkRequest; Response cacheResponse = strategy.cacheResponse; if (cache != null) { cache.trackResponse(strategy); } if (cacheCandidate != null && cacheResponse == null) { closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it. } // If we're forbidden from using the network and the cache is insufficient, fail. 如果網(wǎng)絡(luò)被禁止使用并且沒(méi)有緩存,則請(qǐng)求失敗 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.如果有緩存,則返回響應(yīng)緩存,請(qǐng)求完成 if (networkRequest == null) { return cacheResponse.newBuilder() .cacheResponse(stripBody(cacheResponse)) .build(); } Response networkResponse = null; try { //沒(méi)有緩存,則進(jìn)行網(wǎng)絡(luò)請(qǐng)求,執(zhí)行下一個(gè)攔截器 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()); } } // If we have a cache response too, then we're doing a conditional get. if (cacheResponse != null) { //狀態(tài)碼 304 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; }
-
先看看intercept方法的大致邏輯
- 1.首先通過(guò)CacheStrategy.Factory().get()獲取緩存策略
- 2.如果網(wǎng)絡(luò)被禁止使用并且沒(méi)有緩存,則請(qǐng)求失敗,返回504
- 3.如果有響應(yīng)緩存,則返回響應(yīng)緩存,請(qǐng)求完成
- 4.沒(méi)有緩存,則進(jìn)行網(wǎng)絡(luò)請(qǐng)求,執(zhí)行下一個(gè)攔截器
- 5.進(jìn)行網(wǎng)絡(luò)請(qǐng)求,如果響應(yīng)狀態(tài)碼為304,說(shuō)明客戶(hù)端緩存了目標(biāo)資源但不確定該緩存資源是否是最新版本,服務(wù)端數(shù)據(jù)沒(méi)變化,繼續(xù)使用緩存
- 6.最后保存緩存
緩存的場(chǎng)景也符合設(shè)計(jì)模式中的策略模式,需要CacheStrategy提供策略在不同場(chǎng)景下讀緩存還是請(qǐng)求網(wǎng)絡(luò)。
-
了解了緩存邏輯,繼續(xù)深入了解OKHttp的緩存是如何做的。首先我們應(yīng)該回到最初的緩存攔截器設(shè)置代碼
/**RealCall 設(shè)置緩存攔截器*/ interceptors.add(new CacheInterceptor(client.internalCache())); /**OkHttpClient 設(shè)置緩存*/ Cache cache; @Override public void setCache(OkHttpClient.Builder builder, InternalCache internalCache) { builder.setInternalCache(internalCache); } void setInternalCache(@Nullable InternalCache internalCache) { this.internalCache = internalCache; this.cache = null; } InternalCache internalCache() { return cache != null ? cache.internalCache : internalCache; } /**Cache類(lèi)中 內(nèi)部持有 InternalCache */ final DiskLruCache cache; final InternalCache internalCache = new InternalCache() { @Override public Response get(Request request) throws IOException { return Cache.this.get(request); } @Override public CacheRequest put(Response response) throws IOException { return Cache.this.put(response); } @Override public void remove(Request request) throws IOException { Cache.this.remove(request); } @Override public void update(Response cached, Response network) { Cache.this.update(cached, network); } @Override public void trackConditionalCacheHit() { Cache.this.trackConditionalCacheHit(); } @Override public void trackResponse(CacheStrategy cacheStrategy) { Cache.this.trackResponse(cacheStrategy); } };
-
上面我們分別截取了 RealCall類(lèi)、OkHttpClient類(lèi)和Cache類(lèi)的源碼,可以了解到攔截器使用的緩存類(lèi)是DiskLruCache,設(shè)置緩存緩存只能通過(guò)OkHttpClient的builder來(lái)設(shè)置,緩存操作實(shí)現(xiàn)是在Cache類(lèi)中,但是Cache沒(méi)有實(shí)現(xiàn)InternalCache接口,而是持有InternalCache接口的內(nèi)部類(lèi)對(duì)象來(lái)實(shí)現(xiàn)緩存的操作方法,這樣就使得緩存的操作實(shí)現(xiàn)只在Cache內(nèi)部,外部用戶(hù)是無(wú)法實(shí)現(xiàn)緩存操作的,方便框架內(nèi)部使用,接口擴(kuò)展也不影響外部。
Cache和InternalCache類(lèi)之間關(guān)系.png -
ConnectInterceptor
根據(jù)前面的分析,緩存攔截器中也會(huì)調(diào)用chain.proceed方法,所以這時(shí)候執(zhí)行到了第四個(gè)默認(rèn)攔截器ConnectInterceptor,接著看它的intercept方法
/** * 攔截器ConnectInterceptor的intercept方法 */ @Override public Response intercept(Chain chain) throws IOException { RealInterceptorChain realChain = (RealInterceptorChain) chain; Request request = realChain.request(); StreamAllocation streamAllocation = realChain.streamAllocation(); // We need the network to satisfy this request. Possibly for validating a conditional GET. boolean doExtensiveHealthChecks = !request.method().equals("GET"); //打開(kāi)連接 HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks); RealConnection connection = streamAllocation.connection(); //交由下一個(gè)攔截器處理 return realChain.proceed(request, streamAllocation, httpCodec, connection); }
- 我們看到intercept源碼非常簡(jiǎn)單,通過(guò)StreamAllocation打開(kāi)連接,然后就交由下一個(gè)攔截器處理請(qǐng)求。如何連接呢?我們需要搞懂StreamAllocation。
- StreamAllocation對(duì)象負(fù)責(zé)協(xié)調(diào)請(qǐng)求和連接池之間的聯(lián)系。每一個(gè)OKHttpClient有它對(duì)應(yīng)的一個(gè)連接池,經(jīng)過(guò)前面的分析我們知道StreamAllocation對(duì)象的創(chuàng)建在RetryAndFollowUpInterceptor攔截器的intercept方法中創(chuàng)建,而StreamAllocation打開(kāi)了連接,則連接池在哪創(chuàng)建呢,答案就在OKHttpClient的Builder類(lèi)構(gòu)造方法中
public Builder() { ....... connectionPool = new ConnectionPool(); ....... }
- 了解了StreamAllocation對(duì)象和ConnectionPool對(duì)象的創(chuàng)建,下面來(lái)分析StreamAllocation是如何打開(kāi)連接的。首先是streamAllocation.newStream()方法
public HttpCodec newStream( OkHttpClient client, Interceptor.Chain chain, boolean doExtensiveHealthChecks) { ........ try { RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks); ....... } } catch (IOException e) { throw new RouteException(e); } } /** * Finds a connection and returns it if it is healthy. If it is unhealthy the process is repeated * until a healthy connection is found. */ private RealConnection findHealthyConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks) throws IOException { while (true) { RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis, connectionRetryEnabled); ........ return candidate; } } /** * Returns a connection to host a new stream. This prefers the existing connection if it exists, * then the pool, finally building a new connection. */ private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException { ............ // if (result == null) { // Attempt to get a connection from the pool. Internal.instance.get(connectionPool, address, this, null); if (connection != null) { //連接復(fù)用 foundPooledConnection = true; result = connection; } else { selectedRoute = route; } } .......... if (!foundPooledConnection) { ........ result = new RealConnection(connectionPool, selectedRoute); //記錄每個(gè)連接的引用,每個(gè)調(diào)用必須與同一連接上的調(diào)用配對(duì)。 acquire(result, false); } } ......... synchronized (connectionPool) { ....... // Pool the connection. 將連接放入連接池 Internal.instance.put(connectionPool, result); ...... } } ....... return result; }
- 根據(jù)上面的源碼,我們可以知道findHealthyConnection在循環(huán)找健康的連接,直到找到連接,說(shuō)明findConnection方法是尋找連接的核心方法,該方法中存在可以復(fù)用的連接則復(fù)用,否則創(chuàng)建新的連接,并且記錄連接引用,我們可以明白StreamAllocation主要是為攔截器提供一個(gè)連接, 如果連接池中有復(fù)用的連接則復(fù)用連接, 如果沒(méi)有則創(chuàng)建新的連接。
StreamAllocation創(chuàng)建和復(fù)用流程.png -
ConnectionPool連接池實(shí)現(xiàn)
- 明白StreamAllocation是如何創(chuàng)建和復(fù)用連接池,我們還要明白連接池(ConnectionPool)的是如何實(shí)現(xiàn)的。
- 理解ConnectionPool之前,我們需要明白TCP連接的知識(shí),Tcp建立連接三次握手和斷開(kāi)連接四次握手過(guò)程是需要消耗時(shí)間的,在http/1.0每一次請(qǐng)求只能打開(kāi)一次連接,而在http/1.1是支持持續(xù)連接(persistent connection),使得一次連接打開(kāi)之后會(huì)保持一段時(shí)間,如果還是同一個(gè)請(qǐng)求并且使同一個(gè)服務(wù)器則在這段時(shí)間內(nèi)繼續(xù)請(qǐng)求連接是可以復(fù)用的。而ConnectionPool也實(shí)現(xiàn)了這個(gè)機(jī)制,在它內(nèi)部持有一個(gè)線(xiàn)程池和一個(gè)緩存連接的雙向列表,連接中最多只能存在5個(gè)空閑連接,空閑連接最多只能存活5分鐘,空閑連接到期之后定時(shí)清理。
public final class ConnectionPool { /** * Background threads are used to cleanup expired connections. There will be at most a single * thread running per connection pool. The thread pool executor permits the pool itself to be * garbage collected. */ //線(xiàn)程池 private static final Executor executor = new ThreadPoolExecutor(0 /* corePoolSize */, Integer.MAX_VALUE /* maximumPoolSize */, 60L /* keepAliveTime */, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp ConnectionPool", true)); /** The maximum number of idle connections for each address. */ private final int maxIdleConnections; private final long keepAliveDurationNs; private final Runnable cleanupRunnable = new Runnable() { @Override public void run() { // 后臺(tái)定期清理連接的線(xiàn)程 while (true) { long waitNanos = cleanup(System.nanoTime()); if (waitNanos == -1) return; if (waitNanos > 0) { long waitMillis = waitNanos / 1000000L; waitNanos -= (waitMillis * 1000000L); synchronized (ConnectionPool.this) { try { ConnectionPool.this.wait(waitMillis, (int) waitNanos); } catch (InterruptedException ignored) { } } } } } }; //緩存連接的雙向隊(duì)列 private final Deque<RealConnection> connections = new ArrayDeque<>(); ............ /** * Create a new connection pool with tuning parameters appropriate for a single-user application. * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently * this pool holds up to 5 idle connections which will be evicted after 5 minutes of inactivity. */ public ConnectionPool() { this(5, 5, TimeUnit.MINUTES); } ............ }
ConnectionPool連接池緩存清理流程.png- 這里還要說(shuō)的一點(diǎn)是streamAllocation.newStream()返回的HttpCodec對(duì)象就是我們編碼HTTP請(qǐng)求并解碼HTTP響應(yīng)的接口,他的實(shí)現(xiàn)類(lèi)Http2Codec和Http1Codec對(duì)應(yīng)https和http的解析request與響應(yīng)response對(duì)socket讀寫(xiě)過(guò)程實(shí)現(xiàn),并最終放到RealConnection對(duì)象newCodec類(lèi)中創(chuàng)建。
/** RealConnection類(lèi)newCodec方法 */ public HttpCodec newCodec(OkHttpClient client, Interceptor.Chain chain, StreamAllocation streamAllocation) throws SocketException { if (http2Connection != null) { return new Http2Codec(client, chain, streamAllocation, http2Connection); } else { socket.setSoTimeout(chain.readTimeoutMillis()); source.timeout().timeout(chain.readTimeoutMillis(), MILLISECONDS); sink.timeout().timeout(chain.writeTimeoutMillis(), MILLISECONDS); return new Http1Codec(client, streamAllocation, source, sink); } }
- streamAllocation得到連接對(duì)象,也就是RealConnection對(duì)象,它封裝了套接字socket連接,也就是該類(lèi)的connectSocket方法。并且使用OKio來(lái)對(duì)數(shù)據(jù)讀寫(xiě)。OKio封裝了Java的I/O操作,這里就不細(xì)說(shuō)了。最后返回的ConnectInterceptor攔截器的intercept方法同樣調(diào)用了Chain.proceed,將拿到的連接交由CallServerInterceptor做處理。
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. RealConnection類(lèi)connectSocket方法 */ private void connectSocket(int connectTimeout, int readTimeout, Call call, EventListener eventListener) throws IOException { Proxy proxy = route.proxy(); Address address = route.address(); rawSocket = proxy.type() == Proxy.Type.DIRECT || proxy.type() == Proxy.Type.HTTP ? address.socketFactory().createSocket() : new Socket(proxy); eventListener.connectStart(call, route.socketAddress(), proxy); rawSocket.setSoTimeout(readTimeout); try { //打開(kāi) socket 連接 Platform.get().connectSocket(rawSocket, route.socketAddress(), connectTimeout); } catch (ConnectException e) { ConnectException ce = new ConnectException("Failed to connect to " + route.socketAddress()); ce.initCause(e); throw ce; } // The following try/catch block is a pseudo hacky way to get around a crash on Android 7.0 // More details: // https://github.com/square/okhttp/issues/3245 // https://android-review.googlesource.com/#/c/271775/ try { //使用OKio來(lái)對(duì)數(shù)據(jù)讀寫(xiě) source = Okio.buffer(Okio.source(rawSocket)); sink = Okio.buffer(Okio.sink(rawSocket)); } catch (NullPointerException npe) { if (NPE_THROW_WITH_NULL.equals(npe.getMessage())) { throw new IOException(npe); } } }
- 最后返回的ConnectInterceptor攔截器的intercept方法同樣調(diào)用了Chain.proceed,將拿到的連接交由CallServerInterceptor做處理。
-
CallServerInterceptor
- 在經(jīng)過(guò)前面一系列攔截器之后,OKHttp最終把拿到網(wǎng)絡(luò)請(qǐng)求連接給到CallServerInterceptor攔截器進(jìn)行網(wǎng)絡(luò)請(qǐng)求和服務(wù)器通信。
/**CallServerInterceptor的intercept方法*/ @Override public Response intercept(Chain chain) throws IOException { RealInterceptorChain realChain = (RealInterceptorChain) chain; HttpCodec httpCodec = realChain.httpStream(); StreamAllocation streamAllocation = realChain.streamAllocation(); RealConnection connection = (RealConnection) realChain.connection(); Request request = realChain.request(); long sentRequestMillis = System.currentTimeMillis(); realChain.eventListener().requestHeadersStart(realChain.call()); //按照HTTP協(xié)議,依次寫(xiě)入請(qǐng)求體 httpCodec.writeRequestHeaders(request); ................. if (responseBuilder == null) { realChain.eventListener().responseHeadersStart(realChain.call()); // responseBuilder = httpCodec.readResponseHeaders(false); } Response response = responseBuilder .request(request) .handshake(streamAllocation.connection().handshake()) .sentRequestAtMillis(sentRequestMillis) .receivedResponseAtMillis(System.currentTimeMillis()) .build(); ............... if (forWebSocket && code == 101) { // Connection is upgrading, but we need to ensure interceptors see a non-null response body. response = response.newBuilder() .body(Util.EMPTY_RESPONSE) .build(); } else { //響應(yīng)數(shù)據(jù)OKio寫(xiě)入 response = response.newBuilder() .body(httpCodec.openResponseBody(response)) .build(); } } return response; } /**Http1Codec方法**/ //OKio 讀寫(xiě)對(duì)象 final BufferedSource source; final BufferedSink sink; @Override public void writeRequestHeaders(Request request) throws IOException { //構(gòu)造好請(qǐng)求頭 String requestLine = RequestLine.get( request, streamAllocation.connection().route().proxy().type()); writeRequest(request.headers(), requestLine); } /** Returns bytes of a request header for sending on an HTTP transport. 將請(qǐng)求信息寫(xiě)入sink */ public void writeRequest(Headers headers, String requestLine) throws IOException { if (state != STATE_IDLE) throw new IllegalStateException("state: " + state); sink.writeUtf8(requestLine).writeUtf8("\r\n"); for (int i = 0, size = headers.size(); i < size; i++) { sink.writeUtf8(headers.name(i)) .writeUtf8(": ") .writeUtf8(headers.value(i)) .writeUtf8("\r\n"); } sink.writeUtf8("\r\n"); state = STATE_OPEN_REQUEST_BODY; }
- 可以看到在CallServerInterceptor攔截器的方法中首先通過(guò)HttpCodec(上面貼的是Http1Codec的方法)writeRequestHeaders和writeRequest方法寫(xiě)入請(qǐng)求體,并將請(qǐng)求體寫(xiě)入OKio的寫(xiě)入對(duì)象sink中
/**Http1Codec方法**/ /** 讀取響應(yīng)頭信息 */ @Override public Response.Builder readResponseHeaders(boolean expectContinue) throws IOException { if (state != STATE_OPEN_REQUEST_BODY && state != STATE_READ_RESPONSE_HEADERS) { throw new IllegalStateException("state: " + state); } try { StatusLine statusLine = StatusLine.parse(readHeaderLine()); Response.Builder responseBuilder = new Response.Builder() .protocol(statusLine.protocol) .code(statusLine.code) .message(statusLine.message) .headers(readHeaders()); if (expectContinue && statusLine.code == HTTP_CONTINUE) { return null; } else if (statusLine.code == HTTP_CONTINUE) { state = STATE_READ_RESPONSE_HEADERS; return responseBuilder; } state = STATE_OPEN_RESPONSE_BODY; return responseBuilder; } catch (EOFException e) { // Provide more context if the server ends the stream before sending a response. IOException exception = new IOException("unexpected end of stream on " + streamAllocation); exception.initCause(e); throw exception; } } /** 寫(xiě)入響應(yīng)輸入到ResponseBody */ @Override public ResponseBody openResponseBody(Response response) throws IOException { streamAllocation.eventListener.responseBodyStart(streamAllocation.call); String contentType = response.header("Content-Type"); if (!HttpHeaders.hasBody(response)) { Source source = newFixedLengthSource(0); return new RealResponseBody(contentType, 0, Okio.buffer(source)); } if ("chunked".equalsIgnoreCase(response.header("Transfer-Encoding"))) { Source source = newChunkedSource(response.request().url()); return new RealResponseBody(contentType, -1L, Okio.buffer(source)); } long contentLength = HttpHeaders.contentLength(response); if (contentLength != -1) { Source source = newFixedLengthSource(contentLength); return new RealResponseBody(contentType, contentLength, Okio.buffer(source)); } return new RealResponseBody(contentType, -1L, Okio.buffer(newUnknownLengthSource())); }
- 通過(guò)readResponseHeaders方法讀取響應(yīng)頭信息,openResponseBody得到響應(yīng)體信息。最終將網(wǎng)絡(luò)請(qǐng)求的響應(yīng)信息通過(guò)Callback()回調(diào)方法異步傳遞出去,同步請(qǐng)求則直接返回。到此OKHttp源碼理解到此為止。
-
最后,通過(guò)OKHttp這個(gè)框架源碼閱讀,也是對(duì)自己的一個(gè)提升,不僅了解了框架原理,設(shè)計(jì)模式在適宜場(chǎng)景的運(yùn)用,同時(shí)也是對(duì)自己耐心的一次考驗(yàn),源碼的閱讀是枯燥的,但是只要靜下心來(lái),也能發(fā)現(xiàn)閱讀源碼的樂(lè)趣。由于本人水平有限,文章中如果有錯(cuò)誤,請(qǐng)大家給我提出來(lái),大家一起學(xué)習(xí)進(jìn)步,如果覺(jué)得我的文章給予你幫助,也請(qǐng)給我一個(gè)喜歡和關(guān)注。
-
參考鏈接:
-
參考書(shū)籍:
- 《計(jì)算機(jī)網(wǎng)絡(luò)》第六版