Search code examples
javaokhttp

Should I worry about OkHttp 4 Connection Pools?


I used to create OkHttp Clients like this:

new OkHttpClient.Builder().connectionPool(connectionPool)
                                 .readTimeout(Duration.ofSeconds(timeout))
                                 .connectTimeout(Duration.ofMillis(timeout))
                                 .addInterceptor(logging)
                                 .build();

But now I'm getting a warning about connectionPool being kotlin internal (I'm using it from java).

It seems related to upgrading to OkHttp 4.

Should I forget about pools and let OkHttp handle it?

If I create many OkHttpClients within my application. If I don't configure the pool, will they use the same pool, or each client will have it's own pool? I have one application with many different HTTP connection usage scenarios.


Solution

  • From documentation:

    OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a client for each request wastes resources on idle pools.

    So, yes, each client will have it's own pool.