agentmux_srv_core/backend/reactive/
rate_limiter.rs1use std::time::{Duration, Instant};
7
8pub(super) struct RateLimiter {
9 tokens: u32,
10 max_tokens: u32,
11 last_refill: Instant,
12}
13
14impl RateLimiter {
15 pub(super) fn new(max_tokens: u32) -> Self {
16 Self {
17 tokens: max_tokens,
18 max_tokens,
19 last_refill: Instant::now(),
20 }
21 }
22
23 pub(super) fn check(&mut self) -> bool {
24 let now = Instant::now();
25 let elapsed = now.duration_since(self.last_refill);
26 if elapsed >= Duration::from_secs(1) {
27 self.tokens = self.max_tokens;
28 self.last_refill = now;
29 }
30 if self.tokens > 0 {
31 self.tokens -= 1;
32 true
33 } else {
34 false
35 }
36 }
37}