hyperium / hyperium/hyper

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

Open
#3,835 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-client C-feature K-hyper-util
Dominant language
Rust
Stars
16.3k
Forks
1.8k
Avg merge
1d 22h
Merged PRs (30d)
14

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at hyper_util::client::legacy::connect::Connected and capture_connection, then trace where newly established and pooled connections create or update Connected. Add the reuse information and accessor described in the issue, and verify it with the provided get_conn_reuse_info scenario, ensuring the result distinguishes a first request from a reused connection.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
networking
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.