Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The usage of preCreateConnections method of AbstractConnectionPool #6512

Closed
dwen77 opened this issue Jul 9, 2021 · 4 comments
Closed

The usage of preCreateConnections method of AbstractConnectionPool #6512

dwen77 opened this issue Jul 9, 2021 · 4 comments
Labels

Comments

@dwen77
Copy link

dwen77 commented Jul 9, 2021

Jetty version

org.eclipse.jetty:jetty-client:9.4.36.v20210114

Java version

openjdk version "11.0.2" 2019-01-15

Question
Hi people,

I have an application which uses Jetty client to communicate with a bunch of other systems. The first request or a few concurrent requests to the application normally get slow response since it takes time to create connections toward other systems, especially the SSL handshakes.

I have an idea to invoke preCreateConnections method of AbstractConnectionPool class during application startup phase in order to pre-create a number of connections which are warmed up already before application start to serve any incoming request. However, I did some experiments and realize the connections can only be created when httpclient sending the first request. My question is if there is a way to achieve my goal that is described above or there must be a first request to trigger the precreation of connections. Thanks!

Below is the code snippet that shows the usage of preCreateConnections

        HttpClientTransport transport = httpClient.getTransport();
        transport.setConnectionPoolFactory(destination -> {
            final DuplexConnectionPool duplexConnectionPool = new DuplexConnectionPool(
                    destination,
                    httpClient.getMaxConnectionsPerDestination(),
                    destination);
            try {
                duplexConnectionPool.preCreateConnections(preCreateConnectionCount)
                        .handle((unused, throwable) -> {
                            if (throwable != null) {
                                LOGGER.warn("failed to pre-create connections due to {} ", throwable.getMessage());
                            } else {
                                LOGGER.info("pre-create {} connections successfully", preCreateConnectionCount);
                            }
                            return null;
                        })
                        .get(10, TimeUnit.SECONDS);
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                LOGGER.warn("failed to pre-create connections due to {} ", e.getMessage());
            }
            return duplexConnectionPool;
        });
@dwen77 dwen77 added the Question label Jul 9, 2021
@joakime
Copy link
Contributor

joakime commented Jul 9, 2021

I would think the preCreateConnections should be part of the connection pool factory impl.
And setup before you call httpClient.start();

Like you see in the test cases - https://github.com/eclipse/jetty.project/blob/jetty-9.4.43.v20210629/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java#L1866-L1873

https://github.com/eclipse/jetty.project/blob/526006ecfa3af7f1a27ef3a288e2bef7ea9dd7e8/jetty-client/src/test/java/org/eclipse/jetty/client/HttpClientTest.java#L1866-L1873

@sbordet
Copy link
Contributor

sbordet commented Jul 9, 2021

Method preCreateConnections(int) creates the given number of connections. To what server address though?

You have to tell HttpClient at least one destination for the ConnectionPool to be instantiated and the connections pre-created.

Use HttpClient.resolveDestination(...) to resolve the destination you want to pre-create connections for -- you don't need to send a request.
Sending a request would first resolve the destination (which would pre-create the connections), then send the request.

@dwen77
Copy link
Author

dwen77 commented Jul 9, 2021

Thanks to both of you! Great answers!
It makes perfect sense to tell the HttpClient about the destinations. Let me try to use HttpClient.resolveDestination(...)!

@dwen77
Copy link
Author

dwen77 commented Jul 9, 2021

The suggested approach works like a charm, let me close the ticket and thanks again for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants