Connection pool in hyper client

I use hyper::client in my program for redirecting requests to a back-end server. In the description it says that " A pool of existing connections, allowing better performance when making multiple requests to the same hostname."
But when I check open connections in the server, I see several connections with my back-end.
(I create keep-alive connection requests.)

Can you please tell what is wrong in my understanding?

It will start a new connection if all existing ones are already in use for an ongoing request.

Thanks.
what does it mean when you say if all existing ones are already in use for an ongoing request?
It means that it creates several connection pools and when they use their full capacity then it creates a new one?

If you make one request and then start another before the first is finished, a second connection is opened since the first cannot be reused until its request has finished.

1 Like

Presumably up to some limit?

A connection pool is a group of connections. When you ask Hyper to make a request, it will first look in the connection pool for a connection that it can reuse. If there's no connection available for reuse, then Hyper will create a new connection and use that for the request.

After the request finishes using the connection, Hyper will check to see if the connection can be reused; if it can be reused, the connection will be put into the connection pool for a later request to reuse.

This means that if you have code that does:

async fn do_many_gets(client: &hyper::client::Client) -> Result<(), Error> {
    handle_response(client.get(URI).await?);
    handle_response(client.get(URI).await?);
    handle_response(client.get(URI).await?);
    Ok(())
}

then Hyper will make one connection to the server specified by URI (in the successful case, where the server supports connection reuse), and reuse it for each subsequent request. You thus get 3 requests on a single connection.

If we change the code to look like:

async fn do_many_gets(client: &hyper::client::Client) -> Result<(), Error> {
    handle_response_pair(try_join(client.get(URI), client.get(URI)).await?);
    handle_response_pair(try_join(client.get(URI), client.get(URI)).await?);
    handle_response_pair(try_join(client.get(URI), client.get(URI)).await?);
    Ok(())
}

Hyper will now make 2 connections to the server for the first pair of concurrent requests, but will reuse both connections (in the successful reuse case) for the second and third pair of concurrent requests. You thus get 2 connections to the server, each of which is used for 3 requests.

This means that in real code, the number of connections to a given host is bounded by the number of concurrent requests you're making to that host, but sequential requests can reuse existing connections from the connection pool.

1 Like

I understand HTTP 1.1 pipelining could be problematic with the head blocking, but HTTP 2 fully supports proper multiplexing, right? So shouldn't Hyper reuse a single TCP connection for an unlimited (or nearly) amount of concurrent, ongoing requests?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.