Okhttp3源碼解析(3)-Call分析(整體流程)

###前言
前面我們講了
Okhttp的基本用法
Okhttp3源碼解析(1)-OkHttpClient分析
Okhttp3源碼解析(2)-Request分析

newCall分析

Call初始化

我們首先看一下在哪用到了Call:

   final Call call = okHttpClient.newCall(request);

想起來了吧?無論是get還是post請求 都要生成call對象,在上面我們發現call實例需要一個okHttpClientrequest實例 ,我們先點進Call類去看看:

public interface Call extends Cloneable {
//請求
  Request request();
//同步
  Response execute() throws IOException;
  //異步
  void enqueue(Callback responseCallback);
  //取消請求
  void cancel();
  //是否在請求過程中
  boolean isExecuted();
  //是否取消
  boolean isCanceled();
  Call clone();
  //工廠接口
  interface Factory {
    Call newCall(Request request);
  }
}

我們發現Call是個接口, 並定義了一些方方法(方法含義在註釋上)。
我們繼續看newCal()方法

  @Override public Call newCall(Request request) {
    return RealCall.newRealCall(this, request, false /* for web socket */);
  }

繼續點擊newRealCall()去:

  private RealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    this.client = client;
    this.originalRequest = originalRequest;
    this.forWebSocket = forWebSocket;
    this.retryAndFollowUpInterceptor = new RetryAndFollowUpInterceptor(client, forWebSocket);
  }

  static RealCall newRealCall(OkHttpClient client, Request originalRequest, boolean forWebSocket) {
    // Safely publish the Call instance to the EventListener.
    RealCall call = new RealCall(client, originalRequest, forWebSocket);
    call.eventListener = client.eventListenerFactory().create(call);
    return call;
  }

從代碼中我們發現在newRealCall()中初始化了RealCallRealCall中初始化了retryAndFollowUpInterceptor

  • client: OkHttpClient 實例
  • originalRequest : 最初的Request
  • forWebSocket :是否支持websocket通信
  • retryAndFollowUpInterceptor 從字面意思來說, 是重試和重定向攔截器 ,至於它有什麼作用我們繼續往下看

同步請求分析

 Response response = call.execute();

我們點進execute()中查看:

  @Override public Response execute() throws IOException {
    synchronized (this) {
      if (executed) throw new IllegalStateException("Already Executed");
      executed = true;
    }
    captureCallStackTrace();
    eventListener.callStart(this);
    try {
      client.dispatcher().executed(this);
      Response result = getResponseWithInterceptorChain();
      if (result == null) throw new IOException("Canceled");
      return result;
    } catch (IOException e) {
      eventListener.callFailed(this, e);
      throw e;
    } finally {
      client.dispatcher().finished(this);
    }
  }

從上面代碼得知步驟:
(1).通過 synchronized 保證線程同步,判斷是否已經執行過 ,如果是直接拋異常
(2). captureCallStackTrace(); 字面意思:捕獲調用堆棧跟蹤,我們通過源碼發現裏面涉及到了retryAndFollowUpInterceptor
(3). eventListener 回調CallStart()
(4). client.dispatcher().executed(this); 看到了dispatcher是不是很熟悉?之前在分析okhttpClient初始化的時候遇到了,我們點擊executed()方法進去:

  synchronized void executed(RealCall call) {
    runningSyncCalls.add(call);
  }

發現把我們傳進來的realcall放到了runningSyncCalls隊列中,從字面意思來說就是正在運行的同步的調用隊列中,爲什麼說是隊列呢? :

  private final Deque<RealCall> runningSyncCalls = new ArrayDeque<>();

Deque即雙端隊列。是一種具有隊列和棧的性質的數據結構。雙端隊列中的元素可以從兩端彈出,相比list增加[]運算符重載。

(5).我們回到execute()繼續往下分析,剩下的代碼我們提取出三行代碼:

  • equesr result = getResponseWithInterceptorChain(); 生成一個Response 實例
  • eventListener.callFailed(this, e); :eventListener的callFailed回調
  • client.dispatcher().finished(this); :dispatcher實例的finished方法

不難看出,getResponseWithInterceptorChain()一定是此方法中的核心,字面意思是獲取攔截器鏈的響應,這就明白了,就是通過攔截器鏈處理後返回Response

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);
  }

從上面代碼不難看出, 對最初的request做了層層攔截,每個攔截器的原理我們放在以後的章節去講, 這裏就不展開了!
這裏需要強調的一下 interceptors.addAll(client.interceptors());client.interceptors() 是我們自定義的攔截器 它是在哪定義的?如何添加?我們去OkHttpClient類中發現:

可以通過初始化okHttpClient實例 .addInterceptor的形式 添加。

異步請求分析

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("okhttp_error",e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Gson gson=new Gson();

                Log.d("okhttp_success",response.body().string());
            }
   });

點擊enqueue()查看:

  @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));
  }

(1).通過 synchronized 保證線程同步,判斷是否已經執行過 ,如果是直接拋異常
(2). captureCallStackTrace(); 字面意思:捕獲調用堆棧跟蹤,我們通過源碼發現裏面涉及到了retryAndFollowUpInterceptor
(3). eventListener 回調CallStart()
(4). client.dispatcher().enqueue(new AsyncCall(responseCallback)); 調用了Dispatcher.enqueue()並傳入了一個**new AsyncCall(responseCallback)實例,點擊AsyncCall**查看:
AsyncCall 是RealCall的內部類!

  final class AsyncCall extends NamedRunnable {
    private final Callback responseCallback;

    AsyncCall(Callback responseCallback) {
      super("OkHttp %s", redactedUrl());
      this.responseCallback = responseCallback;
    }

    String host() {
      return originalRequest.url().host();
    }

    Request request() {
      return originalRequest;
    }

    RealCall get() {
      return RealCall.this;
    }

    @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 {
          eventListener.callFailed(RealCall.this, e);
          responseCallback.onFailure(RealCall.this, e);
        }
      } finally {
        client.dispatcher().finished(this);
      }
    }
  }

AsyncCall繼承了NamedRunnable ,我們看下NamedRunnable是什麼:

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 實現了Runnable 接口 是個線程類,在run()中 添加了抽象的execute();方法,看到這裏 我們應該有一個反應,那就是AsyncCall中具體的execute()應該在子線程執行
我們繼續分析,client.dispatcher().enqueue(new AsyncCall(responseCallback)); 點擊進入enqueue():

  synchronized void enqueue(AsyncCall call) {
    if (runningAsyncCalls.size() < maxRequests && runningCallsForHost(call) < maxRequestsPerHost) {
      runningAsyncCalls.add(call);
      executorService().execute(call);
    } else {
      readyAsyncCalls.add(call);
    }
  }
  • runningAsyncCalls 正在運行的異步請求的隊列
  • maxRequests 最大的請求數 64
  • maxRequestsPerHost host最大請求數 5 (可以通過Get與Set方式自定義設置)

如果正在運行的異步請求的隊列大小低於64並且 正在請求的host數量低於5,就會執行(滿足條件)

     runningAsyncCalls.add(call);
     executorService().execute(call);

這裏把 AsyncCall實例添加到 runningAsyncCalls中。
ExecutorService 表示線程池 繼續看 executorService()

  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;
  }

其實就是生成了executorService 實例,這就明白了,AsyncCall實例放入線程池中執行了!

如果不滿足上面的請求數等條件:

  readyAsyncCalls.add(call);

就會被添加到一個等待就緒的異步請求隊列中,目的是什麼呢??? 當然是等待時機再次添加到runningAsyncCalls中並放入線程池中執行,這塊邏輯在 AsyncCall類中的 execute() 至於原因我們繼續往下看!

剛纔我們說了,如果條件滿足, AsyncCall實例就會在線程池中執行(.start),那我們直接去看run()中的 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 {
          eventListener.callFailed(RealCall.this, e);
          responseCallback.onFailure(RealCall.this, e);
        }
      } finally {
        client.dispatcher().finished(this);
      }
    }

上面代碼中得知, 首先通過層層攔截器鏈處理生成了response;然後通過一系列的判斷,responseCallback進行onResponseonFailure回調,最後調用的Dispatcher.finifshed()
這裏需要注意的是 這裏的Dispatcher.finifshed(this)與同步中的Dispatcher.finifshed(this)不一樣 參數不同。

  /** Used by {@code AsyncCall#run} to signal completion. */
  void finished(AsyncCall call) {
    finished(runningAsyncCalls, call, true);
  }

我們繼續看具體的finifshed()方法:

  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();

  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.
    }
  }

經過一系列的判斷, 對等待就緒的異步隊列進行遍歷,生成對應的AsyncCall實例,並添加到runningAsyncCalls中,最後放入到線程池中執行! 這裏就是我們上面說到的等待就緒的異步隊列如何與runningAsyncCalls對接的邏輯。

總結

同步請求流程:
  • 生成call實例realcall
  • Dispatcher.executed()中的runningSyncCalls 添加realcall到此隊列中
  • 通過 getResponseWithInterceptorChain() 對request層層攔截,生成Response
  • 通過Dispatcher.finished(),把call實例從隊列中移除,返回最終的response
異步請求流程:
  • 生成一個AsyncCall(responseCallback)實例(實現了Runnable)
  • AsyncCall實例放入了Dispatcher.enqueue()中,並判斷maxRequests (最大請求數)maxRequestsPerHost(最大host請求數)是否滿足條件,如果滿足就把AsyncCall添加到runningAsyncCalls中,並放入線程池中執行;如果條件不滿足,就添加到等待就緒的異步隊列,當那些滿足的條件的執行時 ,在Dispatcher.finifshed(this)中的promoteCalls();方法中 對等待就緒的異步隊列進行遍歷,生成對應的AsyncCall實例,並添加到runningAsyncCalls中,最後放入到線程池中執行,一直到所有請求都結束。

至此OKhttp整體流程就分析完了, 下一篇會分塊去分析,希望對大家有所幫助…

大家可以關注我的微信公衆號:「秦子帥」一個有質量、有態度的公衆號!

公衆號

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章