pub struct RateLimiter { /* private fields */ }Expand description
Token bucket rate limiter.
Implements the token bucket algorithm for rate limiting:
- Tokens are added at a steady rate up to a maximum (burst) capacity
- Each operation consumes one token
- If no tokens are available, behavior depends on backpressure strategy
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimitConfig) -> Self
pub fn new(config: RateLimitConfig) -> Self
Creates a new rate limiter with the given configuration.
Sourcepub fn with_rate(entities_per_second: f64) -> Self
pub fn with_rate(entities_per_second: f64) -> Self
Creates a rate limiter with a simple rate (entities per second).
Sourcepub fn acquire(&mut self) -> RateLimitAction
pub fn acquire(&mut self) -> RateLimitAction
Acquires a token, blocking if necessary.
Returns the action taken to acquire the token.
Sourcepub fn try_acquire(&mut self) -> Option<RateLimitAction>
pub fn try_acquire(&mut self) -> Option<RateLimitAction>
Tries to acquire a token without blocking.
Returns Some(action) if a token was acquired, None if rate limited.
Sourcepub fn acquire_timeout(&mut self, timeout: Duration) -> Option<RateLimitAction>
pub fn acquire_timeout(&mut self, timeout: Duration) -> Option<RateLimitAction>
Acquires a token with a timeout.
Returns the action taken, or None if the timeout was exceeded.
Sourcepub fn stats(&self) -> RateLimiterStats
pub fn stats(&self) -> RateLimiterStats
Returns the current statistics.
Sourcepub fn available_tokens(&self) -> f64
pub fn available_tokens(&self) -> f64
Returns the current number of available tokens.
Sourcepub fn config(&self) -> &RateLimitConfig
pub fn config(&self) -> &RateLimitConfig
Returns the configuration.
Sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enables or disables the rate limiter.
Sourcepub fn process_buffer(&mut self) -> Vec<Duration>
pub fn process_buffer(&mut self) -> Vec<Duration>
Processes the buffer, releasing items as tokens become available.
Auto Trait Implementations§
impl Freeze for RateLimiter
impl RefUnwindSafe for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin 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