Hyper Connection Pool Example


#1

Hi all, just wondering if anyone’s familiar with hyper’s connection pooling for client requests.

I can’t seem to find any docs on it?

EDIT: So it looks like it’s actually enabled by default, but I’m not sure how to go about configuring it for a pool of more than 1.


Hyper / Reqwest: Connection not being kept alive?
#2

Looks like there’s Client::with_pool_config, which may be useful.


#3

Note the documentation. The number of threads is per host, 1 is definitely a reasonable default here and it shouldn’t be tuned too high.


#4

Hmm, so it looks like it’s basically all wired up out of the box. Maybe I’ll just add a keep alive header to a request and see what happens.

EDIT: Ok so it worked. Kudos to hyper. Adding a keep_alive header to a request did just work. In a test benchmark:

//With keep_alive
took mean 369578ns

//Without keep_alive
took mean 874433ns

Thanks for the help :slight_smile:


#5

I am wondering how do you do that? Would you mind sharing the code with keep_alive enabled?


#6

Sure, you can specify keep_alive using the Connection header:

let client = hyper::Client::new();

let response = client
    .get("http://some-host:8080")
    .header(hyper::header::Connection::keep_alive())
    .send();

#7

Thank you!


#8

No problem :slight_smile: Also make sure you use the same hyper::Client instance to ensure those connections stay open across requests.