pub struct Throttle { /* private fields */ }Expand description
Token-bucket admission control gate.
The bucket is initialised full, so the first burst of up to
capacity tokens is granted immediately.
Implementations§
Source§impl Throttle
impl Throttle
Sourcepub fn new(capacity: u64, refill_per_sec: u64) -> Self
pub fn new(capacity: u64, refill_per_sec: u64) -> Self
Build a new throttle with the given burst capacity and sustained refill rate. The bucket starts full.
The queue metric label defaults to "default". Use
Throttle::with_name to pick a meaningful label.
Sourcepub fn with_name(name: &'static str, capacity: u64, refill_per_sec: u64) -> Self
pub fn with_name(name: &'static str, capacity: u64, refill_per_sec: u64) -> Self
Build a new throttle with an explicit metric label.
Sourcepub fn capacity(&self) -> u64
pub fn capacity(&self) -> u64
Burst capacity (the maximum number of tokens that may be acquired in one go).
Sourcepub fn refill_per_sec(&self) -> u64
pub fn refill_per_sec(&self) -> u64
Sustained refill rate in tokens per second.
Sourcepub fn available(&self) -> u64
pub fn available(&self) -> u64
Best-effort snapshot of the currently available tokens.
Useful for tests and diagnostics; do not branch on this in
admission code (use Throttle::try_acquire instead).
Sourcepub fn try_acquire(&self, n: u64) -> bool
pub fn try_acquire(&self, n: u64) -> bool
Try to take n tokens. Returns true on success and
false if the bucket does not currently hold n tokens.
Refills the bucket from the elapsed wall-clock interval
before checking.
Requesting n > capacity always returns false: the
bucket can never hold that many tokens, so blocking is
pointless.
Sourcepub async fn acquire(&self, n: u64)
pub async fn acquire(&self, n: u64)
Acquire n tokens, waiting if necessary for the bucket to
refill. The time spent waiting is recorded on the
throttle_wait_seconds histogram.
§Panics
Panics if n > capacity. The bucket can never hold that
many tokens, so an unconditional wait would be a deadlock.
Panics if refill_per_sec is zero and the initial bucket
cannot satisfy the request: the throttle has no way to
ever recover, so a deadlock is the alternative.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Throttle
impl RefUnwindSafe for Throttle
impl Send for Throttle
impl Sync for Throttle
impl Unpin for Throttle
impl UnsafeUnpin for Throttle
impl UnwindSafe for Throttle
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> 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> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.