pub struct RateLimiter { /* private fields */ }Expand description
Lock-free token-bucket rate limiter using monotonic timestamps for smooth refill.
Uses std::time::Instant instead of SystemTime so the refill clock is
immune to NTP adjustments and pre-epoch system clocks.
Thread-safe via AtomicU64 — no mutexes, no allocations on the hot path.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests_per_sec: u64) -> Self
pub fn new(max_requests_per_sec: u64) -> Self
Create a rate limiter with the given maximum requests per second.
Sourcepub fn try_acquire(&self) -> bool
pub fn try_acquire(&self) -> bool
Atomically consume one token, returning true if the request is
allowed.
Sourcepub fn max_tokens(&self) -> u64
pub fn max_tokens(&self) -> u64
Maximum token capacity.
Sourcepub fn current_tokens(&self) -> u64
pub fn current_tokens(&self) -> u64
Current token count (snapshot — may change immediately after reading).
Auto Trait Implementations§
impl !Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more