Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async http client (ning) creating more threads?

I am using the ning async http client to achieve the non-blocking goodness. Doing an apples vs apples test (non blocking vs blocking), I am seeing that the non blocking version is serving more request samples, however the async http client is creating more threads comparing to its blocking counterpart. Is this expected or something that I am missing?

Here are the numbers from the stress test

Async Http Client
jMeter - 2 threads, 120 seconds, 1 sec ramp up
Peak threads : 270
Heap usage: 600mb
Peak cpu usage: 30%
Total samples: 18228

Blocking version
jMeter - 2 threads, 120 seconds, 1 sec ramp up
Peak threads: 118
heap usage: 260mb
cpu usage: 18%
total samples: 1472

I am creating a thread pool of connections (reusing them)

AsyncHttpClientConfig.Builder builder = new AsyncHttpClientConfig.Builder();
builder.setRequestTimeoutInMs(2000);
builder.setMaximumConnectionsPerHost(10);
builder.setMaximumConnectionsTotal(100);
client = new AsyncHttpClient(builder.build());

Is there something I am missing here? I tried looking at the thread dump to see what is creating threads, but did not find anything useful. My bet is that there is a thread for each http connection being spawned to fire the callback on I/O completion in the async http client.

like image 757
Vinodh Avatar asked May 07 '12 17:05

Vinodh


People also ask

Is HttpClient asynchronous?

This example demonstrates a basic asynchronous HTTP request / response exchange. Response content is buffered in memory for simplicity. This example demonstrates an asynchronous HTTP request / response exchange with a full content streaming.

Is HttpURLConnection asynchronous?

HTTP Client API Overview Unlike HttpURLConnection, HTTP Client provides synchronous and asynchronous request mechanisms. The API consists of three core classes: HttpRequest represents the request to be sent via the HttpClient. HttpClient behaves as a container for configuration information common to multiple requests.

How does HTTP async work?

Asynchronous HTTP Request Processing is a relatively new technique that allows you to process a single HTTP request using non-blocking I/O and, if desired in separate threads. Some refer to it as COMET capabilities.

Is HTTP request is synchronous or asynchronous in Java?

It has to be made clear the HTTP protocol is synchronous and this has nothing to do with the programming language. Client sends a request and gets a synchronous response.


1 Answers

EDIT 11/16/2015

Looks like the repo moved. See this line where you can change the ThreadFactory used. If this is not set, it looks like it uses a default one. This is used by the ChannelManager here.

You can also set it on the simple client as seen here

ORIGINAL w/ dead links removed

A quick look at the code - it looks like the application thread pool uses an Executor service that is a cached thread pool, which creates threads as need. You can set the executor service that your client uses by using the setter on the builder.

like image 107
NG. Avatar answered Sep 21 '22 09:09

NG.