use of org.apache.hc.core5.pool.PoolStats in project httpcomponents-core by apache.
the class LoggingConnPoolListener method onLease.
@Override
public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
if (connLog.isDebugEnabled()) {
final StringBuilder buf = new StringBuilder();
buf.append("Leased ").append(route).append(" ");
final PoolStats totals = connPoolStats.getTotalStats();
buf.append(" total kept alive: ").append(totals.getAvailable()).append("; ");
buf.append("total allocated: ").append(totals.getLeased() + totals.getAvailable());
buf.append(" of ").append(totals.getMax());
connLog.debug(buf.toString());
}
}
use of org.apache.hc.core5.pool.PoolStats in project httpcomponents-core by apache.
the class TestLaxConnPool method testCloseIdle.
@Test
public void testCloseIdle() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final HttpConnection conn2 = Mockito.mock(HttpConnection.class);
final LaxConnPool<String, HttpConnection> pool = new LaxConnPool<>(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());
Assertions.assertFalse(pool.isShutdown());
}
use of org.apache.hc.core5.pool.PoolStats 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.PoolStats 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());
}
use of org.apache.hc.core5.pool.PoolStats in project httpcomponents-core by apache.
the class TestStrictConnPool method testCreateNewIfExpired.
@Test
public void testCreateNewIfExpired() throws Exception {
final HttpConnection conn1 = Mockito.mock(HttpConnection.class);
final StrictConnPool<String, HttpConnection> pool = new StrictConnPool<>(2, 2);
final Future<PoolEntry<String, HttpConnection>> future1 = pool.lease("somehost", null);
Assertions.assertTrue(future1.isDone());
final PoolEntry<String, HttpConnection> entry1 = future1.get();
Assertions.assertNotNull(entry1);
entry1.assignConnection(conn1);
entry1.updateExpiry(TimeValue.of(1, TimeUnit.MILLISECONDS));
pool.release(entry1, true);
Thread.sleep(200L);
final Future<PoolEntry<String, HttpConnection>> future2 = pool.lease("somehost", null);
Assertions.assertTrue(future2.isDone());
Mockito.verify(conn1).close(CloseMode.GRACEFUL);
final PoolStats totals = pool.getTotalStats();
Assertions.assertEquals(0, totals.getAvailable());
Assertions.assertEquals(1, totals.getLeased());
Assertions.assertEquals(Collections.singleton("somehost"), pool.getRoutes());
final PoolStats stats = pool.getStats("somehost");
Assertions.assertEquals(0, stats.getAvailable());
Assertions.assertEquals(1, stats.getLeased());
}
Aggregations