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());
}
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);
}
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");
}
}
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());
}
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());
}
Aggregations