pub struct RateLimiter { /* private fields */ }Expand description
Token-bucket rate limiter backed by governor.
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(
name: impl Into<String>,
per_second: u32,
burst: u32,
) -> Result<RateLimiter, AppError>
pub fn new( name: impl Into<String>, per_second: u32, burst: u32, ) -> Result<RateLimiter, AppError>
Create a rate limiter that allows per_second requests/second with a burst capacity of burst.
§Errors
Returns an error when per_second or burst is zero.
Sourcepub fn from_config(config: RateLimiterConfig) -> Result<RateLimiter, AppError>
pub fn from_config(config: RateLimiterConfig) -> Result<RateLimiter, AppError>
Create a rate limiter from a configuration object.
§Errors
Returns an error when config.per_second or config.burst is zero.
Sourcepub fn check(&self) -> Result<(), AppError>
pub fn check(&self) -> Result<(), AppError>
Non-blocking check: returns Ok(()) if a token was acquired,
or Err(AppError::rate_limited()) if the bucket is empty.
§Errors
Returns AppError::rate_limited when no token is currently available.
Sourcepub async fn until_ready(
&self,
cancel: Option<CancellationToken>,
) -> Result<(), AppError>
pub async fn until_ready( &self, cancel: Option<CancellationToken>, ) -> Result<(), AppError>
Async wait: blocks until a token is available or cancel fires.
§Errors
Returns an error when the cancellation token fires before a token is available.
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 (const: unstable) · 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 UnsafeUnpin for RateLimiter
impl UnwindSafe for RateLimiter
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::RequestCreates a shared type from an unshared type.