pub struct BucketState {
tokens: u128,
last: Instant,
}Expand description
The mutable half of a token bucket: what it holds and when it was last refilled.
Session-scoped and shared by every stream of a class, so it is the
scheduler’s state and not a stream’s. Kept separate from
BucketConfig so the configuration stays comparable and cloneable
while the state stays exactly one owner’s.
Fields§
§tokens: u128Held tokens in nano-bytes. Never exceeds burst_bytes * NANO and
never goes negative — the type forbids it, and so does the rate
bound the bucket exists to hold.
last: InstantThe instant tokens was last brought up to date.
Implementations§
Source§impl BucketState
impl BucketState
Sourcepub fn new(burst_bytes: u64, now: Instant) -> Self
pub fn new(burst_bytes: u64, now: Instant) -> Self
A bucket that starts full at burst_bytes, as of now.
Starting full is what makes the first unit of a session go
immediately; starting empty would put a burst_bytes / rate_bps
delay in front of every stream and read as a broken proxy.
Sourcepub fn available_bytes(&self) -> u64
pub fn available_bytes(&self) -> u64
Whole bytes currently held, as of the last charge.
Does not refill: this is a read of recorded state, not a clock reading, so it stays usable from a test that fabricates its own instants.
Trait Implementations§
Source§impl Clone for BucketState
impl Clone for BucketState
Source§fn clone(&self) -> BucketState
fn clone(&self) -> BucketState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more