OkHttp源碼分析(二)整體流程

以如下請求為例進行說明

OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder().url("http://gank.io/api/data/Android/10/1").build();
Call call = httpClient.newCall(request);
call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        Log.e(TAG, e.getMessage());
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        String result = response.body().string();
        Log.d(TAG, result);
    }
});

OkHttp和Request的構建就不說了,我們來看下Call的構建

Call的構建

Call是個接口

public interface Call extends Cloneable {
  Request request();
  Response execute() throws IOException;
  void enqueue(Callback responseCallback);
  boolean isExecuted();
  boolean isCanceled();
  Call clone();

  interface Factory {
    Call newCall(Request request);
  }
}

內部還定義了一個Factory接口,OkHttpClient實現了該接口

QQ20170507-084805@2x.png
@Override 
public Call newCall(Request request) {
   return new RealCall(this, request, false /* for web socket */);
}

內部創建了一個RealCall,注意RealCall是Call唯一實現類

RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    final EventListener.Factory eventListenerFactory = client.eventListenerFactory();
    this.client = client;
    this.originalRequest = originalRequest;
    this.forWebSocket = forWebSocket;
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);

    // TODO(jwilson): this is unsafe publication and not threadsafe.
    this.eventListener = eventListenerFactory.create(this);
  }

構造函數中做了以下幾件事

  • EventListener.Factory先忽略,這里不關心;
  • 保存OkHttpClient實例;
  • 保存原始的Request實例;
  • WebSocket的標識,傳過來的是false,表示沒用WebSocket;
  • 創建了第一個攔截器RetryAndFollowUpInterceptor,用來處理請求異常重試;

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));
 }
  • client.dispatcher()返回Dispatcher對象,它在OkHttpClient構建時創建,用來處理請求的任務隊列,使用了生產者消費者模式
  • 創建一個AsyncCall,丟給Dispatcher
synchronized void enqueue(AsyncCall call) {
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
      runningAsyncCalls.add(call);
      executorService().execute(call);
    } else {
      readyAsyncCalls.add(call);
    }
  }
  • maxRequests = 64: 最大并發請求數為64
  • maxRequestsPerHost = 5: 每個主機最大請求數為5

當滿足正在執行的請求數小于64并且每個主機最大請求數小于5,直接把AsyncCall加到runningCalls的隊列中,并在ExecutorService中執行。反之就放入readyAsyncCalls進行等待。這里會進入第一個if塊。
接著執行AsyncCall#execute

@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) {
        if (signalledCallback) {
          // Do not signal the callback twice!
          Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
        } else {
          responseCallback.onFailure(RealCall.this, e);
        }
      } finally {
        client.dispatcher().finished(this);
      }
    }

這個方法做了三件事

  1. 通過getResponseWithInterceptorChain執行請求
  • 處理回調,onRespons or onFailure
  • 釋放Call,將執行完畢的Call從runningCalls隊列中移除,并取出readyAsyncCalls中的一個Call加入到runningCalls執行。

1.責任鏈分發請求與響應

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);
    return chain.proceed(originalRequest);
  }

在這個方法中我們看到了大量的Interceptor,不同的Interceptor賦予不同的職責,有的處理請求重試、有的處理緩存、有的處理請求壓縮、有的執行實際的請求等等,這些Interceptor被Interceptor.Chain串聯起來,順序執行。

這里使用了責任鏈設計模式

責任鏈模式是一種對象的行為模式。在責任鏈模式里,很多對象由每一個對象對其下家的引用而連接起來形成一條鏈。請求在這個鏈上傳遞,直到鏈上的某一個對象決定處理此請求。發出這個請求的客戶端并不知道鏈上的哪一個對象最終處理這個請求,這使得系統可以在不影響客戶端的情況下動態地重新組織和分配責任。

689802-816d907d9939eb58.png

我們看看責任鏈的實現
Interceptor是個接口,定義如下

public interface Interceptor {
  Response intercept(Chain chain) throws IOException;

  interface Chain {
    Request request();
    Response proceed(Request request) throws IOException;
    Connection connection();
  }
}

內部還定義了Chain接口,RealInterceptorChain是唯一實現類,在getResponseWithInterceptorChain內部new了一個RealInterceptorChain
然后調用RealInterceptorChain#proceed

@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 {
    if (index >= interceptors.size()) throw new AssertionError();

    calls++;

    // If we already have a stream, confirm that the incoming request will use it.
    if (this.httpCodec != null && !this.connection.supportsUrl(request.url())) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must retain the same host and port");
    }

    // If we already have a stream, confirm that this is the only call to chain.proceed().
    if (this.httpCodec != null && calls > 1) {
      throw new IllegalStateException("network interceptor " + interceptors.get(index - 1)
          + " must call proceed() exactly once");
    }

    // Call the next interceptor in the chain.
    RealInterceptorChain next = new RealInterceptorChain(
        interceptors, streamAllocation, httpCodec, connection, index + 1, request);
    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");
    }

    // Confirm that the intercepted response isn't null.
    if (response == null) {
      throw new NullPointerException("interceptor " + interceptor + " returned null");
    }
    return response;
}
  • 循環取出攔截器集合中的每個Interceptpr
  • 新建一個RealInterceptorChain,作為上一步取出的Interceptor#Intercept的參數
  • 調用Interceptor#Intercept方法,取出Request,在把它轉發給下一個Interceptor之前做一些加工處理。
  • intercept中通過調用RealInterceptorChain#proceed,將請求轉發給下一個Interceptor處理
  • 當前Interceptor將阻塞等待下一個Interceptor返回Response,拿到Response后,自己在加工處理一下再交給上一個Interceptor。
1819140-312c2f839564cfa3.png

后面將具體分析每一個攔截器的職責。

2. 請求響應回調

在getResponseWithInterceptorChain中拿到Response后需要回調Callback了。

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) {
      if (signalledCallback) {
        // Do not signal the callback twice!
        Platform.get().log(INFO, "Callback failure for " + toLoggableString(), e);
      } else {
        responseCallback.onFailure(RealCall.this, e);
      }
 } 

回調失敗

兩種情況

  • 請求被取消;
  • getResponseWithInterceptorChain內部發生IO異常;

回調成功

  • onResponse,請求結果封裝在ResponseBody中

注意:onFailure和onResponse都執行在線程池的線程中,而不是UI線程。
如果在以上回調中操作UI將拋出如下異常

QQ20170507-161133@2x.png

3. 釋放Call

在finally中最后調用了

client.dispatcher().finished(this);

按照Java語法規范,無論發生異常與否,finally中的代碼必然會執行的。
因此,最終調用Dispatcher#finished

/** Used by {@code AsyncCall#run} to signal completion. */
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();
    }
}
  • promoteCalls為true
  • 從runningSyncCalls中移除該Call
  • 接著執行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.
    }
}
  • 如果正在執行的隊列任務數仍大于64,繼續等待;
  • 如果等待隊列為空,啥也不做,返回;
  • 循環從等待隊列中取出一個AsyncCall
    • 如果滿足單個主機并發數小于5,則將該AsyncCall加入執行隊列,并執行execute;
    • 當執行隊列并發數大于64,退出循環;

Refference

http://www.lxweimin.com/p/aad5aacd79bf
https://blog.piasy.com/2016/07/11/Understand-OkHttp/

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容