Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does HttpAsyncClient 4 work?

In previous versions of HttpClient target host was set up into client itself. In last version (for HttpAsyncClient it's 4.1.1) host is set up into HttpRequest (HttpGet, HttpPost etc.) every time I do a request.

I want to use persistent connection, so I use HttpAsyncClient. I create and use it like this:

CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
client.start();
List<Future<HttpResponse>> responses = new ArrayList<>();
for (int i = 0; i < 10; i++)
{
    HttpGet get = new HttpGet("https://google.com/");
    responses.add(client.execute(get, null));
}
for (Future<HttpResponse> response : responses) {
    response.get(); //wait for the response
}

As I tested, it works faster than usual HttpClient (if I do all the requests, and then wait for all the responses).

But I can't fully understand, how it works inside. How many connections with https://google.com/ are established? What happens if I use client for one host, and then for another? (as I tested, responses can come in any order, so I suppose there are at least 2 connections in parallel). What's the difference between HttpAsyncClients.createDefault() and HttpAsyncClients.createPipelining() ?

Thanks!

like image 613
coolguy Avatar asked Dec 29 '15 05:12

coolguy


1 Answers

By default HttpAsyncClient permits only two concurrent connections to the same host per RFC 2616 specification. This limit has nothing to do with the number of i/o dispatch threads used internally by the i/o reactor.

The code above will create two outgoing connections at most.

HTTP message pipelining has nothing do with connection persistence per se, though pipelined request execution implies the use of persistent connections.

HTTP pipelining is about message sequencing. HttpAsyncClient in the pipelining mode can send multiple requests without waiting for each response.

Default mode:

C -> request1 -> S
C <- response1 <- S
C -> request2 -> S
C <- response2 <- S

Pipelining mode:

C -> request1 -> S
C -> request2 -> S
C <- response1 <- S
C <- response2 <- S
like image 132
ok2c Avatar answered Nov 13 '22 03:11

ok2c