HTTP connection pool configuration
An HTTP connection pool reduces latency, optimizes resource usage, and improves response speed in high-concurrency web services, frequent API calls, and distributed system communication. You can configure the HTTP connection pool parameters for the Java asynchronous SDK to match your workload.
In the SDK, all asynchronous HTTP requests are managed by `PoolingAsyncClientConnectionManager`. The default connection pool parameters are as follows:
|
Parameter |
Description |
|
maxConnections |
The size of the connection pool. The default value is 128. |
|
maxConnectionsPerRoute |
The maximum number of connections per route. The default value is 128. |
|
maxIdleTimeOut |
The maximum idle time for a connection in the pool. The default value is 30,000 ms. |
|
keepAlive |
The idle time to live for a connection. The default value is 20,000 ms. |
|
connectRequestTimeout |
The timeout period for obtaining a connection. The default value is 30,000 ms. |
To handle higher concurrency, increase the values of `maxConnections`, `maxConnectionsPerRoute`, and `connectRequestTimeout`.
The following is sample code:
HttpClient httpClient = new ApacheAsyncHttpClientBuilder()
.maxConnections(10) // Set the size of the connection pool. The default value is 128.
.maxConnectionsPerRoute(10) // Set the maximum number of connections per route. The default value is 128.
.maxIdleTimeOut(Duration.ofMillis(20000)) // Set the maximum idle time for a connection in the pool. The default value is 30,000 ms.
.keepAlive(Duration.ofMillis(5000)) // The idle time to live for a connection. The default value is 20,000 ms.
.connectRequestTimeout(Duration.ofMillis(5000)) // The timeout period for obtaining a connection. The default value is 30,000 ms.
.build();
AsyncClient client = AsyncClient.builder()
.httpClient(httpClient)
.credentialsProvider(credentialProvider) // The implementation of credentialProvider is omitted in this example...
.build();