Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient is not re-using my connections, keeps creating new ones

From netstat -anp | grep I at two different times, I can see HttpClient is not re-using my connections. I connect to the same host every time with different urls such as

  • https://myhost.com/postkey12345
  • https://myhost.com/postkey54321

Why is HttpClient not just re-using the same connection to myhost.com every time?

I have the following code for intialization of the Pool that I pass to HttpClient...

PoolingClientConnectionManager mgr = new PoolingClientConnectionManager();
mgr.setDefaultMaxPerRoute(30);
mgr.setMaxTotal(30);

I see they are using an HttpRoute and have localAddress in the equals which seems odd as HttpRoute should be equal on just hostname and schema(https/http) and that is it, right? Does that have something to do with it? Is this a bug? Performance is very crappy having to re-establish https sockets every time!!!!!

like image 873
Dean Hiller Avatar asked Oct 23 '12 16:10

Dean Hiller


People also ask

Do we need to close HttpClient connection?

If you are processing HTTP responses manually instead of using a response handler, you need to close all the http connections by yourself.

What is HttpClient connection pool?

Connection pooling is specified by the SOCKETCLOSE attribute in a URIMAP resource, which defines if, and for how long, CICS keeps a socket open after the CICS application has finished using its client HTTP connection. Applications must specify the URIMAP resource when they open the connection.

Do we need to close HttpClient in Java?

You do not need to explicitly close the HttpClient, however, (you may be doing this already but worth noting) you should ensure that connections are released after method execution. Edit: The ClientConnectionManager within the HttpClient is going to be responsible for maintaining the state of connections.

Is HttpClient multithreaded?

HttpClient is fully thread-safe when used with a thread-safe connection manager such as MultiThreadedHttpConnectionManager.

How do I close Java net HttpClient?

private void stop() { // Clears HTTP/1.1 cache and close its connections connections. stop(); // Clears HTTP/2 cache and close its connections. client2. stop(); } ... }


1 Answers

I suspect rather strongly that SSL connections established by your applications are stateful. That is, the server requested the client to authenticate with a private certificate, making them security context specific. HttpClient detects that and prevents those connections from being leased to a caller with a different security context. Effectively HttpClient is playing safe by forcing a new connection for each request rather than risking leasing persistent SSL connection to the wrong user.

You can do two things here

  • disable connection state tracking
  • make sure all logically related requests share the same context (recommended)

For details see this section of the HttpClient tutorial

like image 51
ok2c Avatar answered Sep 23 '22 17:09

ok2c