Skip to main content

camel_api/
throttler.rs

1use std::time::Duration;
2
3#[derive(Debug, Clone, Default, PartialEq, Eq)]
4#[non_exhaustive]
5pub enum ThrottleStrategy {
6    /// Queue messages until capacity available (default)
7    #[default]
8    Delay,
9    /// Return error immediately when throttled
10    Reject,
11    /// Silently discard excess messages
12    Drop,
13}
14
15#[derive(Debug, Clone)]
16pub struct ThrottlerConfig {
17    /// Maximum messages per time window
18    pub max_requests: usize,
19    /// Time window duration
20    pub period: Duration,
21    /// Behavior when throttled
22    pub strategy: ThrottleStrategy,
23}
24
25impl ThrottlerConfig {
26    pub fn new(max_requests: usize, period: Duration) -> Self {
27        Self {
28            max_requests,
29            period,
30            strategy: ThrottleStrategy::default(),
31        }
32    }
33
34    pub fn strategy(mut self, s: ThrottleStrategy) -> Self {
35        self.strategy = s;
36        self
37    }
38}