Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zino23 opened this issue Jan 20, 2025 · 2 comments · May be fixed by hyperium/hyper-util#163
Open

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

zino23 opened this issue Jan 20, 2025 · 2 comments · May be fixed by hyperium/hyper-util#163
Labels
A-client Area: client. C-feature Category: feature. This is adding a new feature. K-hyper-util Crate: hyper-util

Comments

@zino23
Copy link

zino23 commented Jan 20, 2025

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

@zino23 zino23 added the C-feature Category: feature. This is adding a new feature. label Jan 20, 2025
@seanmonstar seanmonstar added A-client Area: client. K-hyper-util Crate: hyper-util labels Jan 20, 2025
@seanmonstar
Copy link
Member

See also #3727, might be the same, might be slightly different.

@zino23
Copy link
Author

zino23 commented Jan 21, 2025

See also #3727, might be the same, might be slightly different.

Since hyper_util::client::legacy::Error also contains Connected and exposes it via the connect_info method, I guess hyperium/hyper-util#163 also solves the problem of #3727?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-client Area: client. C-feature Category: feature. This is adding a new feature. K-hyper-util Crate: hyper-util
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants