hyper_util/client/legacy/connect/
capture.rs

1use std::{ops::Deref, sync::Arc};
2
3use http::Request;
4use tokio::sync::watch;
5
6use super::Connected;
7
8/// [`CaptureConnection`] allows callers to capture [`Connected`] information
9///
10/// To capture a connection for a request, use [`capture_connection`].
11#[derive(Debug, Clone)]
12pub struct CaptureConnection {
13    rx: watch::Receiver<Option<Connected>>,
14}
15
16/// Capture the connection for a given request
17///
18/// When making a request with Hyper, the underlying connection must implement the [`Connection`] trait.
19/// [`capture_connection`] allows a caller to capture the returned [`Connected`] structure as soon
20/// as the connection is established.
21///
22/// [`Connection`]: crate::client::legacy::connect::Connection
23///
24/// *Note*: If establishing a connection fails, [`CaptureConnection::connection_metadata`] will always return none.
25///
26/// # Examples
27///
28/// **Synchronous access**:
29/// The [`CaptureConnection::connection_metadata`] method allows callers to check if a connection has been
30/// established. This is ideal for situations where you are certain the connection has already
31/// been established (e.g. after the response future has already completed).
32/// ```rust
33/// use hyper_util::client::legacy::connect::capture_connection;
34/// let mut request = http::Request::builder()
35///   .uri("http://foo.com")
36///   .body(())
37///   .unwrap();
38///
39/// let captured_connection = capture_connection(&mut request);
40/// // some time later after the request has been sent...
41/// let connection_info = captured_connection.connection_metadata();
42/// println!("we are connected! {:?}", connection_info.as_ref());
43/// ```
44///
45/// **Asynchronous access**:
46/// The [`CaptureConnection::wait_for_connection_metadata`] method returns a future resolves as soon as the
47/// connection is available.
48///
49/// ```rust
50/// # #[cfg(feature  = "tokio")]
51/// # async fn example() {
52/// use hyper_util::client::legacy::connect::capture_connection;
53/// use hyper_util::client::legacy::Client;
54/// use hyper_util::rt::TokioExecutor;
55/// use bytes::Bytes;
56/// use http_body_util::Empty;
57/// let mut request = http::Request::builder()
58///   .uri("http://foo.com")
59///   .body(Empty::<Bytes>::new())
60///   .unwrap();
61///
62/// let mut captured = capture_connection(&mut request);
63/// tokio::task::spawn(async move {
64///     let connection_info = captured.wait_for_connection_metadata().await;
65///     println!("we are connected! {:?}", connection_info.as_ref());
66/// });
67///
68/// let client = Client::builder(TokioExecutor::new()).build_http();
69/// client.request(request).await.expect("request failed");
70/// # }
71/// ```
72pub fn capture_connection<B>(request: &mut Request<B>) -> CaptureConnection {
73    let (tx, rx) = CaptureConnection::new();
74    request.extensions_mut().insert(tx);
75    rx
76}
77
78/// TxSide for [`CaptureConnection`]
79///
80/// This is inserted into `Extensions` to allow Hyper to back channel connection info
81#[derive(Clone)]
82pub(crate) struct CaptureConnectionExtension {
83    tx: Arc<watch::Sender<Option<Connected>>>,
84}
85
86impl CaptureConnectionExtension {
87    pub(crate) fn set(&self, connected: &Connected) {
88        self.tx.send_replace(Some(connected.clone()));
89    }
90}
91
92impl CaptureConnection {
93    /// Internal API to create the tx and rx half of [`CaptureConnection`]
94    pub(crate) fn new() -> (CaptureConnectionExtension, Self) {
95        let (tx, rx) = watch::channel(None);
96        (
97            CaptureConnectionExtension { tx: Arc::new(tx) },
98            CaptureConnection { rx },
99        )
100    }
101
102    /// Retrieve the connection metadata, if available
103    pub fn connection_metadata(&self) -> impl Deref<Target = Option<Connected>> + '_ {
104        self.rx.borrow()
105    }
106
107    /// Wait for the connection to be established
108    ///
109    /// If a connection was established, this will always return `Some(...)`. If the request never
110    /// successfully connected (e.g. DNS resolution failure), this method will never return.
111    pub async fn wait_for_connection_metadata(
112        &mut self,
113    ) -> impl Deref<Target = Option<Connected>> + '_ {
114        if self.rx.borrow().is_some() {
115            return self.rx.borrow();
116        }
117        let _ = self.rx.changed().await;
118        self.rx.borrow()
119    }
120}
121
122#[cfg(all(test, not(miri)))]
123mod test {
124    use super::*;
125
126    #[test]
127    fn test_sync_capture_connection() {
128        let (tx, rx) = CaptureConnection::new();
129        assert!(
130            rx.connection_metadata().is_none(),
131            "connection has not been set"
132        );
133        tx.set(&Connected::new().proxy(true));
134        assert!(rx
135            .connection_metadata()
136            .as_ref()
137            .expect("connected should be set")
138            .is_proxied());
139
140        // ensure it can be called multiple times
141        assert!(rx
142            .connection_metadata()
143            .as_ref()
144            .expect("connected should be set")
145            .is_proxied());
146    }
147
148    #[tokio::test]
149    async fn async_capture_connection() {
150        let (tx, mut rx) = CaptureConnection::new();
151        assert!(
152            rx.connection_metadata().is_none(),
153            "connection has not been set"
154        );
155        let test_task = tokio::spawn(async move {
156            assert!(rx
157                .wait_for_connection_metadata()
158                .await
159                .as_ref()
160                .expect("connection should be set")
161                .is_proxied());
162            // can be awaited multiple times
163            assert!(
164                rx.wait_for_connection_metadata().await.is_some(),
165                "should be awaitable multiple times"
166            );
167
168            assert!(rx.connection_metadata().is_some());
169        });
170        // can't be finished, we haven't set the connection yet
171        assert!(!test_task.is_finished());
172        tx.set(&Connected::new().proxy(true));
173
174        assert!(test_task.await.is_ok());
175    }
176
177    #[tokio::test]
178    async fn capture_connection_sender_side_dropped() {
179        let (tx, mut rx) = CaptureConnection::new();
180        assert!(
181            rx.connection_metadata().is_none(),
182            "connection has not been set"
183        );
184        drop(tx);
185        assert!(rx.wait_for_connection_metadata().await.is_none());
186    }
187}