OkHttp how to set maximum connection pool size (no

2019-06-18 02:56发布

问题:

In OkHttp I cannot find a way to set a hard maximum connection pool size. From the documentation https://square.github.io/okhttp/3.x/okhttp/okhttp3/ConnectionPool.html it is clear that you can set a maximum idle connections, but not an overall max. That means that with high load it can grow beyond any limit.

Is there a way to maximize the pool? If not, why not?

回答1:

Connections are either active and held by a particular in-flight call, or idle and in the pool. Limit the total number of connections by limiting the number of threads executing HTTP calls. If you’re using Call.execute() (synchronous) just size your thread pool appropriately. If you’re using Call.enqueue() (asynchronous) the limits are in Dispatcher.