Skip to main content

bornera_core/
limits.rs

1//! Count, byte, and match-key limits for one connection epoch.
2
3use core::fmt;
4use core::num::NonZeroUsize;
5
6use calandria::RetainedBytes;
7
8use crate::{MatchKey, write::WriteQueueLimits};
9
10/// An inclusive range of protocol-visible match keys.
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub struct MatchKeySpace {
13    first: MatchKey,
14    last: MatchKey,
15}
16
17impl MatchKeySpace {
18    /// Creates a nonempty inclusive match-key range.
19    pub const fn new(first: u32, last: u32) -> Result<Self, LimitsError> {
20        if first > last {
21            return Err(LimitsError::EmptyMatchKeySpace);
22        }
23        Ok(Self {
24            first: MatchKey::new(first),
25            last: MatchKey::new(last),
26        })
27    }
28
29    /// Returns the first key in the range.
30    pub const fn first(self) -> MatchKey {
31        self.first
32    }
33
34    /// Returns the last key in the range.
35    pub const fn last(self) -> MatchKey {
36        self.last
37    }
38
39    /// Returns the number of keys in the range.
40    pub fn capacity(self) -> u64 {
41        u64::from(self.last.get()) - u64::from(self.first.get()) + 1
42    }
43
44    pub(crate) const fn next(self, current: MatchKey) -> MatchKey {
45        if current.get() == self.last.get() {
46            self.first
47        } else {
48            MatchKey::new(current.get() + 1)
49        }
50    }
51}
52
53/// Bounded resources owned by one connection epoch.
54#[derive(Clone, Copy, Debug, Eq, PartialEq)]
55pub struct ConnectionLimits {
56    max_operations: NonZeroUsize,
57    max_retained_bytes: RetainedBytes,
58    max_write_frames: NonZeroUsize,
59    max_write_retained_bytes: RetainedBytes,
60    match_keys: MatchKeySpace,
61}
62
63impl ConnectionLimits {
64    /// Creates limits with nonzero count capacities.
65    pub const fn new(
66        max_operations: usize,
67        max_retained_bytes: RetainedBytes,
68        max_write_frames: usize,
69        max_write_retained_bytes: RetainedBytes,
70        match_keys: MatchKeySpace,
71    ) -> Result<Self, LimitsError> {
72        let Some(max_operations) = NonZeroUsize::new(max_operations) else {
73            return Err(LimitsError::ZeroOperationCapacity);
74        };
75        let Some(max_write_frames) = NonZeroUsize::new(max_write_frames) else {
76            return Err(LimitsError::ZeroWriteCapacity);
77        };
78        Ok(Self {
79            max_operations,
80            max_retained_bytes,
81            max_write_frames,
82            max_write_retained_bytes,
83            match_keys,
84        })
85    }
86
87    /// Returns the maximum accepted and reserved operations.
88    pub const fn max_operations(self) -> usize {
89        self.max_operations.get()
90    }
91
92    /// Returns the maximum semantic retained bytes.
93    pub const fn max_retained_bytes(self) -> RetainedBytes {
94        self.max_retained_bytes
95    }
96
97    /// Returns the maximum queued and reserved write frames.
98    pub const fn max_write_frames(self) -> usize {
99        self.max_write_frames.get()
100    }
101
102    /// Returns the maximum memory retained by queued and reserved frames.
103    pub const fn max_write_retained_bytes(self) -> RetainedBytes {
104        self.max_write_retained_bytes
105    }
106
107    /// Returns the match-key space.
108    pub const fn match_keys(self) -> MatchKeySpace {
109        self.match_keys
110    }
111
112    /// Returns the matching bounded-writer limits for this reservation ledger.
113    pub(crate) const fn write_queue_limits(self) -> WriteQueueLimits {
114        WriteQueueLimits::new(self.max_write_frames, self.max_write_retained_bytes)
115    }
116}
117
118/// Invalid connection-limit configuration.
119#[derive(Clone, Copy, Debug, Eq, PartialEq)]
120#[non_exhaustive]
121pub enum LimitsError {
122    /// No operation can be admitted.
123    ZeroOperationCapacity,
124    /// No write frame can be reserved.
125    ZeroWriteCapacity,
126    /// The inclusive match-key range is reversed.
127    EmptyMatchKeySpace,
128}
129
130impl fmt::Display for LimitsError {
131    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
132        formatter.write_str(match self {
133            Self::ZeroOperationCapacity => "operation capacity must be nonzero",
134            Self::ZeroWriteCapacity => "write-frame capacity must be nonzero",
135            Self::EmptyMatchKeySpace => "match-key range must be nonempty",
136        })
137    }
138}
139
140impl core::error::Error for LimitsError {}