Skip to main content

moqtap_client/draft12/session/
request_id.rs

1use moqtap_codec::varint::VarInt;
2
3/// Errors from request ID allocation or validation.
4#[derive(Debug, thiserror::Error, PartialEq, Eq)]
5pub enum RequestIdError {
6    /// The request ID exceeds the current MAX_REQUEST_ID.
7    #[error("request ID {0} exceeds max {1}")]
8    ExceedsMax(u64, u64),
9    /// MAX_REQUEST_ID must only increase; it decreased.
10    #[error("max request ID can only increase: was {0}, got {1}")]
11    Decreased(u64, u64),
12    /// No request IDs are available (max is 0 or exhausted).
13    #[error("no request IDs available (blocked)")]
14    Blocked,
15}
16
17/// Allocates and validates request IDs per the MoQT draft-12 spec.
18///
19/// Draft-12 uses the same monotonic allocation rules as draft-11. The
20/// subscriber allocates IDs monotonically starting at 0; SUBSCRIBE, FETCH,
21/// PUBLISH, ANNOUNCE, etc. all share the same namespace since each carries a
22/// `request_id` field.
23pub struct RequestIdAllocator {
24    next_id: u64,
25    max_id: u64,
26}
27
28impl Default for RequestIdAllocator {
29    fn default() -> Self {
30        Self::new()
31    }
32}
33
34impl RequestIdAllocator {
35    /// Create a new allocator starting at request_id 0 with max_id = 0
36    /// (blocked until the peer sends MAX_REQUEST_ID).
37    pub fn new() -> Self {
38        Self { next_id: 0, max_id: 0 }
39    }
40
41    /// Allocate the next request ID.
42    pub fn allocate(&mut self) -> Result<VarInt, RequestIdError> {
43        if self.max_id == 0 || self.next_id >= self.max_id {
44            return Err(RequestIdError::Blocked);
45        }
46        let id = VarInt::from_u64(self.next_id).unwrap();
47        self.next_id += 1;
48        Ok(id)
49    }
50
51    /// Update the maximum allowed request ID (can only increase).
52    pub fn update_max(&mut self, new_max: u64) -> Result<(), RequestIdError> {
53        if new_max <= self.max_id {
54            return Err(RequestIdError::Decreased(self.max_id, new_max));
55        }
56        self.max_id = new_max;
57        Ok(())
58    }
59
60    /// Validate a request ID received from the peer.
61    pub fn validate_peer_id(&self, id: u64) -> Result<(), RequestIdError> {
62        if id >= self.max_id {
63            return Err(RequestIdError::ExceedsMax(id, self.max_id));
64        }
65        Ok(())
66    }
67
68    /// Check if we are blocked (max_id is 0 or next_id >= max_id).
69    pub fn is_blocked(&self) -> bool {
70        self.max_id == 0 || self.next_id >= self.max_id
71    }
72
73    /// Get the current maximum request ID.
74    pub fn max_id(&self) -> u64 {
75        self.max_id
76    }
77}