2014-10-11 39 views
1
  1. 當同步使用HttpOk時。模式與連接池,maxIdleConnections是每臺主機或全局的限制?OkHttp每個主機或全局連接池maxIdleConnections?

    在源代碼中,我可以看到一條評論,說它是每個地址,但是我無法在公共文檔(javadoc,wiki)中找到這些信息,而且在查看源代碼時它似乎是全局的我只看到它用在connectionsCleanupRunnable而不知道地址)。

  2. 如果所有「緩存」連接正在使用,而另一個線程想要建立連接,則會創建一個新連接?它會在交換結束時關閉還是添加到連接池中,並在下次連接清理運行時運行時關閉並清理乾淨?

謝謝:)

回答

1

這是每一個主機。如果他們在那裏呆了5分鐘,空閒連接將被從池中移除。

2

在okHTTP3連接池是不是全球性的再

Note: as of OkHttp3, it is recommended you declare this object as a singleton because changes in OkHttp3 no long require a global connection pool. 

樓盤編號https://guides.codepath.com/android/Using-OkHttp

There is no longer a global singleton connection pool. In OkHttp 2.x, all OkHttpClient instances shared a common connection pool by default. In OkHttp 3.x, each new OkHttpClient gets its own private connection pool. Applications should avoid creating many connection pools as doing so prevents connection reuse. Each connection pool holds its own set of connections alive so applications that have many pools also risk exhausting memory! 

The best practice in OkHttp 3 is to create a single OkHttpClient instance and share it throughout the application. Requests that needs a customized client should call OkHttpClient.newBuilder() on that shared instance. This allows customization without the drawbacks of separate connection pools. 

樓盤編號https://github.com/amplitude/Amplitude-Android/issues/79