eggfetch-core 0.3.0

Async-first Rust HTTP client engine (tokio + hyper) with connection pooling, phase-aware timeouts, TLS, proxies, and compression
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Transport observability counters.
//!
//! [`TransportMetrics`] counts connector and protocol events where the
//! engine can observe them reliably. It is deliberately separate from
//! [`crate::pool::PoolMetrics`], which counts logical pool-permit
//! waits/cancellations.
//!
//! # What is (and is not) counted
//!
//! - **Connector events**: `*_connector_attempts/successes/failures`,
//!   `*_dns_attempts/failures`, `*_tls_attempts/failures`,
//!   `proxy_*`, `uds_*`. One increment per connector `call` or per
//!   DNS/TLS phase within that call.
//! - **Protocol connections**: `h3_connections_created`,
//!   `upgraded_connections_created`. One per established QUIC/H3 session
//!   or captured 101 upgrade.
//! - **Alt-Svc discovery** (`http3`): `altsvc_learned/expired/cleared/rejected`,
//!   `h3_route_attempted/suppressed`, `h3_fallback_selected`,
//!   `h3_drain_observed/closed/reconnected`. One per routing decision or
//!   cache transition; no secrets, no URLs, only counts.
//! - **Logical requests** are counted by `PoolMetrics`, not here.
//! - **Physical lifecycle**: admission waits/timeouts, current live
//!   connections, and the live high-water mark are counted at the common
//!   Hyper connector boundary. Established read/write inactivity timeouts
//!   are counted when the lifecycle wrapper returns them.
//!
//! Hyper's internal socket-reuse counts and per-connection H2 stream
//! counts are intentionally absent: the legacy pool owns socket lifecycle
//! and eggfetch cannot observe reuse reliably. No estimate is reported.
//!
//! The counters are `AtomicUsize` with `Relaxed` ordering (low overhead,
//! concurrency-safe). H3 diagnostic snapshots use a private bounded mutex
//! because they are copied value records rather than counters. Tests assert
//! exact counts in deterministic local scenarios (loopback listeners,
//! blackholed UDP for H3).

use std::sync::atomic::{AtomicUsize, Ordering};

#[cfg(feature = "http3")]
use std::net::{IpAddr, SocketAddr};
#[cfg(feature = "http3")]
use std::sync::Mutex;
#[cfg(feature = "http3")]
use std::time::Duration;

/// The H3 route that established a diagnostic snapshot.
#[cfg(feature = "http3")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum H3RouteKind {
    /// An explicit `Http3Only` direct route.
    Explicit,
    /// An authenticated Alt-Svc-discovered route.
    AltSvc,
}

/// Sanitized classification of a QUIC connection close.
#[cfg(feature = "http3")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum H3CloseKind {
    /// The peer did not support a negotiated QUIC version.
    VersionMismatch,
    /// QUIC transport error; `code` contains the protocol code.
    TransportError,
    /// The peer aborted the QUIC connection; `code` contains the transport code.
    ConnectionClosed,
    /// An HTTP/3 application close; `code` contains the application code.
    ApplicationClosed,
    /// The peer reset the connection without a close code.
    Reset,
    /// The negotiated idle timeout elapsed.
    TimedOut,
    /// The local application closed the connection.
    LocallyClosed,
    /// The connection exhausted its connection-ID space.
    CidsExhausted,
}

/// A bounded, connection-level H3 diagnostic snapshot.
///
/// These values are copied from Quinn and intentionally contain no origin
/// hostname, request headers, cookies, bodies, or peer-provided close reason.
/// `open_streams` is `None` because the pinned Quinn API does not expose a
/// reliable H3 stream count for a reused multiplexed connection.
#[cfg(feature = "http3")]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct H3ConnectionDiagnostic {
    /// Quinn's stable identifier for this connection.
    pub stable_id: usize,
    /// Last selected remote UDP address observed by Quinn.
    pub remote_address: SocketAddr,
    /// Local address information when the platform exposes it.
    pub local_ip: Option<IpAddr>,
    /// Whether the route was explicit or Alt-Svc discovered.
    pub route: H3RouteKind,
    /// Alt-Svc generation, or `None` for an explicit route.
    pub alt_svc_generation: Option<u64>,
    /// Quinn's current smoothed RTT at snapshot time.
    pub rtt: Duration,
    /// QUIC packets sent on the current path.
    pub sent_packets: u64,
    /// UDP datagrams received on this connection.
    pub received_datagrams: u64,
    /// QUIC packets declared lost on the current path.
    pub lost_packets: u64,
    /// QUIC bytes sent in UDP datagrams.
    pub sent_bytes: u64,
    /// QUIC bytes received in UDP datagrams.
    pub received_bytes: u64,
    /// QUIC bytes declared lost on the current path.
    pub lost_bytes: u64,
    /// H3/QUIC close classification, available after the connection closes.
    pub close: Option<H3CloseSummary>,
    /// H3 stream count is not exposed by the pinned Quinn/h3 APIs.
    pub open_streams: Option<usize>,
}

/// Sanitized close information associated with an H3 diagnostic snapshot.
#[cfg(feature = "http3")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct H3CloseSummary {
    /// Close category.
    pub kind: H3CloseKind,
    /// QUIC or HTTP/3 close code when the upstream API provides one.
    pub code: Option<u64>,
    /// Whether h3 classified the close as `H3_NO_ERROR`.
    pub graceful: bool,
}

/// Observable transport counters.
///
/// Shared via `Arc` across connectors owned by one [`crate::Client`].
/// Cloning the `Arc` is cheap; counter methods are lock-free and diagnostic
/// snapshots use a private bounded mutex.
#[derive(Debug, Default)]
pub struct TransportMetrics {
    /// Physical connection admission waits after an immediate permit attempt
    /// found the configured live-connection cap exhausted.
    pub physical_admission_waits: AtomicUsize,
    /// Physical connection admission waits that reached their deadline.
    pub physical_admission_timeouts: AtomicUsize,
    /// Number of currently live admitted Hyper connections.
    pub physical_connections_live: AtomicUsize,
    /// Highest observed number of simultaneously live admitted connections.
    pub physical_connections_high_water: AtomicUsize,
    /// Established transport read inactivity timeouts.
    pub transport_read_inactivity_timeouts: AtomicUsize,
    /// Established transport write inactivity timeouts.
    pub transport_write_inactivity_timeouts: AtomicUsize,
    /// Direct-connector `call` attempts (connector events).
    pub direct_connector_attempts: AtomicUsize,
    /// Direct-connector TCP successes (at least one address connected).
    pub direct_connector_successes: AtomicUsize,
    /// Direct-connector TCP failures (all addresses failed).
    pub direct_connector_failures: AtomicUsize,
    /// Direct-connector DNS resolution attempts.
    pub direct_dns_attempts: AtomicUsize,
    /// Direct-connector DNS resolution failures.
    pub direct_dns_failures: AtomicUsize,
    /// Direct-connector TLS handshake attempts (HTTPS only).
    pub direct_tls_attempts: AtomicUsize,
    /// Direct-connector TLS handshake failures.
    pub direct_tls_failures: AtomicUsize,
    /// UDS connector attempts (connector events).
    pub uds_connector_attempts: AtomicUsize,
    /// UDS connector failures.
    pub uds_connector_failures: AtomicUsize,
    /// Proxy TCP connect attempts (HTTP forward + CONNECT + SOCKS TCP).
    pub proxy_connector_attempts: AtomicUsize,
    /// Proxy TCP connect failures.
    pub proxy_connector_failures: AtomicUsize,
    /// Proxy-endpoint TLS handshake attempts (`https://` proxy only).
    pub proxy_tls_attempts: AtomicUsize,
    /// Proxy-endpoint TLS handshake failures.
    pub proxy_tls_failures: AtomicUsize,
    /// H3/QUIC sessions successfully established (protocol connections).
    pub h3_connections_created: AtomicUsize,
    /// H3 cache evictions (bounded-cache + stale-generation removals).
    pub h3_cache_evictions: AtomicUsize,
    /// Successfully captured 101 upgrades (protocol connections).
    pub upgraded_connections_created: AtomicUsize,
    /// Alt-Svc advertisements learned (fresh H3 alternatives cached).
    pub altsvc_learned: AtomicUsize,
    /// Alt-Svc entries expired on read (lazy expiry).
    pub altsvc_expired: AtomicUsize,
    /// Alt-Svc entries cleared via `clear` or replaced by empty advertisement.
    pub altsvc_cleared: AtomicUsize,
    /// Alt-Svc header values rejected (malformed, oversized, untrusted context).
    pub altsvc_rejected: AtomicUsize,
    /// H3 route selections attempted (explicit or discovered).
    pub h3_route_attempted: AtomicUsize,
    /// H3 route selections skipped due to broken-route suppression.
    pub h3_route_suppressed: AtomicUsize,
    /// Safe H3-to-H2/H1 fallbacks selected (replayable, pre-commit only).
    pub h3_fallback_selected: AtomicUsize,
    /// H3 graceful drains observed (GOAWAY / `RemoteClosing`).
    pub h3_drain_observed: AtomicUsize,
    /// H3 connections closed (driver `poll_close` terminal).
    pub h3_closed: AtomicUsize,
    /// H3 reconnect generations created after drain/eviction.
    pub h3_reconnected: AtomicUsize,
    /// Bounded H3 connection snapshots, when HTTP/3 is enabled.
    #[cfg(feature = "http3")]
    h3_diagnostics: Mutex<std::collections::VecDeque<H3ConnectionDiagnostic>>,
}

impl TransportMetrics {
    /// Create empty metrics.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    fn inc(counter: &AtomicUsize) {
        counter.fetch_add(1, Ordering::Relaxed);
    }

    pub(crate) fn record_physical_admission_wait(&self) {
        Self::inc(&self.physical_admission_waits);
    }

    pub(crate) fn record_physical_admission_timeout(&self) {
        Self::inc(&self.physical_admission_timeouts);
    }

    pub(crate) fn record_physical_connection_admitted(&self) {
        let live = self
            .physical_connections_live
            .fetch_add(1, Ordering::Relaxed)
            .saturating_add(1);
        let mut high_water = self.physical_connections_high_water.load(Ordering::Relaxed);
        while live > high_water {
            match self.physical_connections_high_water.compare_exchange_weak(
                high_water,
                live,
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                Ok(_) => break,
                Err(observed) => high_water = observed,
            }
        }
    }

    pub(crate) fn record_physical_connection_released(&self) {
        self.physical_connections_live
            .fetch_sub(1, Ordering::Relaxed);
    }

    pub(crate) fn record_transport_io_timeout(&self, direction: crate::TransportIoDirection) {
        match direction {
            crate::TransportIoDirection::Read => {
                Self::inc(&self.transport_read_inactivity_timeouts);
            }
            crate::TransportIoDirection::Write => {
                Self::inc(&self.transport_write_inactivity_timeouts);
            }
        }
    }

    /// Record a direct-connector call start.
    pub fn record_direct_attempt(&self) {
        Self::inc(&self.direct_connector_attempts);
    }

    /// Record a direct-connector TCP success.
    pub fn record_direct_success(&self) {
        Self::inc(&self.direct_connector_successes);
    }

    /// Record a direct-connector TCP failure.
    pub fn record_direct_failure(&self) {
        Self::inc(&self.direct_connector_failures);
    }

    /// Record a direct DNS attempt.
    pub fn record_direct_dns_attempt(&self) {
        Self::inc(&self.direct_dns_attempts);
    }

    /// Record a direct DNS failure.
    pub fn record_direct_dns_failure(&self) {
        Self::inc(&self.direct_dns_failures);
    }

    /// Record a direct TLS handshake attempt.
    pub fn record_direct_tls_attempt(&self) {
        Self::inc(&self.direct_tls_attempts);
    }

    /// Record a direct TLS handshake failure.
    pub fn record_direct_tls_failure(&self) {
        Self::inc(&self.direct_tls_failures);
    }

    /// Record a UDS connect attempt.
    pub fn record_uds_attempt(&self) {
        Self::inc(&self.uds_connector_attempts);
    }

    /// Record a UDS connect failure.
    pub fn record_uds_failure(&self) {
        Self::inc(&self.uds_connector_failures);
    }

    /// Record a proxy TCP connect attempt.
    pub fn record_proxy_attempt(&self) {
        Self::inc(&self.proxy_connector_attempts);
    }

    /// Record a proxy TCP connect failure.
    pub fn record_proxy_failure(&self) {
        Self::inc(&self.proxy_connector_failures);
    }

    /// Record a proxy TLS handshake attempt.
    pub fn record_proxy_tls_attempt(&self) {
        Self::inc(&self.proxy_tls_attempts);
    }

    /// Record a proxy TLS handshake failure.
    pub fn record_proxy_tls_failure(&self) {
        Self::inc(&self.proxy_tls_failures);
    }

    /// Record an established H3 session.
    pub fn record_h3_created(&self) {
        Self::inc(&self.h3_connections_created);
    }

    /// Record an H3 cache eviction.
    pub fn record_h3_eviction(&self) {
        Self::inc(&self.h3_cache_evictions);
    }

    /// Record a captured 101 upgrade.
    pub fn record_upgraded(&self) {
        Self::inc(&self.upgraded_connections_created);
    }

    /// Record a learned Alt-Svc alternative.
    pub fn record_altsvc_learned(&self) {
        Self::inc(&self.altsvc_learned);
    }

    /// Record an Alt-Svc lazy expiry.
    pub fn record_altsvc_expired(&self) {
        Self::inc(&self.altsvc_expired);
    }

    /// Record an Alt-Svc clear.
    pub fn record_altsvc_cleared(&self) {
        Self::inc(&self.altsvc_cleared);
    }

    /// Record a rejected Alt-Svc value.
    pub fn record_altsvc_rejected(&self) {
        Self::inc(&self.altsvc_rejected);
    }

    /// Record an H3 route attempt.
    pub fn record_h3_attempted(&self) {
        Self::inc(&self.h3_route_attempted);
    }

    /// Record an H3 suppression skip.
    pub fn record_h3_suppressed(&self) {
        Self::inc(&self.h3_route_suppressed);
    }

    /// Record a safe H3-to-H2/H1 fallback.
    pub fn record_h3_fallback(&self) {
        Self::inc(&self.h3_fallback_selected);
    }

    /// Record an observed H3 graceful drain.
    pub fn record_h3_drain(&self) {
        Self::inc(&self.h3_drain_observed);
    }

    /// Record an H3 connection close.
    pub fn record_h3_closed(&self) {
        Self::inc(&self.h3_closed);
    }

    /// Record an H3 reconnect generation.
    pub fn record_h3_reconnected(&self) {
        Self::inc(&self.h3_reconnected);
    }

    /// Store the latest snapshot for an H3 connection.
    #[cfg(feature = "http3")]
    pub(crate) fn record_h3_diagnostic(&self, diagnostic: H3ConnectionDiagnostic) {
        let Ok(mut diagnostics) = self.h3_diagnostics.lock() else {
            return;
        };
        if let Some(existing) = diagnostics
            .iter_mut()
            .find(|existing| existing.stable_id == diagnostic.stable_id)
        {
            *existing = diagnostic;
            return;
        }
        if diagnostics.len() >= 64 {
            diagnostics.pop_front();
        }
        diagnostics.push_back(diagnostic);
    }

    /// Return bounded H3 connection snapshots without retaining live Quinn
    /// connection handles.
    #[cfg(feature = "http3")]
    #[must_use]
    pub fn h3_diagnostics(&self) -> Vec<H3ConnectionDiagnostic> {
        self.h3_diagnostics
            .lock()
            .map(|diagnostics| diagnostics.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Snapshot all counters for assertions.
    #[must_use]
    #[allow(
        clippy::too_many_arguments,
        reason = "snapshot groups all counters for deterministic test assertions"
    )]
    pub fn snapshot(&self) -> TransportSnapshot {
        TransportSnapshot {
            physical_admission_waits: self.physical_admission_waits.load(Ordering::Relaxed),
            physical_admission_timeouts: self.physical_admission_timeouts.load(Ordering::Relaxed),
            physical_connections_live: self.physical_connections_live.load(Ordering::Relaxed),
            physical_connections_high_water: self
                .physical_connections_high_water
                .load(Ordering::Relaxed),
            transport_read_inactivity_timeouts: self
                .transport_read_inactivity_timeouts
                .load(Ordering::Relaxed),
            transport_write_inactivity_timeouts: self
                .transport_write_inactivity_timeouts
                .load(Ordering::Relaxed),
            direct_connector_attempts: self.direct_connector_attempts.load(Ordering::Relaxed),
            direct_connector_successes: self.direct_connector_successes.load(Ordering::Relaxed),
            direct_connector_failures: self.direct_connector_failures.load(Ordering::Relaxed),
            direct_dns_attempts: self.direct_dns_attempts.load(Ordering::Relaxed),
            direct_dns_failures: self.direct_dns_failures.load(Ordering::Relaxed),
            direct_tls_attempts: self.direct_tls_attempts.load(Ordering::Relaxed),
            direct_tls_failures: self.direct_tls_failures.load(Ordering::Relaxed),
            uds_connector_attempts: self.uds_connector_attempts.load(Ordering::Relaxed),
            uds_connector_failures: self.uds_connector_failures.load(Ordering::Relaxed),
            proxy_connector_attempts: self.proxy_connector_attempts.load(Ordering::Relaxed),
            proxy_connector_failures: self.proxy_connector_failures.load(Ordering::Relaxed),
            proxy_tls_attempts: self.proxy_tls_attempts.load(Ordering::Relaxed),
            proxy_tls_failures: self.proxy_tls_failures.load(Ordering::Relaxed),
            h3_connections_created: self.h3_connections_created.load(Ordering::Relaxed),
            h3_cache_evictions: self.h3_cache_evictions.load(Ordering::Relaxed),
            upgraded_connections_created: self.upgraded_connections_created.load(Ordering::Relaxed),
            altsvc_learned: self.altsvc_learned.load(Ordering::Relaxed),
            altsvc_expired: self.altsvc_expired.load(Ordering::Relaxed),
            altsvc_cleared: self.altsvc_cleared.load(Ordering::Relaxed),
            altsvc_rejected: self.altsvc_rejected.load(Ordering::Relaxed),
            h3_route_attempted: self.h3_route_attempted.load(Ordering::Relaxed),
            h3_route_suppressed: self.h3_route_suppressed.load(Ordering::Relaxed),
            h3_fallback_selected: self.h3_fallback_selected.load(Ordering::Relaxed),
            h3_drain_observed: self.h3_drain_observed.load(Ordering::Relaxed),
            h3_closed: self.h3_closed.load(Ordering::Relaxed),
            h3_reconnected: self.h3_reconnected.load(Ordering::Relaxed),
        }
    }
}

/// Point-in-time snapshot of [`TransportMetrics`] for tests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TransportSnapshot {
    /// Physical admission waits.
    pub physical_admission_waits: usize,
    /// Physical admission timeouts.
    pub physical_admission_timeouts: usize,
    /// Current live admitted Hyper connections.
    pub physical_connections_live: usize,
    /// High-water mark for live admitted Hyper connections.
    pub physical_connections_high_water: usize,
    /// Established transport read inactivity timeouts.
    pub transport_read_inactivity_timeouts: usize,
    /// Established transport write inactivity timeouts.
    pub transport_write_inactivity_timeouts: usize,
    /// Direct-connector attempts.
    pub direct_connector_attempts: usize,
    /// Direct-connector successes.
    pub direct_connector_successes: usize,
    /// Direct-connector failures.
    pub direct_connector_failures: usize,
    /// Direct DNS attempts.
    pub direct_dns_attempts: usize,
    /// Direct DNS failures.
    pub direct_dns_failures: usize,
    /// Direct TLS attempts.
    pub direct_tls_attempts: usize,
    /// Direct TLS failures.
    pub direct_tls_failures: usize,
    /// UDS attempts.
    pub uds_connector_attempts: usize,
    /// UDS failures.
    pub uds_connector_failures: usize,
    /// Proxy attempts.
    pub proxy_connector_attempts: usize,
    /// Proxy failures.
    pub proxy_connector_failures: usize,
    /// Proxy TLS attempts.
    pub proxy_tls_attempts: usize,
    /// Proxy TLS failures.
    pub proxy_tls_failures: usize,
    /// H3 created.
    pub h3_connections_created: usize,
    /// H3 evictions.
    pub h3_cache_evictions: usize,
    /// Upgraded created.
    pub upgraded_connections_created: usize,
    /// Alt-Svc learned.
    pub altsvc_learned: usize,
    /// Alt-Svc expired.
    pub altsvc_expired: usize,
    /// Alt-Svc cleared.
    pub altsvc_cleared: usize,
    /// Alt-Svc rejected.
    pub altsvc_rejected: usize,
    /// H3 route attempted.
    pub h3_route_attempted: usize,
    /// H3 route suppressed.
    pub h3_route_suppressed: usize,
    /// H3 fallback selected.
    pub h3_fallback_selected: usize,
    /// H3 drain observed.
    pub h3_drain_observed: usize,
    /// H3 closed.
    pub h3_closed: usize,
    /// H3 reconnected.
    pub h3_reconnected: usize,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn counters_start_zeroed() {
        let m = TransportMetrics::new();
        let s = m.snapshot();
        assert_eq!(s.direct_connector_attempts, 0);
        assert_eq!(s.upgraded_connections_created, 0);
        assert_eq!(s.altsvc_learned, 0);
        assert_eq!(s.h3_route_attempted, 0);
    }

    #[test]
    fn increments_are_exact() {
        let m = TransportMetrics::new();
        m.record_direct_attempt();
        m.record_direct_attempt();
        m.record_direct_success();
        m.record_upgraded();
        m.record_altsvc_learned();
        m.record_h3_attempted();
        m.record_h3_fallback();
        let s = m.snapshot();
        assert_eq!(s.direct_connector_attempts, 2);
        assert_eq!(s.direct_connector_successes, 1);
        assert_eq!(s.upgraded_connections_created, 1);
        assert_eq!(s.altsvc_learned, 1);
        assert_eq!(s.h3_route_attempted, 1);
        assert_eq!(s.h3_fallback_selected, 1);
    }

    #[cfg(feature = "http3")]
    #[test]
    fn h3_diagnostics_replace_by_connection_and_stay_bounded() {
        let m = TransportMetrics::new();
        let address = "127.0.0.1:443".parse().expect("address");
        for stable_id in 0..65 {
            m.record_h3_diagnostic(H3ConnectionDiagnostic {
                stable_id,
                remote_address: address,
                local_ip: None,
                route: H3RouteKind::Explicit,
                alt_svc_generation: None,
                rtt: Duration::from_millis(1),
                sent_packets: 1,
                received_datagrams: 1,
                lost_packets: 0,
                sent_bytes: 1,
                received_bytes: 1,
                lost_bytes: 0,
                close: None,
                open_streams: None,
            });
        }
        let diagnostics = m.h3_diagnostics();
        assert_eq!(diagnostics.len(), 64);
        assert_eq!(diagnostics[0].stable_id, 1);

        m.record_h3_diagnostic(H3ConnectionDiagnostic {
            stable_id: 64,
            remote_address: address,
            local_ip: None,
            route: H3RouteKind::AltSvc,
            alt_svc_generation: Some(7),
            rtt: Duration::from_millis(2),
            sent_packets: 2,
            received_datagrams: 2,
            lost_packets: 1,
            sent_bytes: 2,
            received_bytes: 2,
            lost_bytes: 1,
            close: Some(H3CloseSummary {
                kind: H3CloseKind::ApplicationClosed,
                code: Some(0x100),
                graceful: true,
            }),
            open_streams: None,
        });
        let updated = m.h3_diagnostics();
        assert_eq!(updated.len(), 64);
        assert_eq!(
            updated.last().expect("last diagnostic").alt_svc_generation,
            Some(7)
        );
        assert_eq!(updated.last().expect("last diagnostic").lost_packets, 1);
    }
}