pub struct RateLimiter { /* private fields */ }Expand description
A configurable rate limiter for controlling request rates.
This rate limiter supports multiple strategies:
- Token bucket: Continuous token replenishment with burst capacity
- Fixed window: Fixed request count per time window
- Custom strategies via the RateLimitStrategy trait
§Example
use riglr_core::util::{RateLimiter, RateLimitStrategyType};
use std::time::Duration;
// Default token bucket strategy
let rate_limiter = RateLimiter::new(10, Duration::from_secs(60));
// Check rate limit for a user
rate_limiter.check_rate_limit("user123")?;
// Use fixed window strategy
let fixed_limiter = RateLimiter::builder()
.strategy(RateLimitStrategyType::FixedWindow)
.max_requests(100)
.time_window(Duration::from_secs(60))
.build();Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests: usize, time_window: Duration) -> Self
pub fn new(max_requests: usize, time_window: Duration) -> Self
Create a new rate limiter with the default token bucket strategy
Sourcepub fn with_strategy<S: RateLimitStrategy + 'static>(strategy: S) -> Self
pub fn with_strategy<S: RateLimitStrategy + 'static>(strategy: S) -> Self
Create a rate limiter with a custom strategy
Sourcepub fn builder() -> RateLimiterBuilder
pub fn builder() -> RateLimiterBuilder
Create a new rate limiter builder for advanced configuration
Sourcepub fn reset_client(&self, client_id: &str)
pub fn reset_client(&self, client_id: &str)
Reset rate limit for a specific client
Sourcepub fn get_request_count(&self, client_id: &str) -> usize
pub fn get_request_count(&self, client_id: &str) -> usize
Get current request count for a client
Sourcepub fn strategy_name(&self) -> &str
pub fn strategy_name(&self) -> &str
Get the name of the current strategy
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto 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