Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use same connection pool for all clients per OKHttp recommendations #1274

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sbrattla
Copy link

The OKHttp documentation recommends the following usage pattern:

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.

The current HttpCommand implementation for OKHttp creates a new OkHttpClient for each request, and subsequently a new connection pool for each request. This may not be an big issue since the connection pool is set to allow 0 connections and also to evict connections after 500 ms. Nevertheless, the overhead of initializing a new connection pool and related threads for each and every http request may not be negligible for a large number of http requests.

This pull request attempts to address this by providing a singleton client available to all requests. This singleton client is not the client used for the actual http requests, but serves as a "template" client. We use client.newBuilder() to create a shallow copy of the singleton client. This will reuse the existing connection pool, but still enable us to fully customize each http request.

The singleton OkHttpClient will be instantiated in the initialize method of HttpCommand.

int maxIdleConnections = 0;
// OkHttp creates "OkHttp ConnectionPool" thread per every ConnectionPool created to mange its connections. It
// lives as long as the last connection made through it + its keep alive timeout. By default that it 5 min which
// makes no sense for openstack4j since the connections or pools are not reused (after HttpCommand completion,
// at least). Setting strict keepAlive duration here so the connections and threads does not hang around longer
// than necessary.
int keepAliveDuration = 500;
return new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.MILLISECONDS);
ConnectionPool cp = new ConnectionPool(maxIdleConnections, keepAliveDuration, TimeUnit.MILLISECONDS);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Author of the original workaround here. I like the approach of rectifying the misuse of OkHttp client, though I belive we have to rethink the keepalive duration and the size of the pool. The only reason this was ever tempered with was the numerous OkHttpClient instances took minutes to purge the threads and connections causing resource waste. I guess we can get back to OkHttp's defaults here as we are using the (single) client the was it was meant to be.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we need any of this now: dc97e67#diff-e08fdcb99d047ca41bbe3fe37e41802f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants