-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Is your feature request related to a problem? Please describe.
I was going through the hyperium/hyper web_api.rs example https://raw.githubusercontent.com/hyperium/hyper/master/examples/web_api.rs.
Looking specifically at the function below
async fn client_request_response() -> Result<Response<BoxBody>> {
...
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
...
}
when I make multiple http requests,
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
will be triggered on every function meaning establishing a connection every-time an http request is made which may not be very efficient performance wise (apologies if this is a wrong assumption)
Describe the solution you'd like
I want to be able to initialise sender & connection only once, preferably encapsulated in a client
Describe alternatives you've considered
pub struct Client {
pub sender : SendRequest<Empty<Bytes>>,
pub connection: Connection<TokioIo<TcpStream>, Empty<Bytes>>
}
so that i can initialise my client using new()/builder() function as follows:
fn new(url: Uri) -> Self {
let host =` [`url.host`](https://url.host)`().expect("uri has no host");
let port = url.port_u16().unwrap_or(80);
let address = format!("{}:{}", host, port);
let stream = TcpStream::connect(address).await?;
let io = TokioIo::new(stream);
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
tokio::task::spawn(async move {
if let Err(err) = conn.await {
println!("Connection failed: {:?}", err);
}
});
Client {
sender:sender
connection: con
}
}
The new function won't work, unless i add an async keyword behind the new function.
So my questions is:
Is there a way to establish a connection once, creating single instances of sender, conn as in
(sender, conn) = hyper::client::conn::http1::handshake(io).await?;
without using async/await ?
or would there be no performance bottleneck in running the handshake(io).await for every request ?
Additional context
Cargo.toml
[dependencies]
hyper = { version = "1.0.0-rc.4", features = ["full"] }
http-body-util = "0.1.0-rc.3"
hyper-util = { git = "https://github.com/hyperium/hyper-util.git" }
tokio = { version = "1.33.0", features = ["full"] }