7

I'm currently using okhttp 3.10.0 and i just recently found out that creating a new client for every new request is bad for to each client having it's own connection pool.

I'm using okhttp inside servlets. Whatever I'm now doing is declaring a static client in each servlet and initializing it inside the init() method

client = new OkHttpClient.Builder()
              .readTimeout(15000,TimeUnit.MILLISECONDS)
              .retryOnConnectionFailure(false)
              .connectTimeOut(10000,TimeUnit.MILLISECONDS)
              .connectionPool(new ConnectionPool(20,5L,TimeUnit.MINUTES)
              .build();

Now here, there's the configuration for max idle connections which I'm setting to 20 and the idle timeout is set to 5 minutes.

1) What is the actual pool size, i.e the maximum connections that the pool will have (looking through the code, i found that it might be Integer.MAX_VALUE and max simultaneous connections to a host is default 64 but I'm not sure)?

2) Is this approach good enough. My current approach will be making one client per servlet. So should i instead have one singleton class which gives the same client to all my Servlets in which case what should i configure for ideal load for the following specs.i couldn't find how to configure the actual pool size so i don't know if this even is possible

Digital ocean droplet

Ram 2gb

Cpu cores 2

Tomcat max heap 768mb

Nginx concurrent workers 1000

Mysql max ram ~800mb

Load i'd want to support ~500 users per sec max

1 Answer 1

8

You should set up an application-wide singleton. Preferably with a dependency injector like Guice or Spring, but a static singleton is also fine.

How many distinct hostnames are you connecting to? If it's very many, the long tail of hosts won't benefit from the pool. If it's just a few, then your pool will track your peek concurrent connections.

For a starting point, set it to match the thread count in your servlet container.

8
  • Can you tell me how i can set it? I can't find how to set it anywhere. Also how would i find my thread count?
    – Kushan
    Mar 2, 2018 at 14:23
  • The host names aren't a lot, i just got the shock today when i found out how inefficient my current setup was. I will be making a singleton now but i don't know how to configure the pool size of okhttp and i don't know what the ideal value would be. Could you please post some code fragments to guide me. And if you tell me how to find out the thread count, it'll be amazing
    – Kushan
    Mar 2, 2018 at 14:55
  • Hy jesse, i found out my thread count... I'm not using any executors to my thread count is default 200... Now where do i set this in the okhttp thread pool as it's size? I'm not using enqueue and using execute... So where would i set this 200?
    – Kushan
    Mar 2, 2018 at 18:02
  • Create a ConnectionPool using that class’ constructor, and then pass that ConnectionPool instance to your OkHttpClient.Builder. Mar 3, 2018 at 2:56
  • I m doing that already, but the constructor doesnt let me set max pool size, only the maxidlecount and timeout.... Is the max count auto?
    – Kushan
    Mar 3, 2018 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.