pub struct RateLimiter { /* private fields */ }Expand description
An async token-bucket rate limiter that throttles aggregate transfer throughput.
Construct with RateLimiter::new. When max_bytes_per_sec is None (or
Some(0)), the limiter is unlimited and acquire
returns immediately. Otherwise tokens refill at max_bytes_per_sec per
second, allowing a burst of up to ~1 second’s worth of budget.
The limiter is Arc-shareable and Clone (cloning shares the same
underlying bucket).
Implementations§
Source§impl RateLimiter
impl RateLimiter
Sourcepub fn new(max_bytes_per_sec: Option<u64>) -> Self
pub fn new(max_bytes_per_sec: Option<u64>) -> Self
Builds a limiter. None (or Some(0)) yields an unlimited, no-op
limiter whose acquire never waits.
Sourcepub async fn set_rate(&self, bytes_per_sec: Option<u64>)
pub async fn set_rate(&self, bytes_per_sec: Option<u64>)
Retunes the limiter’s aggregate byte-rate cap live, so an adaptive controller can raise or lower throttling between operations.
None(orSome(0)) switches the limiter to unlimited: the rate and capacity drop to0and the bucket is emptied, so the nextacquireis a no-op.Some(r > 0)installs (or replaces) a bucket refilling atrbytes/sec with ~1 second of burst capacity. Switching from unlimited to limited primes the bucket full (tokens = capacity) so a freshly throttled limiter still allows one immediate burst.
Calling set_rate is the only way the rate changes after [new];
limiters that never call it behave exactly as before.
Sourcepub async fn acquire(&self, n: u64)
pub async fn acquire(&self, n: u64)
Blocks until n bytes of budget are available, refilling the bucket at
the configured rate. Unlimited limiters return immediately.
A single request larger than the bucket capacity is still satisfied: the bucket is allowed to go negative and the caller waits out the deficit, so throttling is correct even for objects bigger than one second’s worth of budget.
Trait Implementations§
Source§impl Clone for RateLimiter
impl Clone for RateLimiter
Source§fn clone(&self) -> RateLimiter
fn clone(&self) -> RateLimiter
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RateLimiter
impl !UnwindSafe for RateLimiter
impl Freeze for RateLimiter
impl Send for RateLimiter
impl Sync for RateLimiter
impl Unpin for RateLimiter
impl UnsafeUnpin 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
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>
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 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>
T in a tonic::Request