pub struct RateLimiter { /* private fields */ }Expand description
Rate limiter using sliding window algorithm
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_requests: u32, per_duration: Duration) -> Self
pub fn new(max_requests: u32, per_duration: Duration) -> Self
Create a new rate limiter
§Arguments
max_requests- Maximum number of requests allowedper_duration- Time window for the rate limit
§Example
use lastfm_client::client::RateLimiter;
use std::time::Duration;
// Allow max 5 requests per second
let limiter = RateLimiter::new(5, Duration::from_secs(1));Sourcepub async fn acquire(&self)
pub async fn acquire(&self)
Acquire permission to make a request
This will block until a slot is available according to the rate limit
§Panics
Panics if acquiring a semaphore permit fails (should be unreachable under normal operation).
Sourcepub fn max_requests(&self) -> u32
pub fn max_requests(&self) -> u32
Get the maximum requests per duration
Sourcepub fn per_duration(&self) -> Duration
pub fn per_duration(&self) -> Duration
Get the time duration for the rate limit
Sourcepub fn current_count(&self) -> u32
pub fn current_count(&self) -> u32
Get the current request count in the current window
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