Search in sources :

Example 6 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class TestStrictConnPool method testStatefulConnectionRedistributionOnPerRouteMaxLimit.

@Test
public void testStatefulConnectionRedistributionOnPerRouteMaxLimit() throws Exception {
    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
    final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
    pool.setMaxPerRoute("somehost", 2);
    pool.setMaxTotal(2);
    final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
    Assertions.assertTrue(future1.isDone());
    final PoolEntry<String, HttpConnection> entry1 = future1.get();
    entry1.assignConnection(conn1);
    Assertions.assertNotNull(entry1);
    Assertions.assertTrue(future2.isDone());
    final PoolEntry<String, HttpConnection> entry2 = future2.get();
    Assertions.assertNotNull(entry2);
    entry2.assignConnection(conn2);
    PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(0, totals.getAvailable());
    Assertions.assertEquals(2, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    entry1.updateState("some-stuff");
    pool.release(entry1, true);
    entry2.updateState("some-stuff");
    pool.release(entry2, true);
    final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("somehost", "some-stuff");
    final Future<PoolEntry<String, HttpConnection>> future4 = pool.lease("somehost", "some-stuff");
    Assertions.assertTrue(future1.isDone());
    final PoolEntry<String, HttpConnection> entry3 = future3.get();
    Assertions.assertNotNull(entry3);
    Assertions.assertSame(conn2, entry3.getConnection());
    Assertions.assertTrue(future4.isDone());
    final PoolEntry<String, HttpConnection> entry4 = future4.get();
    Assertions.assertNotNull(entry4);
    Assertions.assertSame(conn1, entry4.getConnection());
    pool.release(entry3, true);
    pool.release(entry4, true);
    totals = pool.getTotalStats();
    Assertions.assertEquals(2, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    final Future<PoolEntry<String, HttpConnection>> future5 = pool.lease("somehost", "some-other-stuff");
    Assertions.assertTrue(future5.isDone());
    Mockito.verify(conn2).close(CloseMode.GRACEFUL);
    Mockito.verify(conn1, Mockito.never()).close(ArgumentMatchers.any());
    totals = pool.getTotalStats();
    Assertions.assertEquals(1, totals.getAvailable());
    Assertions.assertEquals(1, totals.getLeased());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 7 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class RequesterBootstrap method create.

public HttpRequester create() {
    final HttpRequestExecutor requestExecutor = new HttpRequestExecutor(HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE, connReuseStrategy != null ? connReuseStrategy : DefaultConnectionReuseStrategy.INSTANCE, streamListener);
    final ManagedConnPool<HttpHost, HttpClientConnection> connPool;
    switch(poolConcurrencyPolicy != null ? poolConcurrencyPolicy : PoolConcurrencyPolicy.STRICT) {
        case LAX:
            connPool = new LaxConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
        case STRICT:
        default:
            connPool = new StrictConnPool<>(defaultMaxPerRoute > 0 ? defaultMaxPerRoute : 20, maxTotal > 0 ? maxTotal : 50, timeToLive, poolReusePolicy, new DefaultDisposalCallback<>(), connPoolListener);
            break;
    }
    return new HttpRequester(requestExecutor, httpProcessor != null ? httpProcessor : HttpProcessors.client(), connPool, socketConfig != null ? socketConfig : SocketConfig.DEFAULT, connectFactory != null ? connectFactory : new DefaultBHttpClientConnectionFactory(Http1Config.DEFAULT, CharCodingConfig.DEFAULT), sslSocketFactory, sslSetupHandler != null ? sslSetupHandler : new DefaultTlsSetupHandler(), sslSessionVerifier, DefaultAddressResolver.INSTANCE);
}
Also used : HttpRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor) DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) HttpHost(org.apache.hc.core5.http.HttpHost) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) DefaultBHttpClientConnectionFactory(org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory) DefaultTlsSetupHandler(org.apache.hc.core5.http.io.ssl.DefaultTlsSetupHandler)

Example 8 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class ClassicTestClient method start.

public void start(final HttpProcessor httpProcessor) {
    if (requesterRef.get() == null) {
        final HttpRequestExecutor requestExecutor = new HttpRequestExecutor(HttpRequestExecutor.DEFAULT_WAIT_FOR_CONTINUE, DefaultConnectionReuseStrategy.INSTANCE, LoggingHttp1StreamListener.INSTANCE);
        final StrictConnPool<HttpHost, HttpClientConnection> connPool = new StrictConnPool<>(20, 50, TimeValue.NEG_ONE_MILLISECOND, PoolReusePolicy.LIFO, LoggingConnPoolListener.INSTANCE);
        final HttpRequester requester = new HttpRequester(requestExecutor, httpProcessor != null ? httpProcessor : HttpProcessors.client(), connPool, socketConfig, new LoggingBHttpClientConnectionFactory(Http1Config.DEFAULT, CharCodingConfig.DEFAULT), sslContext != null ? sslContext.getSocketFactory() : null, null, null, DefaultAddressResolver.INSTANCE);
        requesterRef.compareAndSet(null, requester);
    } else {
        throw new IllegalStateException("Requester has already been started");
    }
}
Also used : HttpRequestExecutor(org.apache.hc.core5.http.impl.io.HttpRequestExecutor) StrictConnPool(org.apache.hc.core5.pool.StrictConnPool) HttpHost(org.apache.hc.core5.http.HttpHost) HttpClientConnection(org.apache.hc.core5.http.io.HttpClientConnection) HttpRequester(org.apache.hc.core5.http.impl.bootstrap.HttpRequester)

Example 9 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class TestStrictConnPool method testCloseExpired.

@Test
public void testCloseExpired() throws Exception {
    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
    final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 2);
    final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
    Assertions.assertTrue(future1.isDone());
    final PoolEntry<String, HttpConnection> entry1 = future1.get();
    Assertions.assertNotNull(entry1);
    entry1.assignConnection(conn1);
    Assertions.assertTrue(future2.isDone());
    final PoolEntry<String, HttpConnection> entry2 = future2.get();
    Assertions.assertNotNull(entry2);
    entry2.assignConnection(conn2);
    entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS));
    pool.release(entry1, true);
    Thread.sleep(200);
    entry2.updateExpiry(TimeValue.of(1000, TimeUnit.SECONDS));
    pool.release(entry2, true);
    pool.closeExpired();
    Mockito.verify(conn1).close(CloseMode.GRACEFUL);
    Mockito.verify(conn2, Mockito.never()).close(ArgumentMatchers.any());
    final PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(1, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    final PoolStats stats = pool.getStats("somehost");
    Assertions.assertEquals(1, stats.getAvailable());
    Assertions.assertEquals(0, stats.getLeased());
    Assertions.assertEquals(0, stats.getPending());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 10 with StrictConnPool

use of org.apache.hc.core5.pool.StrictConnPool in project httpcomponents-core by apache.

the class TestStrictConnPool method testLeaseRelease.

@Test
public void testLeaseRelease() throws Exception {
    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn3 = Mockito.mock(HttpConnection.class);
    final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
    final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("otherhost", null);
    final PoolEntry<String, HttpConnection> entry1 = future1.get();
    Assertions.assertNotNull(entry1);
    entry1.assignConnection(conn1);
    final PoolEntry<String, HttpConnection> entry2 = future2.get();
    Assertions.assertNotNull(entry2);
    entry2.assignConnection(conn2);
    final PoolEntry<String, HttpConnection> entry3 = future3.get();
    Assertions.assertNotNull(entry3);
    entry3.assignConnection(conn3);
    pool.release(entry1, true);
    pool.release(entry2, true);
    pool.release(entry3, false);
    Mockito.verify(conn1, Mockito.never()).close(ArgumentMatchers.any());
    Mockito.verify(conn2, Mockito.never()).close(ArgumentMatchers.any());
    Mockito.verify(conn3, Mockito.times(1)).close(CloseMode.GRACEFUL);
    final PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(2, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Aggregations

HttpConnection (org.apache.hc.core5.http.HttpConnection)10 Test (org.junit.jupiter.api.Test)10 HttpHost (org.apache.hc.core5.http.HttpHost)4 DefaultDisposalCallback (org.apache.hc.core5.pool.DefaultDisposalCallback)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpRequestExecutor (org.apache.hc.core5.http.impl.io.HttpRequestExecutor)2 ClientHttp1StreamDuplexerFactory (org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory)2 HttpClientConnection (org.apache.hc.core5.http.io.HttpClientConnection)2 IOEventHandlerFactory (org.apache.hc.core5.reactor.IOEventHandlerFactory)2 IOSession (org.apache.hc.core5.reactor.IOSession)2 ExecutionException (java.util.concurrent.ExecutionException)1 Supplier (org.apache.hc.core5.function.Supplier)1 HttpRequester (org.apache.hc.core5.http.impl.bootstrap.HttpRequester)1 DefaultBHttpClientConnectionFactory (org.apache.hc.core5.http.impl.io.DefaultBHttpClientConnectionFactory)1 ClientHttp1IOEventHandlerFactory (org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory)1 DefaultHttpResponseParserFactory (org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory)1 DefaultTlsSetupHandler (org.apache.hc.core5.http.io.ssl.DefaultTlsSetupHandler)1 BasicClientTlsStrategy (org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy)1 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)1 RequestHandlerRegistry (org.apache.hc.core5.http.protocol.RequestHandlerRegistry)1