Connection pool and File handles

后端 未结 1 1947
挽巷
挽巷 2020-12-29 15:21

We use Retrofit/OkHttp3 for all network traffic from our Android application. So far everything seems to run quite smoothly.

However, we have now occasionally had ou

相关标签:
1条回答
  • 2020-12-29 15:43

    I am answering my own question here to document this issue we had. It took us a while to figure this out and I think others might encounter this too and might be glad for this answer.


    Our problem was that we created one OkHttpClient per request, as we used it's builder/interceptor API to configure some per-request parameters like HTTP headers or timeouts.

    By default each OkHttpClient comes with its own connection pool, which of course blows up the number of connections/threads/file handles and prevents proper reuse in the pool.

    Our solution

    We solved the problem by manually creating a global ConnectionPool in a singleton, and then passing that to the OkHttpClient.Builder object which build the actual OkHttpClient.

    • This still allows for per-request configuration using the OkHttpClient.Builder
    • Makes sure all OkHttpClient instances are still using a common connection pool.

    We were the able to properly size the global connection pool.

    0 讨论(0)
提交回复
热议问题