OKHTTP3源碼2-連接池管理

整體架構(gòu)

《OKHTTP3源碼和設(shè)計模式-1》浊闪,中整體介紹了 OKHttp3 的源碼架構(gòu),重點講解了請求任務的分發(fā)管理和線程池以及請求執(zhí)行過程中的攔截器概荷。這一章我們接著往下走認識一下 OKHttp3 底層連接和連接池工作機制秕岛。

RealCall 封裝了請求過程, 組織了用戶和內(nèi)置攔截器误证,其中內(nèi)置攔截器 retryAndFollowUpInterceptor -> BridgeInterceptor -> CacheInterceptor 完執(zhí)行層的大部分邏輯 继薛,ConnectInterceptor -> CallServerInterceptor 兩個攔截器開始邁向連接層最終完成網(wǎng)絡(luò)請求。

連接層連接器

進入連接層

ConnectInterceptor 的工作很簡單愈捅, 負責打開連接; CallServerIntercerceptor 是核心連接器鏈上的最后一個連接器遏考,
負責從當前連接中寫入和讀取數(shù)據(jù)。

連接的打開

    /** Opens a connection to the target server and proceeds to the next interceptor. */
        // 打開一個和目標服務器的連接蓝谨,并把處理交個下一個攔截器
  public final class ConnectInterceptor implements Interceptor {
   public final OkHttpClient client;

   public ConnectInterceptor(OkHttpClient client) {
    this.client = client;
  }

  @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");
            // 打開連接
    HttpCodec httpCodec = streamAllocation.newStream(client, doExtensiveHealthChecks);
    RealConnection connection = streamAllocation.connection();
     // 交個下一個攔截器
   return realChain.proceed(request, streamAllocation, httpCodec, connection);
  }
}

單獨看 ConnectInterceptor 的代碼很簡單诈皿,不過連接正在打開的過程需要看看 streamAllocation.newStream(client, doExtensiveHealthChecks),內(nèi)部執(zhí)行過程像棘。還是先整體上了看看 StreamAllocation 這個類的作用稽亏。

StreamAllocation

StreamAllocation 處于上層請求和底層連接池直接 , 協(xié)調(diào)請求和連接池直接的關(guān)系缕题。先來看看 StreamAllocation 對象在哪里創(chuàng)建的? 回到之前文章中介紹的 RetryAndFollowUpInterceptor截歉, 這是核心攔截器鏈上的頂層攔截器其中源碼:

    @Override 
    public Response intercept(Chain chain) throws IOException {
             Request request = chain.request();
       streamAllocation = new StreamAllocation(
       client.connectionPool(), createAddress(request.url()), callStackTrace);
      ...省略代碼
  }

這里, 每一次請求創(chuàng)建了一個 StreamAllocation 對象烟零, 那么問題來了瘪松? 之前我們說過每一個 OkHttpClient 對象只有一個對應的連接池咸作, 剛剛又說到 StreamAllocation 打開連接, 那么 StreamAllocation 是如何創(chuàng)建連接池的呢宵睦?我們很容易就去 StreamAllocation 中找連接池創(chuàng)建的邏輯记罚,但是找不到。 連接池創(chuàng)建的地方在 OkHttpClient 中:

   public Builder() {
      dispatcher = new Dispatcher();
      protocols = DEFAULT_PROTOCOLS;
      connectionSpecs = DEFAULT_CONNECTION_SPECS;
      eventListenerFactory = EventListener.factory(EventListener.NONE);
      proxySelector = ProxySelector.getDefault();
      cookieJar = CookieJar.NO_COOKIES;
      socketFactory = SocketFactory.getDefault();
      hostnameVerifier = OkHostnameVerifier.INSTANCE;
      certificatePinner = CertificatePinner.DEFAULT;
      proxyAuthenticator = Authenticator.NONE;
      authenticator = Authenticator.NONE;
                // 創(chuàng)建連接池
      connectionPool = new ConnectionPool();
      dns = Dns.SYSTEM;
      followSslRedirects = true;
      followRedirects = true;
      retryOnConnectionFailure = true;
      connectTimeout = 10_000;
      readTimeout = 10_000;
      writeTimeout = 10_000;
      pingInterval = 0;
}

OkHttpClient 默認構(gòu)造函數(shù)的 Builder 壳嚎, 在這里創(chuàng)建了連接池桐智。所以這里我們也可以看到, 如果我們對默認連接池不滿烟馅,我們是可以直通過 builder 接指定的说庭。
搞懂了 StreamAllocation 和 ConnectionPool 的創(chuàng)建 , 我們再來看看 StreamAllocation 是怎么打開連接的郑趁?直接兜源碼可能有點繞 刊驴,先給一個粗略流程圖,然后逐點分析寡润。

連接創(chuàng)建請求流程

鏈接池實現(xiàn)

相信大家都有一些 Http 協(xié)議的基礎(chǔ)(如果沒有就去補了捆憎,不然看不懂)都知道 Http 的下層協(xié)議是 TCP。TCP 連接的創(chuàng)建和斷開是有性能開銷的梭纹,在 Http1.0 中攻礼,每一次請求就打開一個連接,在一些老的舊的瀏覽器上栗柒,如果還是基于 Http1.0礁扮,體驗會非常差; Http1.1 以后支持長連接, 運行一個請求打開連接完成請求后瞬沦, 連接可以不關(guān)閉太伊, 下次請求時復用此連接,從而提高連接的利用率逛钻。當然并不是連接打開后一直開著不關(guān)僚焦,這樣又會造成連接浪費,怎么管理曙痘?
在OKHttp3 的默認實現(xiàn)中芳悲,使用一個雙向隊列來緩存所有連接, 這些連接中最空閑時間已經(jīng)超過了keep-alive指定的時間就要移除了边坤。


連接

定期清理實現(xiàn)

     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.
   */
        // 后臺定期清理連接的線程池
       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() {
      while (true) {
                   // cleanup 執(zhí)行清理
        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) {
            }
          }
        }
      }
    }
  };

雙向隊列

 // 存儲連接的雙向隊列
private final Deque<RealConnection> connections = new ArrayDeque<>();

放入連接

void put(RealConnection connection) {
    assert (Thread.holdsLock(this));
    if (!cleanupRunning) {
      cleanupRunning = true;
      executor.execute(cleanupRunnable);
   }
   connections.add(connection);
 }

獲取連接

RealConnection get(Address address, StreamAllocation streamAllocation, Route route) {
    assert (Thread.holdsLock(this));
    for (RealConnection connection : connections) {
      if (connection.isEligible(address, route)) {
        streamAllocation.acquire(connection);
        return connection;
      }
    }
    return null;
}

StreamAllocation.連接創(chuàng)建和復用

ConnectionPool 的源碼邏輯還是相當比較簡單名扛, 主要提供一個雙向列表來存取連接, 使用一個定時任務定期清理無用連接茧痒。 二連接的創(chuàng)建和復用邏輯主要在 StreamAllocation 中肮韧。

尋找連接

private RealConnection findHealthyConnection(int connectTimeout, int readTimeout,
  int writeTimeout, boolean connectionRetryEnabled, boolean doExtensiveHealthChecks)
  throws IOException {
while (true) {
       //  核心邏輯在 findConnection()中
  RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout,
      connectionRetryEnabled);

  // If this is a brand new connection, we can skip the extensive health checks.
  synchronized (connectionPool) {
    if (candidate.successCount == 0) {
      return candidate;
    }
  }

  // Do a (potentially slow) check to confirm that the pooled connection is still good. If it
  // isn't, take it out of the pool and start again.
  if (!candidate.isHealthy(doExtensiveHealthChecks)) {
    noNewStreams();
    continue;
  }

  return candidate;
}
}

findConnection():

private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
  boolean connectionRetryEnabled) throws IOException {
Route selectedRoute;
synchronized (connectionPool) {
  // 省略部分代碼...
  // Attempt to get a connection from the pool. Internal.instance 就是 ConnectionPool 的實例
  Internal.instance.get(connectionPool, address, this, null);
  if (connection != null) {
           // 復用此連接
    return connection;
  }
          // 省略部分代碼...
            // 創(chuàng)建新新連接
  result = new RealConnection(connectionPool, selectedRoute);
        // 引用計數(shù)
  acquire(result);
}

synchronized (connectionPool) {
  // Pool the connection. 放入連接池
  Internal.instance.put(connectionPool, result);
 }
  // 省略部分代碼...
 return result;
}       

StreamAllocation 主要是為上層提供一個連接, 如果連接池中有復用的連接則復用連接, 如果沒有則創(chuàng)建新的弄企。無論是拿到可復用的還是創(chuàng)建新的超燃, 都要為此連接計算一下引用計數(shù)。

public void acquire(RealConnection connection) {
 assert (Thread.holdsLock(connectionPool));
 if (this.connection != null) throw new IllegalStateException();

 this.connection = connection;
     //  連接使用allocations列表來記錄每一個引用
 connection.allocations.add(new StreamAllocationReference(this, callStackTrace));
}

Realconnection

Realconnection 封裝了底層 socket 連接拘领, 同時使用 OKio 來進行數(shù)據(jù)讀寫意乓, OKio 是 square 公司的另一個獨立的開源項目, 大家感興趣可以去深入讀下 OKio 源碼约素, 這里不展開届良。

連接
/** Does all the work necessary to build a full HTTP or HTTPS connection on a raw socket. */
  private void connectSocket(int connectTimeout, int readTimeout) 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);

    rawSocket.setSoTimeout(readTimeout);
    try {
              // 打開 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;
    }
           // 使用 OKil 連上 socket 后續(xù)讀寫使用 Okio
    source = Okio.buffer(Okio.source(rawSocket));
    sink = Okio.buffer(Okio.sink(rawSocket));
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市业汰,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌菩颖,老刑警劉巖样漆,帶你破解...
    沈念sama閱讀 218,682評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異晦闰,居然都是意外死亡放祟,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評論 3 395
  • 文/潘曉璐 我一進店門呻右,熙熙樓的掌柜王于貴愁眉苦臉地迎上來跪妥,“玉大人,你說我怎么就攤上這事声滥∶寄欤” “怎么了?”我有些...
    開封第一講書人閱讀 165,083評論 0 355
  • 文/不壞的土叔 我叫張陵落塑,是天一觀的道長纽疟。 經(jīng)常有香客問我,道長憾赁,這世上最難降的妖魔是什么污朽? 我笑而不...
    開封第一講書人閱讀 58,763評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮龙考,結(jié)果婚禮上蟆肆,老公的妹妹穿的比我還像新娘。我一直安慰自己晦款,他們只是感情好炎功,可當我...
    茶點故事閱讀 67,785評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著缓溅,像睡著了一般亡问。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,624評論 1 305
  • 那天州藕,我揣著相機與錄音束世,去河邊找鬼。 笑死床玻,一個胖子當著我的面吹牛毁涉,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播锈死,決...
    沈念sama閱讀 40,358評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼贫堰,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了待牵?” 一聲冷哼從身側(cè)響起其屏,我...
    開封第一講書人閱讀 39,261評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎缨该,沒想到半個月后偎行,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體贰拿,經(jīng)...
    沈念sama閱讀 45,722評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡蛤袒,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年妙真,在試婚紗的時候發(fā)現(xiàn)自己被綠了菱阵。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嫡锌。...
    茶點故事閱讀 40,030評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡及皂,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出礼烈,到底是詐尸還是另有隱情弧满,我是刑警寧澤,帶...
    沈念sama閱讀 35,737評論 5 346
  • 正文 年R本政府宣布此熬,位于F島的核電站庭呜,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏摹迷。R本人自食惡果不足惜疟赊,卻給世界環(huán)境...
    茶點故事閱讀 41,360評論 3 330
  • 文/蒙蒙 一郊供、第九天 我趴在偏房一處隱蔽的房頂上張望峡碉。 院中可真熱鬧,春花似錦驮审、人聲如沸鲫寄。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,941評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽地来。三九已至,卻和暖如春熙掺,著一層夾襖步出監(jiān)牢的瞬間未斑,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,057評論 1 270
  • 我被黑心中介騙來泰國打工币绩, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蜡秽,地道東北人。 一個月前我還...
    沈念sama閱讀 48,237評論 3 371
  • 正文 我出身青樓缆镣,卻偏偏與公主長得像芽突,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子董瞻,可洞房花燭夜當晚...
    茶點故事閱讀 44,976評論 2 355

推薦閱讀更多精彩內(nèi)容