pub struct RateLimiter { /* private fields */ }Expand description
Rate limiter for API calls
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(config: RateLimitConfig) -> Self
pub fn new(config: RateLimitConfig) -> Self
Create a new rate limiter
Sourcepub async fn check(&self, provider: &str) -> RateLimitResult
pub async fn check(&self, provider: &str) -> RateLimitResult
Check if a request is allowed (doesn’t consume)
Sourcepub async fn acquire(&self, provider: &str) -> Result<(), RateLimitError>
pub async fn acquire(&self, provider: &str) -> Result<(), RateLimitError>
Acquire a permit (blocks if rate limited)
Sourcepub async fn try_acquire(&self, provider: &str) -> Result<(), RateLimitError>
pub async fn try_acquire(&self, provider: &str) -> Result<(), RateLimitError>
Try to acquire a permit (non-blocking)
Sourcepub async fn try_acquire_with_tokens(
&self,
provider: &str,
estimated_tokens: u32,
) -> Result<(), RateLimitError>
pub async fn try_acquire_with_tokens( &self, provider: &str, estimated_tokens: u32, ) -> Result<(), RateLimitError>
Try to acquire a permit with estimated token usage (atomic check) This prevents race conditions between request counting and token tracking
Sourcepub async fn record_tokens(&self, provider: &str, additional_tokens: u32)
pub async fn record_tokens(&self, provider: &str, additional_tokens: u32)
Record additional token usage after completion Use this to adjust for actual tokens used vs estimated
Sourcepub async fn stats(&self, provider: &str) -> RateLimitStats
pub async fn stats(&self, provider: &str) -> RateLimitStats
Get current usage stats
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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>
Converts
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>
Converts
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