Skip to main content

cratestack_axum/ratelimit/
config.rs

1/// Configuration for a single bucket: capacity (max burst) and refill rate
2/// in tokens per second. Banks running high-frequency back-office traffic
3/// pick large bursts; consumer-facing channels use small bursts to dampen
4/// abuse.
5#[derive(Debug, Clone, Copy)]
6pub struct RateLimitConfig {
7    pub burst: u32,
8    pub refill_per_second: f64,
9}
10
11impl RateLimitConfig {
12    pub fn new(burst: u32, refill_per_second: f64) -> Self {
13        Self {
14            burst,
15            refill_per_second,
16        }
17    }
18}
19
20/// Result of attempting to consume a token. `Allowed` carries the number
21/// of tokens left after consumption; `Throttled` carries seconds the
22/// caller should wait before retrying.
23#[derive(Debug, Clone, Copy, PartialEq)]
24pub enum RateLimitDecision {
25    Allowed { remaining: u32 },
26    Throttled { retry_after_secs: u32 },
27}
28
29/// Sleep helper for tests — exposes the bucket's wall-clock refill model so
30/// the integration tests can exercise both the burst and the throttle path
31/// without depending on real time.
32#[doc(hidden)]
33pub fn _bucket_capacity_for(config: RateLimitConfig) -> u32 {
34    config.burst
35}