Search in sources :

Example 1 with StrictConnPool

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

the class H2RequesterBootstrap method create.

public H2AsyncRequester create() {
    final ManagedConnPool<HttpHost, IOSession> 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;
    }
    final RequestHandlerRegistry<Supplier<AsyncPushConsumer>> registry = new RequestHandlerRegistry<>(uriPatternType);
    for (final HandlerEntry<Supplier<AsyncPushConsumer>> entry : pushConsumerList) {
        registry.register(entry.hostname, entry.uriPattern, entry.handler);
    }
    final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory = new ClientH2StreamMultiplexerFactory(httpProcessor != null ? httpProcessor : H2Processors.client(), new DefaultAsyncPushConsumerFactory(registry), h2Config != null ? h2Config : H2Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, streamListener);
    final TlsStrategy actualTlsStrategy = tlsStrategy != null ? tlsStrategy : new H2ClientTlsStrategy();
    final ClientHttp1StreamDuplexerFactory http1StreamHandlerFactory = new ClientHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.client(), http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, DefaultConnectionReuseStrategy.INSTANCE, new DefaultHttpResponseParserFactory(http1Config), DefaultHttpRequestWriterFactory.INSTANCE, DefaultContentLengthStrategy.INSTANCE, DefaultContentLengthStrategy.INSTANCE, http1StreamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ClientHttpProtocolNegotiationStarter(http1StreamHandlerFactory, http2StreamHandlerFactory, versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, actualTlsStrategy, handshakeTimeout);
    return new H2AsyncRequester(versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE, ioReactorConfig, ioEventHandlerFactory, ioSessionDecorator, exceptionCallback, sessionListener, connPool, actualTlsStrategy, handshakeTimeout);
}
Also used : H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) TlsStrategy(org.apache.hc.core5.http.nio.ssl.TlsStrategy) DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) ClientH2StreamMultiplexerFactory(org.apache.hc.core5.http2.impl.nio.ClientH2StreamMultiplexerFactory) ClientHttpProtocolNegotiationStarter(org.apache.hc.core5.http2.impl.nio.ClientHttpProtocolNegotiationStarter) RequestHandlerRegistry(org.apache.hc.core5.http.protocol.RequestHandlerRegistry) DefaultAsyncPushConsumerFactory(org.apache.hc.core5.http2.nio.support.DefaultAsyncPushConsumerFactory) DefaultHttpResponseParserFactory(org.apache.hc.core5.http.impl.nio.DefaultHttpResponseParserFactory) ClientHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory) HttpHost(org.apache.hc.core5.http.HttpHost) H2ClientTlsStrategy(org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy) IOSession(org.apache.hc.core5.reactor.IOSession) Supplier(org.apache.hc.core5.function.Supplier) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory)

Example 2 with StrictConnPool

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

the class AsyncRequesterBootstrap method create.

public HttpAsyncRequester create() {
    final ManagedConnPool<HttpHost, IOSession> 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;
    }
    final ClientHttp1StreamDuplexerFactory streamDuplexerFactory = new ClientHttp1StreamDuplexerFactory(httpProcessor != null ? httpProcessor : HttpProcessors.client(), http1Config != null ? http1Config : Http1Config.DEFAULT, charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT, connStrategy, null, null, streamListener);
    final IOEventHandlerFactory ioEventHandlerFactory = new ClientHttp1IOEventHandlerFactory(streamDuplexerFactory, tlsStrategy != null ? tlsStrategy : new BasicClientTlsStrategy(), handshakeTimeout);
    return new HttpAsyncRequester(ioReactorConfig, ioEventHandlerFactory, ioSessionDecorator, exceptionCallback, sessionListener, connPool);
}
Also used : DefaultDisposalCallback(org.apache.hc.core5.pool.DefaultDisposalCallback) BasicClientTlsStrategy(org.apache.hc.core5.http.nio.ssl.BasicClientTlsStrategy) ClientHttp1StreamDuplexerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1StreamDuplexerFactory) HttpHost(org.apache.hc.core5.http.HttpHost) IOSession(org.apache.hc.core5.reactor.IOSession) ClientHttp1IOEventHandlerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory) IOEventHandlerFactory(org.apache.hc.core5.reactor.IOEventHandlerFactory) ClientHttp1IOEventHandlerFactory(org.apache.hc.core5.http.impl.nio.ClientHttp1IOEventHandlerFactory)

Example 3 with StrictConnPool

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

the class TestStrictConnPool method testCloseIdle.

@Test
public void testCloseIdle() 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.updateState(null);
    pool.release(entry1, true);
    Thread.sleep(200L);
    entry2.updateState(null);
    pool.release(entry2, true);
    pool.closeIdle(TimeValue.of(50, TimeUnit.MILLISECONDS));
    Mockito.verify(conn1).close(CloseMode.GRACEFUL);
    Mockito.verify(conn2, Mockito.never()).close(ArgumentMatchers.any());
    PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(1, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    PoolStats stats = pool.getStats("somehost");
    Assertions.assertEquals(1, stats.getAvailable());
    Assertions.assertEquals(0, stats.getLeased());
    Assertions.assertEquals(0, stats.getPending());
    pool.closeIdle(TimeValue.of(-1, TimeUnit.MILLISECONDS));
    Mockito.verify(conn2).close(CloseMode.GRACEFUL);
    totals = pool.getTotalStats();
    Assertions.assertEquals(0, totals.getAvailable());
    Assertions.assertEquals(0, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    stats = pool.getStats("somehost");
    Assertions.assertEquals(0, 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 4 with StrictConnPool

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

the class TestStrictConnPool method testLeaseRequestTimeout.

@Test
public void testLeaseRequestTimeout() throws Exception {
    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(1, 1);
    final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null, Timeout.ofMilliseconds(0), null);
    final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null, Timeout.ofMilliseconds(0), null);
    final Future<PoolEntry<String, HttpConnection>> future3 = pool.lease("somehost", null, Timeout.ofMilliseconds(10), null);
    Assertions.assertTrue(future1.isDone());
    final PoolEntry<String, HttpConnection> entry1 = future1.get();
    Assertions.assertNotNull(entry1);
    entry1.assignConnection(conn1);
    Assertions.assertFalse(future2.isDone());
    Assertions.assertFalse(future3.isDone());
    Thread.sleep(100);
    pool.validatePendingRequests();
    Assertions.assertFalse(future2.isDone());
    Assertions.assertTrue(future3.isDone());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 5 with StrictConnPool

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

the class TestStrictConnPool method testConnectionRedistributionOnTotalMaxLimit.

@Test
public void testConnectionRedistributionOnTotalMaxLimit() throws Exception {
    final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn3 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn4 = Mockito.mock(HttpConnection.class);
    final HttpConnection conn5 = Mockito.mock(HttpConnection.class);
    final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 10);
    pool.setMaxPerRoute("somehost", 2);
    pool.setMaxPerRoute("otherhost", 2);
    pool.setMaxTotal(2);
    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 Future<PoolEntry<String, HttpConnection>> future4 = pool.lease("otherhost", null);
    Assertions.assertTrue(future1.isDone());
    final PoolEntry<String, HttpConnection> entry1 = future1.get();
    Assertions.assertNotNull(entry1);
    Assertions.assertFalse(entry1.hasConnection());
    entry1.assignConnection(conn1);
    Assertions.assertTrue(future2.isDone());
    final PoolEntry<String, HttpConnection> entry2 = future2.get();
    Assertions.assertNotNull(entry2);
    Assertions.assertFalse(entry2.hasConnection());
    entry2.assignConnection(conn2);
    Assertions.assertFalse(future3.isDone());
    Assertions.assertFalse(future4.isDone());
    PoolStats totals = pool.getTotalStats();
    Assertions.assertEquals(0, totals.getAvailable());
    Assertions.assertEquals(2, totals.getLeased());
    Assertions.assertEquals(2, totals.getPending());
    pool.release(entry1, true);
    pool.release(entry2, true);
    Assertions.assertTrue(future3.isDone());
    final PoolEntry<String, HttpConnection> entry3 = future3.get();
    Assertions.assertNotNull(entry3);
    Assertions.assertFalse(entry3.hasConnection());
    entry3.assignConnection(conn3);
    Assertions.assertTrue(future4.isDone());
    final PoolEntry<String, HttpConnection> entry4 = future4.get();
    Assertions.assertNotNull(entry4);
    Assertions.assertFalse(entry4.hasConnection());
    entry4.assignConnection(conn4);
    totals = pool.getTotalStats();
    Assertions.assertEquals(0, totals.getAvailable());
    Assertions.assertEquals(2, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    final Future<PoolEntry<String, HttpConnection>> future5 = pool.lease("somehost", null);
    final Future<PoolEntry<String, HttpConnection>> future6 = pool.lease("otherhost", null);
    pool.release(entry3, true);
    pool.release(entry4, true);
    Assertions.assertTrue(future5.isDone());
    final PoolEntry<String, HttpConnection> entry5 = future5.get();
    Assertions.assertNotNull(entry5);
    Assertions.assertFalse(entry5.hasConnection());
    entry5.assignConnection(conn5);
    Assertions.assertTrue(future6.isDone());
    final PoolEntry<String, HttpConnection> entry6 = future6.get();
    Assertions.assertNotNull(entry6);
    Assertions.assertTrue(entry6.hasConnection());
    Assertions.assertSame(conn4, entry6.getConnection());
    totals = pool.getTotalStats();
    Assertions.assertEquals(0, totals.getAvailable());
    Assertions.assertEquals(2, totals.getLeased());
    Assertions.assertEquals(0, totals.getPending());
    pool.release(entry5, true);
    pool.release(entry6, true);
    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