Struct RateLimiter
pub struct RateLimiter { /* private fields */ }Expand description
A cheap-to-clone async handle around a shared TokenBucket:
clones share the same budget.
Two ways to consume it:
RateLimiter::acquirewaits until the budget allows (pacing);RateLimiter::try_acquirenever waits and reports when to retry (rejecting, e.g. a 429 path).
Time is tracked against a per-limiter epoch taken at construction, using tokio’s clock (so paused-time tests work as expected).
There is no FIFO fairness guarantee between concurrent waiters. Under a sustained stream of smaller acquisitions, a larger acquisition can be repeatedly beaten to newly refilled tokens and starve; callers that need strict waiter fairness must serialize acquisitions above this primitive.
Implementations§
§impl RateLimiter
impl RateLimiter
pub fn new(rate: Rate, burst: u64) -> RateLimiter
Available on crate features grpc and http and std only.
pub fn new(rate: Rate, burst: u64) -> RateLimiter
grpc and http and std only.Create a new RateLimiter with the given Rate and
burst capacity, starting full.
§Panics
Panics if burst is zero.
pub fn from_rate(rate: Rate) -> RateLimiter
Available on crate features grpc and http and std only.
pub fn from_rate(rate: Rate) -> RateLimiter
grpc and http and std only.Create a new RateLimiter with a burst capacity of one period
worth of units (rate.units()): a plain smooth rate.
pub fn rate(&self) -> Rate
Available on crate features grpc and http and std only.
pub fn rate(&self) -> Rate
grpc and http and std only.The configured Rate.
pub fn burst(&self) -> u64
Available on crate features grpc and http and std only.
pub fn burst(&self) -> u64
grpc and http and std only.The configured burst capacity.
pub fn try_acquire(&self, n: u64) -> Acquire
Available on crate features grpc and http and std only.
pub fn try_acquire(&self, n: u64) -> Acquire
grpc and http and std only.Try to spend n units without waiting.
See TokenBucket::try_acquire; the Acquire::RetryAt instant
is in this limiter’s clock and can be turned into a deadline
with RateLimiter::deadline.
pub async fn acquire(&self, n: u64)
Available on crate features grpc and http and std only.
pub async fn acquire(&self, n: u64)
grpc and http and std only.Spend n units, waiting until the budget allows it.
Requests larger than the burst capacity are acquired in burst-sized chunks, so this never fails: it paces. Concurrent acquisitions are not queued fairly; see the type docs.
Dropping the future while it waits for a chunk spends nothing for that chunk. For a request larger than the burst, chunks already acquired before cancellation stay spent. Refunding them could mint capacity when concurrent users spent refill produced while those chunks were held.
pub fn refund(&self, n: u64)
Available on crate features grpc and http and std only.
pub fn refund(&self, n: u64)
grpc and http and std only.Give back up to n previously spent units, saturating at the
burst capacity.
pub fn notified_on_refund(&self) -> RefundWait ⓘ
Available on crate features grpc and http and std only.
pub fn notified_on_refund(&self) -> RefundWait ⓘ
grpc and http and std only.Wait until this limiter is next refunded.
Poll-based users should create this listener before calling
try_acquire, so a concurrent refund cannot land
between observing a deficit and registering for notification.
pub fn deadline(&self, retry_at_nanos: u64) -> Instant
Available on crate features grpc and http and std only.
pub fn deadline(&self, retry_at_nanos: u64) -> Instant
grpc and http and std only.Turn an Acquire::RetryAt instant into a timer deadline,
for poll-based callers.
Trait Implementations§
§impl Clone for RateLimiter
impl Clone for RateLimiter
§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§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§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
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,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§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> ⓘ
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§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a rama_grpc::Request§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§fn and<P, B, E>(self, other: P) -> And<T, P>
fn and<P, B, E>(self, other: P) -> And<T, P>
Policy that returns Action::Follow only if self and other return
Action::Follow. Read more