pub struct WriteMeter<C: Clock = DefaultClock> {
limits: WriteLimits,
ops: RateLimiter<String, HashMapStateStore<String>, C, NoOpMiddleware<<C as Clock>::Instant>>,
bytes: RateLimiter<String, HashMapStateStore<String>, C, NoOpMiddleware<<C as Clock>::Instant>>,
clock: C,
last_swept: Mutex<Instant>,
}Expand description
The two buckets for every account that has written lately. A bucket that has refilled is dropped, since a missing one starts full anyway.
Fields§
§limits: WriteLimits§ops: RateLimiter<String, HashMapStateStore<String>, C, NoOpMiddleware<<C as Clock>::Instant>>§bytes: RateLimiter<String, HashMapStateStore<String>, C, NoOpMiddleware<<C as Clock>::Instant>>§clock: C§last_swept: Mutex<Instant>Implementations§
Source§impl WriteMeter
impl WriteMeter
Sourcepub fn new(limits: WriteLimits) -> Self
pub fn new(limits: WriteLimits) -> Self
A meter on the system’s monotonic clock.
Source§impl<C: Clock + Clone> WriteMeter<C>
impl<C: Clock + Clone> WriteMeter<C>
Sourcepub fn with_clock(limits: WriteLimits, clock: C) -> Self
pub fn with_clock(limits: WriteLimits, clock: C) -> Self
A meter on clock, which a test can advance by hand.
§Panics
If a rate or burst in limits is zero, a burst is past u32::MAX, or
a rate refills a token in under a nanosecond. Overflows if a slow rate
meets a large burst. WriteLimits::check refuses each of these.
Sourcepub fn limits(&self) -> WriteLimits
pub fn limits(&self) -> WriteLimits
The limits this meter enforces.
Sourcepub fn charge(&self, did: &str, ops: u32, bytes: u64) -> Result<(), Refusal>
pub fn charge(&self, did: &str, ops: u32, bytes: u64) -> Result<(), Refusal>
Spends ops operations and bytes bytes of did’s buckets, or
refuses and spends nothing more.
Operations are spent first. A write the byte bucket refuses keeps them spent.
Sourcefn sweep(&self)
fn sweep(&self)
Drops refilled buckets, at most once per SWEEP_EVERY, so the maps
hold the accounts writing now rather than every account that has.
fn spend( &self, limiter: &RateLimiter<String, HashMapStateStore<String>, C, NoOpMiddleware<<C as Clock>::Instant>>, key: &String, bucket: Bucket, wanted: u64, ) -> Result<(), Refusal>
Trait Implementations§
Auto Trait Implementations§
impl<C = MonotonicClock> !Freeze for WriteMeter<C>
impl<C = MonotonicClock> !RefUnwindSafe for WriteMeter<C>
impl<C> Send for WriteMeter<C>where
C: Send,
impl<C> Sync for WriteMeter<C>where
C: Sync,
impl<C> Unpin for WriteMeter<C>
impl<C> UnsafeUnpin for WriteMeter<C>where
C: UnsafeUnpin,
<C as Clock>::Instant: UnsafeUnpin,
impl<C> UnwindSafe for WriteMeter<C>where
C: UnwindSafe,
<C as Clock>::Instant: UnwindSafe,
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<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