openkache 0.1.0

Maintained Rust client for the OpenKache cache server
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
//! QUIC backend boundary and persistent stream-lane pool.

use std::collections::HashMap;
use std::future::Future;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::task::{Context, Poll, Waker};
use std::time::{Duration, Instant};

#[cfg(any(feature = "quic-quinn", feature = "tls-tcp"))]
use crate::internal_protocol::MAX_RESPONSE_FRAME_BYTES;
use crate::internal_protocol::{Response, ResponseHeaderBytes, ResponseParts};
use crossfire::{MAsyncRx, MAsyncTx};
use futures_util::{FutureExt, pin_mut, select};

use crate::internal_core::request::RequestAttempt;
use crate::internal_core::{Backend, Error, Operation, Result};

#[cfg(feature = "quic-compio")]
mod compio;
#[cfg(feature = "quic-quinn")]
mod quinn;
#[cfg(feature = "tls-tcp")]
pub(crate) mod tcp;

#[cfg(feature = "quic-quinn")]
pub use quinn::{Lane as QuinnTransportLane, Transport as QuinnTransportConnection};
#[cfg(feature = "tls-tcp")]
pub use tcp::{TcpLane, TcpTransport, TlsTcpLane, TlsTcpTransport};

/// Returns the complete response length once the opaque response header is
/// available.
///
/// Transport adapters share this small framing boundary. They retain the
/// complete bytes, but never inspect an operation body or value codec.
#[cfg(any(feature = "quic-quinn", feature = "tls-tcp"))]
pub(crate) fn response_frame_len(
    prefix: &[u8],
    maximum: usize,
) -> std::result::Result<Option<usize>, String> {
    let header = Response::decode_header(prefix).map_err(|error| error.to_string())?;
    let Some(header) = header else {
        return Ok(None);
    };
    let frame_len = header.frame_len().map_err(|error| error.to_string())?;
    let maximum = maximum.min(MAX_RESPONSE_FRAME_BYTES);
    if frame_len > maximum {
        return Err(format!(
            "response frame is {frame_len} bytes, maximum is {maximum}"
        ));
    }
    Ok(Some(frame_len))
}

/// Applies the one client-side security profile shared by QUIC and TCP.
///
/// The public transport bindings accept a caller-owned Rustls configuration,
/// but they must not let a permissive configuration silently downgrade the
/// wire profile.  Restricting the offered ALPN and key-exchange list before
/// constructing the backend configuration makes plaintext and classical-only
/// fallbacks impossible.  Session resumption is disabled because a resumed
/// TLS 1.3 handshake does not expose a fresh key-exchange group to validate.
#[cfg(any(feature = "quic-compio", feature = "quic-quinn", feature = "tls-tcp"))]
pub(crate) fn enforce_client_profile(
    tls: &mut rustls::ClientConfig,
) -> crate::internal_core::Result<()> {
    if tls.alpn_protocols.len() != 1
        || tls.alpn_protocols[0].as_slice() != crate::internal_protocol::ALPN
    {
        return Err(crate::internal_core::Error::configuration(
            "tls.alpn_protocols",
            "must offer only openkache/1",
        ));
    }
    let groups = &tls.crypto_provider().kx_groups;
    if groups.len() != 1 || groups[0].name() != crate::internal_core::config::PQ_GROUP {
        return Err(crate::internal_core::Error::configuration(
            "tls.kx_groups",
            "must offer only X25519MLKEM768",
        ));
    }
    tls.check_selected_alpn = true;
    tls.enable_early_data = false;
    tls.resumption = rustls::client::Resumption::disabled();
    Ok(())
}

/// Validates a complete opaque response frame without decoding its payload.
///
/// The request engine remains responsible for correlation and operation
/// status validation.  This helper only confirms that the bytes consumed by a
/// lane still describe the same bounded frame advertised by its header.
#[cfg(any(feature = "quic-quinn", feature = "tls-tcp"))]
pub(crate) fn validate_response_frame(
    frame: &[u8],
    expected: usize,
) -> std::result::Result<(), String> {
    if Response::decode_header(frame)
        .map_err(|error| error.to_string())?
        .and_then(|header| header.frame_len().ok())
        != Some(expected)
    {
        return Err("response frame length is malformed".into());
    }
    Ok(())
}

pub(crate) trait ClientConnection: Sized {
    type Lane<'a>: ClientLane
    where
        Self: 'a;

    fn connect(
        address: SocketAddr,
        server_name: &str,
        tls: rustls::ClientConfig,
        timeout: Duration,
        max_stream_lanes: usize,
        budget: RequestBudget,
    ) -> impl Future<Output = Result<Self>>;

    fn acquire_lane(&self, deadline: Deadline) -> impl Future<Output = Result<Self::Lane<'_>>>;

    fn negotiated_alpn(&self) -> Option<&[u8]>;

    fn timeout<F: Future>(
        duration: Duration,
        future: F,
    ) -> impl Future<Output = Result<Option<F::Output>>>;

    fn close(&self);
}

pub(crate) trait ClientLane {
    fn write_request(
        &mut self,
        request: RequestAttempt,
        timeout: Duration,
    ) -> impl Future<Output = Result<()>>;

    fn read_response(
        &mut self,
        maximum: usize,
        deadline: Deadline,
    ) -> impl Future<Output = Result<ResponseParts>>;

    /// Transfers the response-body lease to a caller-owned value.
    ///
    /// The lease is present only after a successful response read. Protected
    /// clients use it to keep network bytes accounted while value decoding
    /// performs authentication, decompression, and structured parsing.
    fn take_response_permit(&mut self) -> Option<RequestBudgetPermit>;

    fn release(self);
}

/// One weighted byte budget shared by transport and value work.
///
/// The permit is acquired before a response body allocation/read and is held
/// by the lane until the response callback completes. Value codecs use the
/// same budget for decrypted, decompressed, and structured-value allocations.
#[derive(Clone)]
pub struct InFlightByteBudget {
    inner: Arc<RequestBudgetInner>,
}

pub type RequestBudget = InFlightByteBudget;

struct RequestBudgetInner {
    capacity: usize,
    state: Mutex<RequestBudgetState>,
}

struct RequestBudgetState {
    used: usize,
    next_waiter_id: u64,
    waiters: HashMap<u64, Waker>,
}

pub struct BytePermit {
    inner: Arc<RequestBudgetInner>,
    bytes: usize,
}

pub(crate) type RequestBudgetPermit = BytePermit;

struct RequestBudgetAcquire {
    inner: Arc<RequestBudgetInner>,
    bytes: usize,
    waiter_id: Option<u64>,
}

impl InFlightByteBudget {
    /// Creates one aggregate byte budget shared by concurrent operations.
    pub fn new(capacity: usize) -> Self {
        Self {
            inner: Arc::new(RequestBudgetInner {
                capacity,
                state: Mutex::new(RequestBudgetState {
                    used: 0,
                    next_waiter_id: 0,
                    waiters: HashMap::new(),
                }),
            }),
        }
    }

    /// Returns the configured aggregate capacity.
    pub fn capacity(&self) -> usize {
        self.inner.capacity
    }

    /// Reserves bytes synchronously for a bounded codec operation.
    /// Reserves bytes immediately or returns a local resource-limit error.
    pub fn try_reserve(&self, bytes: usize) -> Result<BytePermit> {
        let mut state = self
            .inner
            .state
            .lock()
            .expect("request budget lock poisoned");
        if bytes > self.inner.capacity.saturating_sub(state.used) {
            return Err(Error::ResourceLimit {
                requested: bytes,
                maximum: self.inner.capacity,
            });
        }
        state.used += bytes;
        Ok(BytePermit {
            inner: Arc::clone(&self.inner),
            bytes,
        })
    }

    /// Waits for bytes without allocating until a permit is available.
    ///
    /// The timeout is applied by the backend wrapper around the response read;
    /// it is retained in this API so callers cannot accidentally omit an
    /// operation deadline when acquiring network capacity.
    pub async fn reserve(&self, bytes: usize, _timeout: Duration) -> Result<BytePermit> {
        if bytes > self.inner.capacity {
            return Err(Error::ResourceLimit {
                requested: bytes,
                maximum: self.inner.capacity,
            });
        }
        Ok(RequestBudgetAcquire {
            inner: Arc::clone(&self.inner),
            bytes,
            waiter_id: None,
        }
        .await)
    }

    pub(crate) async fn acquire(&self, bytes: usize, timeout: Duration) -> Result<BytePermit> {
        self.reserve(bytes, timeout).await
    }
}

impl Future for RequestBudgetAcquire {
    type Output = RequestBudgetPermit;

    fn poll(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll<Self::Output> {
        let inner = Arc::clone(&self.inner);
        let mut state = inner.state.lock().expect("request budget lock poisoned");
        if self.bytes <= inner.capacity.saturating_sub(state.used) {
            if let Some(waiter_id) = self.waiter_id.take() {
                state.waiters.remove(&waiter_id);
            }
            state.used += self.bytes;
            drop(state);
            return Poll::Ready(RequestBudgetPermit {
                inner,
                bytes: self.bytes,
            });
        }

        if let Some(waiter_id) = self.waiter_id {
            if let Some(waiter) = state.waiters.get_mut(&waiter_id) {
                if !waiter.will_wake(context.waker()) {
                    waiter.clone_from(context.waker());
                }
            }
            return Poll::Pending;
        }

        let waiter_id = state.next_waiter_id;
        state.next_waiter_id = state
            .next_waiter_id
            .checked_add(1)
            .expect("request budget waiter identifier overflowed");
        state.waiters.insert(waiter_id, context.waker().clone());
        self.waiter_id = Some(waiter_id);
        Poll::Pending
    }
}

impl Drop for RequestBudgetAcquire {
    fn drop(&mut self) {
        if let Some(waiter_id) = self.waiter_id {
            self.inner
                .state
                .lock()
                .expect("request budget lock poisoned")
                .waiters
                .remove(&waiter_id);
        }
    }
}

impl BytePermit {
    /// Returns the number of bytes retained by this permit.
    pub const fn bytes(&self) -> usize {
        self.bytes
    }
}

impl Drop for BytePermit {
    fn drop(&mut self) {
        if self.bytes == 0 {
            return;
        }
        let waiters = {
            let mut state = self
                .inner
                .state
                .lock()
                .expect("request budget lock poisoned");
            state.used = state
                .used
                .checked_sub(self.bytes)
                .expect("released request bytes must be reserved");
            // Keep registrations in place while waking. A wake-up is only a
            // hint: if one waiter consumes the newly available bytes, every
            // other waiter must remain registered for the next release.
            state.waiters.values().cloned().collect::<Vec<_>>()
        };
        for waiter in waiters {
            waiter.wake();
        }
    }
}

macro_rules! connection_backend {
    ($connection:ident, $lane:ident, $module:ident, $timeout:ident) => {
        pub(crate) struct $connection(PooledConnection<$module::Connection>);

        pub(crate) struct $lane<'a>(PooledLane<'a, $module::Connection>);

        impl ClientConnection for $connection {
            type Lane<'a> = $lane<'a>;

            async fn connect(
                address: SocketAddr,
                server_name: &str,
                tls: rustls::ClientConfig,
                timeout: Duration,
                max_stream_lanes: usize,
                budget: RequestBudget,
            ) -> Result<Self> {
                $module::connect(address, server_name, tls, timeout)
                    .await
                    .map(|connection| {
                        Self(PooledConnection::new(connection, max_stream_lanes, budget))
                    })
                    .map_err(Error::from)
            }

            async fn acquire_lane(&self, deadline: Deadline) -> Result<Self::Lane<'_>> {
                let remaining = deadline.remaining(Operation::StreamAcquisition)?;
                match $timeout(remaining, self.0.acquire_lane(deadline)).await? {
                    Some(result) => result.map($lane),
                    None => Err(Error::Timeout {
                        operation: Operation::StreamAcquisition,
                    }),
                }
            }

            fn negotiated_alpn(&self) -> Option<&[u8]> {
                self.0.inner.negotiated_alpn()
            }

            async fn timeout<F: Future>(
                duration: Duration,
                future: F,
            ) -> Result<Option<F::Output>> {
                $timeout(duration, future).await
            }

            fn close(&self) {
                self.0.inner.close();
            }
        }

        impl ClientLane for $lane<'_> {
            async fn write_request(
                &mut self,
                request: RequestAttempt,
                timeout: Duration,
            ) -> Result<()> {
                self.0.write_request(request, timeout).await
            }

            async fn read_response(
                &mut self,
                maximum: usize,
                deadline: Deadline,
            ) -> Result<ResponseParts> {
                let remaining = deadline.remaining(Operation::ResponseBodyRead)?;
                match $timeout(remaining, self.0.read_response(maximum, deadline)).await? {
                    Some(result) => result,
                    None => Err(Error::Timeout {
                        operation: Operation::ResponseBodyRead,
                    }),
                }
            }

            fn take_response_permit(&mut self) -> Option<RequestBudgetPermit> {
                self.0.take_response_permit()
            }

            fn release(self) {
                self.0.release();
            }
        }
    };
}

#[cfg(feature = "quic-quinn")]
connection_backend!(QuinnConnection, QuinnLane, quinn, timeout_quinn);
#[cfg(feature = "quic-compio")]
connection_backend!(CompioConnection, CompioLane, compio, timeout_compio);

#[cfg(feature = "quic-quinn")]
async fn timeout_quinn<F: Future>(duration: Duration, future: F) -> Result<Option<F::Output>> {
    if tokio::runtime::Handle::try_current().is_err() {
        return Err(
            TransportError::runtime(Backend::Quinn, "an active Tokio runtime is required").into(),
        );
    }
    Ok(tokio::time::timeout(duration, future).await.ok())
}

#[cfg(feature = "quic-compio")]
async fn timeout_compio<F: Future>(duration: Duration, future: F) -> Result<Option<F::Output>> {
    if ::compio::runtime::Runtime::try_current().is_none() {
        return Err(TransportError::runtime(
            Backend::Compio,
            "an active Compio runtime is required",
        )
        .into());
    }
    Ok(::compio::runtime::time::timeout(duration, future)
        .await
        .ok())
}

trait BackendConnection {
    type Stream: BackendStream + 'static;

    fn negotiated_alpn(&self) -> Option<&[u8]>;

    fn open_bi(
        &self,
        timeout: Duration,
    ) -> impl Future<Output = std::result::Result<Self::Stream, TransportError>>;

    fn close(&self);
}

trait BackendStream {
    fn write_request(
        &mut self,
        request: RequestAttempt,
        timeout: Duration,
    ) -> impl Future<Output = std::result::Result<(), TransportError>>;

    fn read_byte(
        &mut self,
        timeout: Duration,
    ) -> impl Future<Output = std::result::Result<u8, TransportError>>;

    fn read_exact(
        &mut self,
        length: usize,
        timeout: Duration,
    ) -> impl Future<Output = std::result::Result<Vec<u8>, TransportError>>;
}

type LaneSender<T> = MAsyncTx<crossfire::mpmc::Array<T>>;
type LaneReceiver<T> = MAsyncRx<crossfire::mpmc::Array<T>>;

struct PooledConnection<B: BackendConnection> {
    inner: B,
    idle_lanes_tx: LaneSender<B::Stream>,
    idle_lanes_rx: LaneReceiver<B::Stream>,
    lane_capacity_tx: LaneSender<()>,
    lane_capacity_rx: LaneReceiver<()>,
    open_lanes: AtomicUsize,
    max_stream_lanes: usize,
    budget: RequestBudget,
}

struct PooledLane<'a, B: BackendConnection> {
    connection: &'a PooledConnection<B>,
    stream: Option<B::Stream>,
    response_permit: Option<RequestBudgetPermit>,
}

impl<B: BackendConnection> PooledConnection<B> {
    fn new(inner: B, max_stream_lanes: usize, budget: RequestBudget) -> Self {
        let (idle_lanes_tx, idle_lanes_rx) = crossfire::mpmc::bounded_async(max_stream_lanes);
        let (lane_capacity_tx, lane_capacity_rx) = crossfire::mpmc::bounded_async(max_stream_lanes);
        Self {
            inner,
            idle_lanes_tx,
            idle_lanes_rx,
            lane_capacity_tx,
            lane_capacity_rx,
            open_lanes: AtomicUsize::new(0),
            max_stream_lanes,
            budget,
        }
    }

    async fn acquire_lane(&self, deadline: Deadline) -> Result<PooledLane<'_, B>> {
        loop {
            if let Ok(lane) = self.idle_lanes_rx.try_recv() {
                return Ok(PooledLane::new(self, lane));
            }
            if !self.reserve_lane() {
                let idle = self.idle_lanes_rx.recv().fuse();
                let capacity = self.lane_capacity_rx.recv().fuse();
                pin_mut!(idle, capacity);
                select! {
                    lane = idle => {
                        return lane
                            .map(|lane| PooledLane::new(self, lane))
                            .map_err(|_| Error::Connection("stream lane pool closed".into()));
                    }
                    _ = capacity => continue,
                }
            }

            let reservation = LaneReservation::new(self);
            let opening = self
                .inner
                .open_bi(deadline.remaining(Operation::StreamAcquisition)?)
                .fuse();
            let idle = self.idle_lanes_rx.recv().fuse();
            pin_mut!(opening, idle);
            select! {
                opened = opening => {
                    let stream = opened?;
                    reservation.commit();
                    return Ok(PooledLane::new(self, stream));
                }
                lane = idle => {
                    return lane
                        .map(|lane| PooledLane::new(self, lane))
                        .map_err(|_| Error::Connection("stream lane pool closed".into()));
                }
            }
        }
    }

    fn reserve_lane(&self) -> bool {
        self.open_lanes
            .try_update(Ordering::AcqRel, Ordering::Acquire, |open| {
                (open < self.max_stream_lanes).then_some(open + 1)
            })
            .is_ok()
    }

    fn release_lane(&self, lane: B::Stream) {
        if self.idle_lanes_tx.try_send(lane).is_err() {
            self.remove_lane();
        }
    }

    fn discard_lane(&self, lane: B::Stream) {
        drop(lane);
        self.remove_lane();
    }

    fn remove_lane(&self) {
        if self
            .open_lanes
            .try_update(Ordering::AcqRel, Ordering::Acquire, |open| {
                open.checked_sub(1)
            })
            .is_ok()
        {
            let _ = self.lane_capacity_tx.try_send(());
        }
    }
}

impl<'a, B: BackendConnection> PooledLane<'a, B> {
    fn new(connection: &'a PooledConnection<B>, stream: B::Stream) -> Self {
        Self {
            connection,
            stream: Some(stream),
            response_permit: None,
        }
    }

    async fn write_request(&mut self, request: RequestAttempt, timeout: Duration) -> Result<()> {
        let stream = self
            .stream
            .as_mut()
            .ok_or_else(|| Error::Connection("stream lane has already been released".into()))?;
        // Retain request bytes only for the duration of the network write.
        // The permit is dropped before response admission, allowing one
        // full-sized request and response to make progress under the same
        // aggregate budget.
        let _request_permit = self.connection.budget.try_reserve(request.len())?;
        stream.write_request(request, timeout).await?;
        Ok(())
    }

    async fn read_response(&mut self, maximum: usize, deadline: Deadline) -> Result<ResponseParts> {
        let stream = self
            .stream
            .as_mut()
            .ok_or_else(|| Error::Connection("stream lane has already been released".into()))?;
        let mut header_bytes = ResponseHeaderBytes::new();
        let header = loop {
            header_bytes
                .push(
                    stream
                        .read_byte(deadline.remaining(Operation::ResponseHeaderRead)?)
                        .await?,
                )
                .map_err(Error::protocol)?;
            if let Some(header) =
                Response::decode_header(header_bytes.as_slice()).map_err(Error::protocol)?
            {
                break header;
            }
        };
        let frame_len = header.frame_len().map_err(Error::protocol)?;
        if frame_len > maximum {
            return Err(Error::ResponseTooLarge { maximum });
        }
        let body_len = header.payload_len();
        let permit = self
            .connection
            .budget
            .acquire(body_len, deadline.remaining(Operation::ResponseBodyRead)?)
            .await?;
        let payload = if body_len == 0 {
            Vec::new()
        } else {
            stream
                .read_exact(body_len, deadline.remaining(Operation::ResponseBodyRead)?)
                .await?
        };
        let response = ResponseParts::decode(header_bytes, payload).map_err(Error::protocol)?;
        self.response_permit = Some(permit);
        Ok(response)
    }

    fn take_response_permit(&mut self) -> Option<RequestBudgetPermit> {
        self.response_permit.take()
    }

    fn release(mut self) {
        if let Some(stream) = self.stream.take() {
            self.connection.release_lane(stream);
        }
    }
}

impl<B: BackendConnection> Drop for PooledLane<'_, B> {
    fn drop(&mut self) {
        if let Some(stream) = self.stream.take() {
            self.connection.discard_lane(stream);
        }
    }
}

struct LaneReservation<'a, B: BackendConnection> {
    connection: &'a PooledConnection<B>,
    active: bool,
}

impl<'a, B: BackendConnection> LaneReservation<'a, B> {
    fn new(connection: &'a PooledConnection<B>) -> Self {
        Self {
            connection,
            active: true,
        }
    }

    fn commit(mut self) {
        self.active = false;
    }
}

impl<B: BackendConnection> Drop for LaneReservation<'_, B> {
    fn drop(&mut self) {
        if self.active {
            self.connection.remove_lane();
        }
    }
}

#[derive(Clone, Copy)]
pub(crate) struct Deadline(Instant);

impl Deadline {
    pub(crate) fn after(timeout: Duration) -> Result<Self> {
        Instant::now()
            .checked_add(timeout)
            .map(Self)
            .ok_or_else(|| {
                Error::configuration("request_timeout", "exceeds the platform clock range")
            })
    }

    pub(crate) fn remaining(self, operation: Operation) -> Result<Duration> {
        self.0
            .checked_duration_since(Instant::now())
            .filter(|remaining| !remaining.is_zero())
            .ok_or(Error::Timeout { operation })
    }
}

#[derive(Debug, thiserror::Error)]
#[error("{backend} QUIC {operation} failed: {message}")]
pub(crate) struct TransportError {
    backend: Backend,
    operation: Operation,
    message: String,
    kind: TransportErrorKind,
}

#[derive(Clone, Copy, Debug)]
enum TransportErrorKind {
    Runtime,
    Timeout,
    Transport,
}

impl TransportError {
    fn backend(backend: Backend, operation: Operation, error: impl std::fmt::Display) -> Self {
        Self {
            backend,
            operation,
            message: error.to_string(),
            kind: TransportErrorKind::Transport,
        }
    }

    fn runtime(backend: Backend, message: &'static str) -> Self {
        Self {
            backend,
            operation: Operation::ConnectionSetup,
            message: message.into(),
            kind: TransportErrorKind::Runtime,
        }
    }

    fn timeout(backend: Backend, operation: Operation, timeout: Duration) -> Self {
        Self {
            backend,
            operation,
            message: format!("timed out after {timeout:?}"),
            kind: TransportErrorKind::Timeout,
        }
    }
}

impl From<TransportError> for Error {
    fn from(error: TransportError) -> Self {
        match error.kind {
            TransportErrorKind::Runtime => Self::Runtime {
                backend: error.backend,
                message: error.message,
            },
            TransportErrorKind::Timeout => Self::Timeout {
                operation: error.operation,
            },
            TransportErrorKind::Transport => Self::Transport {
                backend: error.backend,
                operation: error.operation,
                message: error.message,
            },
        }
    }
}