Skip to content

hyper-util: get info about if the connection is reused in Connected #3835

Open
hyperium/hyper-util
#163
@zino23

Description

@zino23

Is your feature request related to a problem? Please describe.
hyper_util::client::legacy::connect::capture_connection grants us the ability to capture the Connected structure after a connection has established. But we cannot tell whether the connection is reused from Connected, which is quite helpful. E.g. when debugging slow requests, whether the request established a new connection or reused a connection from the pool is an important metric.

/// Extra information about the connected transport.
///
/// This can be used to inform recipients about things like if ALPN
/// was used, or if connected to an HTTP proxy.
#[derive(Debug)]
pub struct Connected {
    pub(super) alpn: Alpn,
    pub(super) is_proxied: bool,
    pub(super) extra: Option<Extra>,
    pub(super) poisoned: PoisonPill,
}

Describe the solution you'd like
Add a field is_reused in Connected, and expose that information through a method of Connected.

#[derive(Debug)]
pub struct Connected {
    pub(super) alpn: Alpn,
    pub(super) is_proxied: bool,
    pub(super) extra: Option<Extra>,
    pub(super) poisoned: PoisonPill,
+   pub(super) is_reused: bool,
}

impl Connected {
+   /// Determines if the connection is reused.
+   pub fn is_reused(&self) -> bool {
+       self.is_reused
+   }
}

With this, we can get the connection reuse info like this:

#[tokio::test]
async fn get_conn_reuse_info() {
    use bytes::Bytes;
    use http::Request;
    use hyper_util::client::legacy::Client;
    use hyper_util::rt::TokioExecutor;

    let client = Client::builder(TokioExecutor::new()).build_http();
    let mut req = Request::builder()
        .uri("http://127.0.0.1")
        .body(http_body_util::Empty::<Bytes>::new())
        .unwrap();
    let captured = capture_connection(&mut req);
    let _ = client.request(req).await;
    assert!(!captured
        .connection_metadata()
        .as_ref()
        .is_some_and(|c| c.is_reused()));
}

Describe alternatives you've considered
N/A

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-clientArea: client.C-featureCategory: feature. This is adding a new feature.K-hyper-utilCrate: hyper-util

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions