h3x 0.2.0

High-performance zero-copy DHTTP/3 implementation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Reusable error-latching utilities for RPC/IPC.
//!
//! These types enforce the QUIC connection-error consistency requirement:
//! once a connection error occurs, **every** subsequent operation on the
//! same connection must observe the **same** error.
//!
//! # Design rules
//!
//! - **No eager `latch(e)` on the public surface.** Callers must always use
//!   `latch_with(|| ...)` / `guard*_with(...)` so that error construction is
//!   lazy and skipped entirely when an error is already latched.
//! - **No bare `check()?` on operation paths.** Use one of the `guard*`
//!   helpers on [`LifecycleExt`] so the check / execute / latch sequence
//!   stays in one place.
//! - **The latch is an implementation detail.** Application code never
//!   touches [`ConnectionErrorLatch`] directly — it interacts through
//!   [`LifecycleExt`] on the owning container type.
//!
//! # Design
//!
//! Container types (e.g. `RemoteConnection`, `IpcLifecycle`, session types)
//! own a [`ConnectionErrorLatch`] field and expose it to the crate through
//! the sealed [`HasLatch`] trait. Implementing `HasLatch` plus
//! [`quic::Lifecycle`] automatically gives the container all of
//! [`LifecycleExt`]'s guard/check/closed helpers via blanket impl.
//!
//! The container authors its own `impl Lifecycle` using
//! [`check_with_probe`](LifecycleExt::check_with_probe) and
//! [`resolve_closed`](LifecycleExt::resolve_closed) as building blocks;
//! operation paths use [`guard`](LifecycleExt::guard) /
//! [`guard_with`](LifecycleExt::guard_with) /
//! [`guard_sync`](LifecycleExt::guard_sync) /
//! [`guard_sync_with`](LifecycleExt::guard_sync_with) exclusively.

use std::future::Future;

use crate::{
    quic::{self, ConnectionError},
    util::set_once::SetOnce,
};

// ---------------------------------------------------------------------------
// ConnectionErrorLatch
// ---------------------------------------------------------------------------

/// First-wins connection error latch.
///
/// Cloning yields a handle to the **same** underlying state — the latch is
/// `Arc`-backed via [`SetOnce`].
///
/// This type is a crate-internal primitive. Application code interacts with
/// it only through [`LifecycleExt`] on the owning container.
#[derive(Debug, Clone, Default)]
pub struct ConnectionErrorLatch {
    terminal_error: SetOnce<ConnectionError>,
}

impl ConnectionErrorLatch {
    pub fn new() -> Self {
        Self::default()
    }

    /// Lazy latch: only calls `f` when no error is latched yet.
    ///
    /// Returns the canonical (first-wins) error.
    pub(crate) fn latch_with(&self, f: impl FnOnce() -> ConnectionError) -> ConnectionError {
        match self.terminal_error.peek() {
            Some(existing) => existing,
            None => {
                let error = f();
                let _ = self.terminal_error.set(error.clone());
                error
            }
        }
    }

    /// Crate-internal shortcut equivalent to `latch_with(|| error)`.
    ///
    /// Intended for call sites that already own a concrete [`ConnectionError`]
    /// (e.g. forwarding the terminal error out of `closed()` after the
    /// underlying future resolved).
    pub(crate) fn latch_raw(&self, error: ConnectionError) -> ConnectionError {
        self.latch_with(|| error)
    }

    /// Return the latched error, if any.
    pub(crate) fn check(&self) -> Result<(), ConnectionError> {
        match self.terminal_error.peek() {
            Some(error) => Err(error),
            None => Ok(()),
        }
    }

    /// Peek at the latched error without consuming it.
    pub(crate) fn peek(&self) -> Option<ConnectionError> {
        self.terminal_error.peek()
    }
}

// ---------------------------------------------------------------------------
// Sealed HasLatch — bridge between container types and LifecycleExt
// ---------------------------------------------------------------------------

pub(crate) mod sealed {
    use super::ConnectionErrorLatch;

    /// Crate-internal trait that lets [`super::LifecycleExt`] reach into a
    /// container's latch without making the latch a public API.
    ///
    /// Implementers must return a stable reference to the same
    /// [`ConnectionErrorLatch`] on every call.
    pub trait HasLatch {
        fn latch(&self) -> &ConnectionErrorLatch;
    }
}

pub(crate) use sealed::HasLatch;

// ---------------------------------------------------------------------------
// LifecycleExt — the only public surface for guard / check / closed
// ---------------------------------------------------------------------------

/// Extension trait layered on top of [`quic::Lifecycle`] that enforces the
/// first-wins error-latching discipline for all operation paths.
///
/// This trait is **sealed**: it is automatically implemented for any type
/// that implements both [`quic::Lifecycle`] and the crate-private
/// [`HasLatch`] marker.
///
/// Implementers typically do two things:
///
/// 1. Own a `latch: ConnectionErrorLatch` field and implement [`HasLatch`].
/// 2. Author their own `impl quic::Lifecycle`, using
///    [`check_with_probe`](Self::check_with_probe) inside `check` and
///    [`resolve_closed`](Self::resolve_closed) inside `closed`.
///
/// Callers use [`guard`](Self::guard) /
/// [`guard_with`](Self::guard_with) /
/// [`guard_sync`](Self::guard_sync) /
/// [`guard_sync_with`](Self::guard_sync_with) for operation paths. No other
/// mechanism for installing errors is exposed.
#[allow(async_fn_in_trait)]
pub trait LifecycleExt: quic::Lifecycle + HasLatch {
    /// Standard implementation of [`quic::Lifecycle::check`].
    ///
    /// Consults the latch first; if clean, invokes `probe` for any
    /// liveness signal the container wants to expose (e.g. a remoc channel
    /// being closed, or a parent lifecycle being dead). A `Some(error)`
    /// result is folded into the latch via `latch_with` so every subsequent
    /// observer sees the same canonical error.
    fn check_with_probe(
        &self,
        probe: impl FnOnce() -> Option<ConnectionError>,
    ) -> Result<(), ConnectionError> {
        self.latch().check()?;
        match probe() {
            None => Ok(()),
            Some(error) => Err(self.latch().latch_with(|| error)),
        }
    }

    /// Standard implementation of [`quic::Lifecycle::closed`].
    ///
    /// Returns the latched error immediately if one exists; otherwise awaits
    /// `wait` (the container-specific terminal-error future) and latches
    /// whatever it produces.
    async fn resolve_closed(&self, wait: impl Future<Output = ConnectionError>) -> ConnectionError {
        if let Some(error) = self.latch().peek() {
            return error;
        }
        let error = wait.await;
        self.latch().latch_raw(error)
    }

    /// Guard an async operation whose error is already a [`ConnectionError`].
    ///
    /// Checks liveness via [`quic::Lifecycle::check`] first; on success runs
    /// `fut` and lazily latches any error produced.
    async fn guard<T>(
        &self,
        fut: impl Future<Output = Result<T, ConnectionError>>,
    ) -> Result<T, ConnectionError> {
        quic::Lifecycle::check(self)?;
        match fut.await {
            Ok(v) => Ok(v),
            Err(e) => Err(self.latch().latch_with(|| e)),
        }
    }

    /// Guard an async operation whose error must be lazily converted to a
    /// [`ConnectionError`].
    ///
    /// The conversion closure `map_err` is **only** invoked if the operation
    /// errors **and** no error has been latched yet.
    async fn guard_with<T, E, M>(
        &self,
        fut: impl Future<Output = Result<T, E>>,
        map_err: M,
    ) -> Result<T, ConnectionError>
    where
        M: FnOnce(E) -> ConnectionError,
    {
        quic::Lifecycle::check(self)?;
        match fut.await {
            Ok(v) => Ok(v),
            Err(e) => Err(self.latch().latch_with(|| map_err(e))),
        }
    }

    /// Guard a synchronous fallible closure.
    ///
    /// Useful for code paths where both the pre-check and the post-op
    /// error-latching would otherwise be written by hand (e.g. FD unwrapping
    /// after an async stream-open call).
    fn guard_sync<T>(
        &self,
        f: impl FnOnce() -> Result<T, ConnectionError>,
    ) -> Result<T, ConnectionError> {
        quic::Lifecycle::check(self)?;
        match f() {
            Ok(v) => Ok(v),
            Err(e) => Err(self.latch().latch_with(|| e)),
        }
    }

    /// Like [`guard_sync`](Self::guard_sync) but with a lazy error converter.
    fn guard_sync_with<T, E, M>(
        &self,
        f: impl FnOnce() -> Result<T, E>,
        map_err: M,
    ) -> Result<T, ConnectionError>
    where
        M: FnOnce(E) -> ConnectionError,
    {
        quic::Lifecycle::check(self)?;
        match f() {
            Ok(v) => Ok(v),
            Err(e) => Err(self.latch().latch_with(|| map_err(e))),
        }
    }
}

impl<T: quic::Lifecycle + HasLatch + ?Sized> LifecycleExt for T {}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use std::{
        borrow::Cow,
        sync::{Arc, Mutex},
    };

    use super::*;
    use crate::{error::Code, varint::VarInt};

    fn make_err(tag: u32) -> ConnectionError {
        ConnectionError::Transport {
            source: quic::TransportError {
                kind: VarInt::from_u32(tag),
                frame_type: VarInt::from_u32(0),
                reason: format!("test-{tag}").into(),
            },
        }
    }

    /// Minimal container used to exercise the sealed trait.
    struct TestLifecycle {
        latch: ConnectionErrorLatch,
        probe: Mutex<Option<ConnectionError>>,
        wait: Mutex<Option<ConnectionError>>,
    }

    impl TestLifecycle {
        fn new() -> Self {
            Self {
                latch: ConnectionErrorLatch::new(),
                probe: Mutex::new(None),
                wait: Mutex::new(None),
            }
        }

        fn set_probe(&self, error: ConnectionError) {
            *self.probe.lock().unwrap() = Some(error);
        }

        fn set_wait(&self, error: ConnectionError) {
            *self.wait.lock().unwrap() = Some(error);
        }
    }

    impl HasLatch for TestLifecycle {
        fn latch(&self) -> &ConnectionErrorLatch {
            &self.latch
        }
    }

    impl quic::Lifecycle for TestLifecycle {
        fn close(&self, _code: Code, _reason: Cow<'static, str>) {}

        fn check(&self) -> Result<(), ConnectionError> {
            self.check_with_probe(|| self.probe.lock().unwrap().take())
        }

        async fn closed(&self) -> ConnectionError {
            self.resolve_closed(async {
                self.wait
                    .lock()
                    .unwrap()
                    .take()
                    .unwrap_or_else(|| make_err(99))
            })
            .await
        }
    }

    #[test]
    fn latch_with_is_lazy_when_already_latched() {
        let latch = ConnectionErrorLatch::new();
        let first = latch.latch_with(|| make_err(1));
        assert!(matches!(
            &first,
            ConnectionError::Transport { source } if source.kind == VarInt::from_u32(1)
        ));

        let called = Mutex::new(false);
        let again = latch.latch_with(|| {
            *called.lock().unwrap() = true;
            make_err(2)
        });
        assert!(
            !*called.lock().unwrap(),
            "closure must not be invoked once latched"
        );
        assert!(matches!(
            &again,
            ConnectionError::Transport { source } if source.kind == VarInt::from_u32(1)
        ));
    }

    #[test]
    fn check_with_probe_folds_probe_into_latch() {
        let lc = TestLifecycle::new();
        lc.set_probe(make_err(42));
        let e1 = quic::Lifecycle::check(&lc).unwrap_err();
        // probe now consumed — but latch remembers the error.
        let e2 = quic::Lifecycle::check(&lc).unwrap_err();
        match (&e1, &e2) {
            (
                ConnectionError::Transport { source: s1 },
                ConnectionError::Transport { source: s2 },
            ) => {
                assert_eq!(s1.kind, VarInt::from_u32(42));
                assert_eq!(s2.kind, VarInt::from_u32(42));
            }
            _ => panic!("unexpected error shape"),
        }
    }

    #[test]
    fn check_with_probe_no_error_when_clean() {
        let lc = TestLifecycle::new();
        assert!(quic::Lifecycle::check(&lc).is_ok());
    }

    #[tokio::test]
    async fn resolve_closed_returns_latched_without_awaiting() {
        let lc = TestLifecycle::new();
        lc.latch.latch_with(|| make_err(7));
        // `wait` is never polled because the latch is already set; if it
        // were, the `take()` below would come back `None`.
        let never = Mutex::new(Some(make_err(8)));
        let got = lc
            .resolve_closed(async { never.lock().unwrap().take().unwrap() })
            .await;
        assert!(matches!(
            &got,
            ConnectionError::Transport { source } if source.kind == VarInt::from_u32(7)
        ));
        assert!(never.lock().unwrap().is_some(), "wait must not be polled");
    }

    #[tokio::test]
    async fn resolve_closed_latches_wait_result() {
        let lc = TestLifecycle::new();
        lc.set_wait(make_err(5));
        let got = quic::Lifecycle::closed(&lc).await;
        assert!(matches!(
            &got,
            ConnectionError::Transport { source } if source.kind == VarInt::from_u32(5)
        ));
        // Second call must return the same error and must not hit the wait
        // future again.
        let again = quic::Lifecycle::closed(&lc).await;
        assert!(matches!(
            &again,
            ConnectionError::Transport { source } if source.kind == VarInt::from_u32(5)
        ));
    }

    #[tokio::test]
    async fn guard_with_skips_closure_when_latched() {
        let lc = TestLifecycle::new();
        lc.latch.latch_with(|| make_err(7));

        let called = Arc::new(Mutex::new(false));
        let called2 = called.clone();
        let res: Result<(), ConnectionError> = lc
            .guard_with(async { Result::<(), &'static str>::Err("x") }, move |_| {
                *called2.lock().unwrap() = true;
                make_err(8)
            })
            .await;
        assert!(res.is_err());
        assert!(
            !*called.lock().unwrap(),
            "map_err must stay lazy once latched"
        );
    }

    #[tokio::test]
    async fn guard_with_success_is_untouched() {
        let lc = TestLifecycle::new();
        let out: Result<i32, ConnectionError> = lc
            .guard_with(async { Ok::<_, &'static str>(7) }, |_| make_err(1))
            .await;
        assert_eq!(out.unwrap(), 7);
    }

    #[test]
    fn guard_sync_latches_only_first_error() {
        let lc = TestLifecycle::new();
        let a = lc.guard_sync(|| Err::<(), _>(make_err(1))).unwrap_err();
        let b = lc.guard_sync(|| Err::<(), _>(make_err(2))).unwrap_err();
        match (&a, &b) {
            (
                ConnectionError::Transport { source: sa },
                ConnectionError::Transport { source: sb },
            ) => {
                assert_eq!(sa.kind, sb.kind);
                assert_eq!(sa.kind, VarInt::from_u32(1));
            }
            _ => panic!("unexpected error shape"),
        }
    }
}