Search in sources :

Example 6 with PoolStats

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

the class TestLaxConnPool 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 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);
    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 7 with PoolStats

use of org.apache.hc.core5.pool.PoolStats 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)

Example 8 with PoolStats

use of org.apache.hc.core5.pool.PoolStats 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 9 with PoolStats

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

the class AsyncReverseProxyExample method main.

public static void main(final String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Usage: <hostname[:port]> [listener port] [--quiet]");
        System.exit(1);
    }
    // Target host
    final HttpHost targetHost = HttpHost.create(args[0]);
    int port = 8080;
    if (args.length > 1) {
        port = Integer.parseInt(args[1]);
    }
    for (final String s : args) {
        if ("--quiet".equalsIgnoreCase(s)) {
            quiet = true;
            break;
        }
    }
    println("Reverse proxy to " + targetHost);
    final IOReactorConfig config = IOReactorConfig.custom().setSoTimeout(1, TimeUnit.MINUTES).build();
    final HttpAsyncRequester requester = AsyncRequesterBootstrap.bootstrap().setIOReactorConfig(config).setConnPoolListener(new ConnPoolListener<HttpHost>() {

        @Override
        public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
            final StringBuilder buf = new StringBuilder();
            buf.append("[proxy->origin] connection leased ").append(route);
            println(buf.toString());
        }

        @Override
        public void onRelease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
            final StringBuilder buf = new StringBuilder();
            buf.append("[proxy->origin] connection released ").append(route);
            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());
            println(buf.toString());
        }
    }).setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
        // empty
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
        // empty
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            println("[proxy<-origin] connection " + connection.getLocalAddress() + "->" + connection.getRemoteAddress() + (keepAlive ? " kept alive" : " cannot be kept alive"));
        }
    }).setMaxTotal(100).setDefaultMaxPerRoute(20).create();
    final HttpAsyncServer server = AsyncServerBootstrap.bootstrap().setIOReactorConfig(config).setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
        // empty
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
        // empty
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            println("[client<-proxy] connection " + connection.getLocalAddress() + "->" + connection.getRemoteAddress() + (keepAlive ? " kept alive" : " cannot be kept alive"));
        }
    }).register("*", () -> new IncomingExchangeHandler(targetHost, requester)).create();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        println("Reverse proxy shutting down");
        server.close(CloseMode.GRACEFUL);
        requester.close(CloseMode.GRACEFUL);
    }));
    requester.start();
    server.start();
    server.listen(new InetSocketAddress(port), URIScheme.HTTP);
    println("Listening on port " + port);
    server.awaitShutdown(TimeValue.MAX_VALUE);
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) HttpConnection(org.apache.hc.core5.http.HttpConnection) InetSocketAddress(java.net.InetSocketAddress) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) AsyncClientEndpoint(org.apache.hc.core5.http.nio.AsyncClientEndpoint) PoolStats(org.apache.hc.core5.pool.PoolStats) ConnPoolStats(org.apache.hc.core5.pool.ConnPoolStats) IOReactorConfig(org.apache.hc.core5.reactor.IOReactorConfig) HttpAsyncServer(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer) HttpHost(org.apache.hc.core5.http.HttpHost) HttpAsyncRequester(org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester)

Example 10 with PoolStats

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

the class ClassicReverseProxyExample method main.

public static void main(final String[] args) throws Exception {
    if (args.length < 1) {
        System.out.println("Usage: <hostname[:port]> [listener port]");
        System.exit(1);
    }
    final HttpHost targetHost = HttpHost.create(args[0]);
    int port = 8080;
    if (args.length > 1) {
        port = Integer.parseInt(args[1]);
    }
    System.out.println("Reverse proxy to " + targetHost);
    final HttpRequester requester = RequesterBootstrap.bootstrap().setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
            System.out.println("[proxy->origin] " + Thread.currentThread() + " " + request.getMethod() + " " + request.getRequestUri());
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
            System.out.println("[proxy<-origin] " + Thread.currentThread() + " status " + response.getCode());
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            System.out.println("[proxy<-origin] " + Thread.currentThread() + " exchange completed; " + "connection " + (keepAlive ? "kept alive" : "cannot be kept alive"));
        }
    }).setConnPoolListener(new ConnPoolListener<HttpHost>() {

        @Override
        public void onLease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
            final StringBuilder buf = new StringBuilder();
            buf.append("[proxy->origin] ").append(Thread.currentThread()).append(" connection leased ").append(route);
            System.out.println(buf);
        }

        @Override
        public void onRelease(final HttpHost route, final ConnPoolStats<HttpHost> connPoolStats) {
            final StringBuilder buf = new StringBuilder();
            buf.append("[proxy->origin] ").append(Thread.currentThread()).append(" connection released ").append(route);
            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());
            System.out.println(buf);
        }
    }).create();
    final HttpServer server = ServerBootstrap.bootstrap().setListenerPort(port).setStreamListener(new Http1StreamListener() {

        @Override
        public void onRequestHead(final HttpConnection connection, final HttpRequest request) {
            System.out.println("[client->proxy] " + Thread.currentThread() + " " + request.getMethod() + " " + request.getRequestUri());
        }

        @Override
        public void onResponseHead(final HttpConnection connection, final HttpResponse response) {
            System.out.println("[client<-proxy] " + Thread.currentThread() + " status " + response.getCode());
        }

        @Override
        public void onExchangeComplete(final HttpConnection connection, final boolean keepAlive) {
            System.out.println("[client<-proxy] " + Thread.currentThread() + " exchange completed; " + "connection " + (keepAlive ? "kept alive" : "cannot be kept alive"));
        }
    }).setExceptionListener(new ExceptionListener() {

        @Override
        public void onError(final Exception ex) {
            if (ex instanceof SocketException) {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
            } else {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
                ex.printStackTrace(System.out);
            }
        }

        @Override
        public void onError(final HttpConnection connection, final Exception ex) {
            if (ex instanceof SocketTimeoutException) {
                System.out.println("[client->proxy] " + Thread.currentThread() + " time out");
            } else if (ex instanceof SocketException || ex instanceof ConnectionClosedException) {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
            } else {
                System.out.println("[client->proxy] " + Thread.currentThread() + " " + ex.getMessage());
                ex.printStackTrace(System.out);
            }
        }
    }).register("*", new ProxyHandler(targetHost, requester)).create();
    server.start();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        server.close(CloseMode.GRACEFUL);
        requester.close(CloseMode.GRACEFUL);
    }));
    System.out.println("Listening on port " + port);
    server.awaitTermination(TimeValue.MAX_VALUE);
}
Also used : BasicClassicHttpRequest(org.apache.hc.core5.http.message.BasicClassicHttpRequest) ClassicHttpRequest(org.apache.hc.core5.http.ClassicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) SocketException(java.net.SocketException) HttpConnection(org.apache.hc.core5.http.HttpConnection) Http1StreamListener(org.apache.hc.core5.http.impl.Http1StreamListener) ConnPoolStats(org.apache.hc.core5.pool.ConnPoolStats) ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) HttpException(org.apache.hc.core5.http.HttpException) IOException(java.io.IOException) ConnectionClosedException(org.apache.hc.core5.http.ConnectionClosedException) PoolStats(org.apache.hc.core5.pool.PoolStats) ConnPoolStats(org.apache.hc.core5.pool.ConnPoolStats) SocketTimeoutException(java.net.SocketTimeoutException) HttpHost(org.apache.hc.core5.http.HttpHost) ConnPoolListener(org.apache.hc.core5.pool.ConnPoolListener) HttpServer(org.apache.hc.core5.http.impl.bootstrap.HttpServer) ExceptionListener(org.apache.hc.core5.http.ExceptionListener) HttpRequester(org.apache.hc.core5.http.impl.bootstrap.HttpRequester)

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