Why is Jetty Http Client so slow?

I have an HTTP gateway client configured as follows:

HttpClient client = new HttpClient(); client.setTimeout(connectionTimeout); client.setIdleTimeout(readTimeout); client.setMaxConnectionsPerAddress(100); client.setThreadPool(new QueuedThreadPool(100)); client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL); client.setMaxRetries(retries); 

and the call below always takes ~ 400 ms, which seems to last for an asynchronous call. To clarify, I'm not worried about the response time to the request. Only a method call keeps my thread busy for 400 ms. This method seems to block somewhere, but it should be asynchronous.

  client.send(httpExchange); 

Did I somehow mess up my configuration?

+4
source share
2 answers

I did not have time to verify this, but I suspect your fix

 client.setConnectBlocking(false); 

It seems that by default, HttpClient locks are blocked when connected - even if the connector does not block sending / receiving request + response.

(This assumes Jetty 7. I have not tested Jetty 8)

+4
source

I don’t know why the send() call can take so long, but if it is too slow for you, then just upload the send() call to another thread:

 public class SendThread extends Thread { private HttpClient client; private HttpExchange exchange; public SendThread(HttpClient client, HttpExchange exchange) { this.client = client; this.exchange = exchange; } @Override public void run() { client.send(exchange); } } 

Then you can do:

 new SendThread(client, exchange).start(); 

... instead of:

 client.send(httpExchange); 

If you want to try to find out why the library takes so long, you can also try reading the source code . In short, I would say that 1) what send() does is not entirely trivial, and 2) the only thing that seems asynchronous is the transfer of the actual data / payload to the server; things like making an initial connection to the server are performed synchronously as part of the send() call.

-one
source

Source: https://habr.com/ru/post/1392811/


All Articles