Why does okhttp3 connection pool use Int.MAX_VALUE as maximumPoolSize?

Viewed 195

When a large number of requests are executed, a large number of threads are created to maintain idle connections, and these threads are in the TIMED_WAITING state for five minutes.

The following source code exists in the latest version:

class RealBackend(threadFactory: ThreadFactory) : Backend {
  private val executor = ThreadPoolExecutor(
      0, // corePoolSize.
      Int.MAX_VALUE, // maximumPoolSize.
      60L, TimeUnit.SECONDS, // keepAliveTime.
      SynchronousQueue(),
      threadFactory
  )
}

From okhttp/TaskRunner.kt at master · square/okhttp · GitHub

Connection pool source code: okhttp/ConnectionPool.kt at master · square/okhttp · GitHub

Similar feedback in the GitHub repository issue:

Why not use a lower value to limit the peak number of threads? Too high thread count may cause problems.

1 Answers

OkHttp limits its own resource usage limits, but not here. Instead, Dispatcher limits how many calls are in flight concurrently, and ConnectionPool limits how many connections are alive.

Related