pub struct RateLimiter { /* private fields */ }Expand description
In-memory rate limiter that manages token buckets for different endpoints Thread-safe but not cross-process
Implementations§
Source§impl RateLimiter
impl RateLimiter
pub fn new() -> Self
Sourcepub fn new_with_safety_factor(safety_factor: f64) -> Self
pub fn new_with_safety_factor(safety_factor: f64) -> Self
Create a new rate limiter with a custom safety factor Safety factor > 1.0 adds buffer time between requests For example, 1.1 means 10% longer wait times
Sourcepub fn set_safety_factor(&mut self, safety_factor: f64)
pub fn set_safety_factor(&mut self, safety_factor: f64)
Set the safety factor for rate limiting This adds a buffer to prevent 429 errors due to timing inconsistencies
Sourcepub fn get_safety_factor(&self) -> f64
pub fn get_safety_factor(&self) -> f64
Get the current safety factor
Sourcepub async fn wait(
&self,
identifier: &str,
rate: f64,
burst: u32,
) -> Result<RateLimitGuard>
pub async fn wait( &self, identifier: &str, rate: f64, burst: u32, ) -> Result<RateLimitGuard>
Wait for a token to become available for the given endpoint and return a guard When the guard is dropped, record_response will be called automatically
Sourcepub async fn check_token_availability(&self, identifier: &str) -> Result<bool>
pub async fn check_token_availability(&self, identifier: &str) -> Result<bool>
Check if a token is available without consuming it
Sourcepub async fn get_token_status(&self) -> Result<HashMap<String, (f64, f64, u32)>>
pub async fn get_token_status(&self) -> Result<HashMap<String, (f64, f64, u32)>>
Get current token status for all endpoints Returns (tokens, rate, burst) for each endpoint
Sourcepub async fn active_buckets_count(&self) -> usize
pub async fn active_buckets_count(&self) -> usize
Get the number of active buckets
Trait Implementations§
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