Skip to main content

serenade_rate_limiter/
rate_limit.rs

1//! Outcome of a [`crate::RateLimiter::consume`] call.
2
3use std::time::Duration;
4
5/// Accept / reject decision with remaining budget.
6#[derive(Clone, Debug, PartialEq, Eq)]
7pub struct RateLimit {
8    accepted: bool,
9    limit: u32,
10    remaining_tokens: u32,
11    retry_after: Option<Duration>,
12}
13
14impl RateLimit {
15    pub(crate) const fn accepted(
16        limit: u32,
17        remaining_tokens: u32,
18        retry_after: Option<Duration>,
19    ) -> Self {
20        Self {
21            accepted: true,
22            limit,
23            remaining_tokens,
24            retry_after,
25        }
26    }
27
28    pub(crate) const fn rejected(
29        limit: u32,
30        remaining_tokens: u32,
31        retry_after: Option<Duration>,
32    ) -> Self {
33        Self {
34            accepted: false,
35            limit,
36            remaining_tokens,
37            retry_after,
38        }
39    }
40
41    /// Whether the consume was accepted.
42    #[must_use]
43    pub const fn is_accepted(&self) -> bool {
44        self.accepted
45    }
46
47    /// Configured limit for this policy.
48    #[must_use]
49    pub const fn limit(&self) -> u32 {
50        self.limit
51    }
52
53    /// Tokens remaining after this decision.
54    #[must_use]
55    pub const fn remaining_tokens(&self) -> u32 {
56        self.remaining_tokens
57    }
58
59    /// Suggested wait before retrying when rejected.
60    #[must_use]
61    pub const fn retry_after(&self) -> Option<Duration> {
62        self.retry_after
63    }
64}