pub struct RateLimiter { /* private fields */ }Expand description
Advanced rate limiter for API calls that maintains a request history
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(limit_type: RateLimitType) -> Self
pub fn new(limit_type: RateLimitType) -> Self
Creates a new rate limiter with the specified limit type
Sourcepub fn with_safety_margin(&mut self, safety_margin: f64) -> Self
pub fn with_safety_margin(&mut self, safety_margin: f64) -> Self
Creates a new rate limiter with a custom safety margin
§Arguments
safety_margin- A value between 0.0 and 1.0 representing the percentage of the actual limit to use (e.g., 0.8 means use 80% of the actual limit)
Sourcepub fn limit_type(&self) -> RateLimitType
pub fn limit_type(&self) -> RateLimitType
Returns the rate limit type for this limiter
Sourcepub fn effective_limit(&self) -> usize
pub fn effective_limit(&self) -> usize
Returns the effective request limit (after applying safety margin)
Sourcepub async fn current_request_count(&self) -> usize
pub async fn current_request_count(&self) -> usize
Gets the current number of requests in the time window
Sourcepub async fn time_until_next_request_ms(&self) -> u64
pub async fn time_until_next_request_ms(&self) -> u64
Gets the time until the next request can be made (in milliseconds) Returns 0 if a request can be made immediately
Sourcepub async fn notify_rate_limit_exceeded(&self)
pub async fn notify_rate_limit_exceeded(&self)
Notifies the rate limiter that a rate limit error has been encountered This will cause the rate limiter to enforce a mandatory cooldown period
Sourcepub async fn wait(&self)
pub async fn wait(&self)
Waits if necessary to respect the rate limit This method is thread-safe and can be called from multiple threads concurrently
Sourcepub async fn get_stats(&self) -> RateLimiterStats
pub async fn get_stats(&self) -> RateLimiterStats
Gets statistics about the current rate limit usage
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more