pub struct LeakBucket {
pub(crate) scale: AtomicU64,
pub(crate) ltat: AtomicU64,
pub(crate) origin: Instant,
pub(crate) total_allowed_quotas: AtomicU64,
pub(crate) total_waited_nanos: AtomicI64,
}
Expand description
A GCRA-like leak bucket visual scheduler that never deny request even whose weight is larger than tau and only count TAT.
Fields§
§scale: AtomicU64
Weight scale per 1.0 unit quota in nanosecond.
scale is always non-zero.
scale = rate / 1 (in second)
ltat: AtomicU64
Last request’s TAT (Theoretical Arrival Time) in nanosecond.
origin: Instant
Zero time instant.
total_allowed_quotas: AtomicU64
Total allowed quotas.
total_waited_nanos: AtomicI64
Total waited nanos.
Implementations§
source§impl LeakBucket
impl LeakBucket
pub(crate) const NANO: u64 = 1_000_000_000u64
sourcepub(crate) fn scale(rate: NonZeroU64) -> u64
pub(crate) fn scale(rate: NonZeroU64) -> u64
calculate the weight scale per 1.0 unit quota in nanosecond.
sourcepub(crate) fn new(rate: NonZeroU64) -> Self
pub(crate) fn new(rate: NonZeroU64) -> Self
Create a new GCRA-like leak bucket visual scheduler with given rate.
sourcepub(crate) fn check(&self, quota: u64) -> Result<(), Duration>
pub(crate) fn check(&self, quota: u64) -> Result<(), Duration>
Check if the request with the given quota is supposed to be allowed at the moment.
On success, the quota will be consumed. The caller is supposed to proceed the quota.
On failure, the minimal duration to retry check()
is returned.
sourcepub(crate) fn _avg_wait_nanos_per_quota(&self) -> i64
pub(crate) fn _avg_wait_nanos_per_quota(&self) -> i64
Average wait time per quota.
Positive value indicates waits, negative value indicates there is spare rate limit.
sourcepub(crate) fn _reset_stats(&self)
pub(crate) fn _reset_stats(&self)
Reset statistics.
sourcepub(crate) fn _update(&self, rate: NonZeroU64)
pub(crate) fn _update(&self, rate: NonZeroU64)
Update rate limit with the given rate.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for LeakBucket
impl RefUnwindSafe for LeakBucket
impl Send for LeakBucket
impl Sync for LeakBucket
impl Unpin for LeakBucket
impl UnwindSafe for LeakBucket
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<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> 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
§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
source§impl<M> MetricVecRelabelExt for M
impl<M> MetricVecRelabelExt for M
source§fn relabel(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M>
fn relabel( self, metric_level: MetricLevel, relabel_threshold: MetricLevel, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level
.source§fn relabel_n(
self,
metric_level: MetricLevel,
relabel_threshold: MetricLevel,
relabel_num: usize,
) -> RelabeledMetricVec<M>
fn relabel_n( self, metric_level: MetricLevel, relabel_threshold: MetricLevel, relabel_num: usize, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level_relabel_n
.source§fn relabel_debug_1(
self,
relabel_threshold: MetricLevel,
) -> RelabeledMetricVec<M>
fn relabel_debug_1( self, relabel_threshold: MetricLevel, ) -> RelabeledMetricVec<M>
RelabeledMetricVec::with_metric_level_relabel_n
with metric_level
set to
MetricLevel::Debug
and relabel_num
set to 1.