Skip to main content

camel_api/
throttler.rs

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