Search in sources :

Example 11 with PoolStats

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());
    }
}
Also used : PoolStats(org.apache.hc.core5.pool.PoolStats) ConnPoolStats(org.apache.hc.core5.pool.ConnPoolStats)

Example 12 with PoolStats

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());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 13 with PoolStats

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());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 14 with PoolStats

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());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Example 15 with PoolStats

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());
}
Also used : HttpConnection(org.apache.hc.core5.http.HttpConnection) Test(org.junit.jupiter.api.Test)

Aggregations

HttpConnection (org.apache.hc.core5.http.HttpConnection)14 Test (org.junit.jupiter.api.Test)12 PoolStats (org.apache.hc.core5.pool.PoolStats)5 ConnPoolStats (org.apache.hc.core5.pool.ConnPoolStats)4 HttpHost (org.apache.hc.core5.http.HttpHost)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 Http1StreamListener (org.apache.hc.core5.http.impl.Http1StreamListener)2 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)1 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)1 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)1 PoolingHttpClientConnectionManager (org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager)1 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)1 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)1 ConnectionClosedException (org.apache.hc.core5.http.ConnectionClosedException)1 ExceptionListener (org.apache.hc.core5.http.ExceptionListener)1