@Test public void testGlobalIdleTimeout() throws IOException { final long times[] = new long[]{-1, -1}; try (AsyncHttpClient client = asyncHttpClient(config().setPooledConnectionIdleTimeout(2000))) { Future<Response> responseFuture = client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandler<Response>() { @Override public Response onCompleted(Response response) { return response; } @Override public State onBodyPartReceived(HttpResponseBodyPart content) throws Exception { times[0] = unpreciseMillisTime(); return super.onBodyPartReceived(content); } @Override public void onThrowable(Throwable t) { times[1] = unpreciseMillisTime(); super.onThrowable(t); } }); Response response = responseFuture.get(); assertNotNull(response); assertEquals(response.getResponseBody(), MSG + MSG); } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { logger.info(String.format("\n@%dms Last body part received\n@%dms Connection killed\n %dms difference.", times[0], times[1], (times[1] - times[0]))); fail("Timeouted on idle.", e); } }
@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); } } } } }
@Test public void testClientStatus() throws Throwable { try (final AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setPooledConnectionIdleTimeout(5000))) { final String url = getTargetUrl();
config.setPooledConnectionIdleTimeout(poolConfiguration.getIdleConnectionInPoolTimeout());
.setMaxConnections(maxConnections).setMaxConnectionsPerHost(maxConnectionsPerHost) .setConnectTimeout(connectTimeout).setReadTimeout(readTimeout) .setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout).setRequestTimeout(requestTimeout) .setFollowRedirect(followRedirect).setMaxRedirects(maxRedirects).setMaxRequestRetry(maxRequestRetry) .setConnectionTtl(connectionTtl).build();
.setMaxConnections(maxConnections).setMaxConnectionsPerHost(maxConnectionsPerHost) .setConnectTimeout(connectTimeout).setReadTimeout(readTimeout) .setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout).setRequestTimeout(requestTimeout) .setFollowRedirect(followRedirect).setMaxRedirects(maxRedirects).setMaxRequestRetry(maxRequestRetry) .setConnectionTtl(connectionTtl).build();
/** * 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); }
@Test(groups = "online", invocationCount = 4) public void multipleSslRequestsWithDelayAndKeepAlive() throws Exception { AsyncHttpClientConfig config = config() .setFollowRedirect(true) .setKeepAlive(true) .setConnectTimeout(10000) .setPooledConnectionIdleTimeout(60000) .setRequestTimeout(10000) .setMaxConnectionsPerHost(-1) .setMaxConnections(-1) .build(); try (AsyncHttpClient client = asyncHttpClient(config)) { final BoundRequestBuilder builder = client.prepareGet(GOOGLE_HTTPS_URL); final Response response1 = builder.execute().get(); Thread.sleep(4000); final Response response2 = builder.execute().get(); assertNotNull(response1); assertNotNull(response2); } } }
public Builder setPooledConnectionIdleTimeout(int pooledConnectionIdleTimeout) { configBuilder.setPooledConnectionIdleTimeout(pooledConnectionIdleTimeout); return this; }
@Test public void idleStateTest() throws Exception { try (AsyncHttpClient c = asyncHttpClient(config().setPooledConnectionIdleTimeout(10 * 1000))) { c.prepareGet(getTargetUrl()).execute().get(); } catch (ExecutionException e) { fail("Should allow to finish processing request.", e); } }