Skip to main content

token_bucket/decision/
impl.rs

1use super::*;
2
3/// Accessors and tuple/duration conversions for `RateLimitDecision`.
4impl RateLimitDecision {
5    /// Constructs a new `RateLimitDecision`.
6    ///
7    /// # Arguments
8    ///
9    /// - `bool` - Whether the request was allowed.
10    /// - `u64` - Remaining quota after evaluation.
11    /// - `u64` - Nanoseconds until full quota reset/refill.
12    /// - `u64` - Nanoseconds until the request may be retried.
13    /// - `u64` - Configured burst or window limit.
14    #[inline(always)]
15    pub fn new(
16        allowed: bool,
17        remaining: u64,
18        reset_after_ns: u64,
19        retry_after_ns: u64,
20        limit: u64,
21    ) -> Self {
22        Self {
23            allowed,
24            remaining,
25            reset_after_ns,
26            retry_after_ns,
27            limit,
28        }
29    }
30
31    /// Returns whether the request was allowed.
32    ///
33    /// # Arguments
34    ///
35    /// - `&Self` - The decision.
36    ///
37    /// # Returns
38    ///
39    /// - `bool` - `true` if admitted, `false` if rate-limited.
40    #[inline(always)]
41    pub fn get_allowed(&self) -> bool {
42        self.allowed
43    }
44
45    /// Alias for `get_allowed` returning whether the request was admitted.
46    ///
47    /// # Arguments
48    ///
49    /// - `&Self` - The decision.
50    ///
51    /// # Returns
52    ///
53    /// - `bool` - `true` if admitted, `false` if rate-limited.
54    #[inline(always)]
55    pub fn is_allowed(&self) -> bool {
56        self.get_allowed()
57    }
58
59    /// Returns remaining tokens or requests available after evaluation.
60    ///
61    /// # Arguments
62    ///
63    /// - `&Self` - The decision.
64    ///
65    /// # Returns
66    ///
67    /// - `u64` - Remaining quota units.
68    #[inline(always)]
69    pub fn get_remaining(&self) -> u64 {
70        self.remaining
71    }
72
73    /// Returns nanoseconds until the quota fully resets or refills.
74    ///
75    /// # Arguments
76    ///
77    /// - `&Self` - The decision.
78    ///
79    /// # Returns
80    ///
81    /// - `u64` - Nanoseconds until full reset.
82    #[inline(always)]
83    pub fn get_reset_after_ns(&self) -> u64 {
84        self.reset_after_ns
85    }
86
87    /// Returns nanoseconds until the caller should retry.
88    ///
89    /// # Arguments
90    ///
91    /// - `&Self` - The decision.
92    ///
93    /// # Returns
94    ///
95    /// - `u64` - `0` when allowed, retry wait in nanoseconds when denied.
96    #[inline(always)]
97    pub fn get_retry_after_ns(&self) -> u64 {
98        self.retry_after_ns
99    }
100
101    /// Returns the configured limit or burst capacity.
102    ///
103    /// # Arguments
104    ///
105    /// - `&Self` - The decision.
106    ///
107    /// # Returns
108    ///
109    /// - `u64` - Configured burst or window limit.
110    #[inline(always)]
111    pub fn get_limit(&self) -> u64 {
112        self.limit
113    }
114
115    /// Returns `(allowed, remaining, reset_after_ns, retry_after_ns)` as a tuple.
116    ///
117    /// # Arguments
118    ///
119    /// - `&Self` - The decision.
120    ///
121    /// # Returns
122    ///
123    /// - `(bool, u64, u64, u64)` - Compact decision tuple.
124    #[inline(always)]
125    pub fn as_tuple(&self) -> (bool, u64, u64, u64) {
126        (
127            self.get_allowed(),
128            self.get_remaining(),
129            self.get_reset_after_ns(),
130            self.get_retry_after_ns(),
131        )
132    }
133
134    /// Returns `reset_after_ns` as a `Duration`.
135    ///
136    /// # Arguments
137    ///
138    /// - `&Self` - The decision.
139    ///
140    /// # Returns
141    ///
142    /// - `Duration` - Duration until full quota reset.
143    #[inline(always)]
144    pub fn reset_after(&self) -> Duration {
145        Duration::from_nanos(self.get_reset_after_ns())
146    }
147
148    /// Returns `retry_after_ns` as a `Duration`.
149    ///
150    /// # Arguments
151    ///
152    /// - `&Self` - The decision.
153    ///
154    /// # Returns
155    ///
156    /// - `Duration` - Duration until retry.
157    #[inline(always)]
158    pub fn retry_after(&self) -> Duration {
159        Duration::from_nanos(self.get_retry_after_ns())
160    }
161}