简体   繁体   中英

Okhttpclient connection pool size dilemma

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, ie 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM