@Test(enabled = false, description = "https://github.com/AsyncHttpClient/async-http-client/issues/1113") public void testTTLBug() throws Throwable { // The purpose of this test is to reproduce two issues: // 1) Connections that are rejected by the pool are not closed and eventually use all available sockets. // 2) It is possible for a connection to be closed while active by the timer task that checks for expired connections. try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setConnectionTtl(1).setPooledConnectionIdleTimeout(1))) { for (int i = 0; i < 200000; ++i) { Request request = new RequestBuilder().setUrl(String.format("http://localhost:%d/", port1)).build(); Future<Response> future = client.executeRequest(request); future.get(5, TimeUnit.SECONDS); // This is to give a chance to the timer task that removes expired connection // from sometimes winning over poll for the ownership of a connection. if (System.currentTimeMillis() % 100 == 0) { Thread.sleep(5); } } } } }
config.setConnectTimeout(timeout); config.setFollowRedirect(false); config.setConnectionTtl((int) (period * 2)); // 2 times the period so it gives a chance under heavy load to reuse connections config.setMaxConnectionsPerHost(maxConnections + 10); config.setMaxConnections(maxConnections + 10);
/** * Initialize the client config. * * @param requestTimeout Timeout to use for the client configuration. * * @return the set up client */ private static AsyncHttpClient initializeWebClient(int requestTimeout) { LOG.debug("Druid request timeout: {}ms", requestTimeout); List<String> cipherSuites = SYSTEM_CONFIG.getListProperty(SSL_ENABLED_CIPHER_KEY, null); String[] enabledCipherSuites = cipherSuites == null || cipherSuites.isEmpty() ? null : cipherSuites.toArray(new String[cipherSuites.size()]); // Build the configuration AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() .setReadTimeout(requestTimeout) .setRequestTimeout(requestTimeout) .setConnectTimeout(requestTimeout) .setConnectionTtl(requestTimeout) .setPooledConnectionIdleTimeout(requestTimeout) .setEnabledCipherSuites(enabledCipherSuites) .setFollowRedirect(true) .build(); return new DefaultAsyncHttpClient(config); }
config.setHttpClientCodecMaxChunkSize(poolConfiguration.getMaxFileUploadSize()); config.setChunkedFileChunkSize(poolConfiguration.getMaxFileUploadSize()); config.setConnectionTtl(poolConfiguration.getConnectionTtl());
.setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout).setRequestTimeout(requestTimeout) .setFollowRedirect(followRedirect).setMaxRedirects(maxRedirects).setMaxRequestRetry(maxRequestRetry) .setConnectionTtl(connectionTtl).build(); asyncHttpClient = new DefaultAsyncHttpClient(config);
.setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout).setRequestTimeout(requestTimeout) .setFollowRedirect(followRedirect).setMaxRedirects(maxRedirects).setMaxRequestRetry(maxRequestRetry) .setConnectionTtl(connectionTtl).build(); asyncHttpClient = new DefaultAsyncHttpClient(config);