Skip to content
bbc-tomfitzhenry edited this page Jun 14, 2016 · 14 revisions

async-http-client supports HTTP persistent connections. Connections are stored in a ChannelPool.

The default ChannelPool is DefaultChannelPool.

Optionally, connection pool behaviour can be configured via DefaultAsyncHttpClientConfig.Builder:

DefaultAsyncHttpClientConfig.Builder builder = new DefaultAsyncHttpClientConfig.Builder()
    .setMaxConnections(500)
    .setMaxConnectionsPerHost(200)
    .setPooledConnectionIdleTimeout(100)
    .setConnectionTtl(500)
    .build();
AsyncHttpClient http = new DefaultAsyncHttpClient(builder);

Default values can found in ahc-default.properties

Per-implementation configuration

DefaultChannelPool

By default, DefaultChannelPool will lease connections on a Last In, First Out basis. This has a few advantages:

  • keeps the fewest number of connections active, thus allowing the remaining connections to be terminated via idle timeouts, thus conserving resources
  • by leasing recently-used connections, there's less chance the leased connections are closed, or are in the process of being closed

Alternatively, some users may prefer to keep an artificially large number of connections, for example, to be able to handle bursty load. This can be achieved by configuring DefaultChannelPool to lease connections a First In, First Out basis:

HashedWheelTimer timer = new HashedWheelTimer();
timer.start();
ChannelPool pool = new DefaultChannelPool(60000, -1, DefaultChannelPool.PoolLeaseStrategy.FIFO, timer);
DefaultAsyncHttpClientConfig httpCfg = new DefaultAsyncHttpClientConfig.Builder()
    .setChannelPool(pool)
    .setNettyTimer(timer)
    .build();
AsyncHttpClient httpClient = new DefaultAsyncHttpClient(httpCfg);
Clone this wiki locally