pub struct BucketConfig { /* private fields */ }Expand description
The parameters that define a token bucket.
A bucket holds up to capacity tokens (the burst ceiling) and accrues
refill_amount tokens every refill_period (the sustained rate). It starts
with initial tokens. These four numbers fully describe the bucket’s
behaviour; everything else is accounting.
Construct one with BucketConfig::new, which rejects values that cannot
describe a working bucket (see BucketError). The Tier-1 constructors
Bucket::per_second and
Bucket::per_duration build a config for you
for the common case.
initial is clamped to capacity: asking a bucket to start with more
tokens than it can hold simply starts it full.
§Examples
use better_bucket::BucketConfig;
use std::time::Duration;
// 500-token burst ceiling, refilling 100 tokens/second, starting empty.
let config = BucketConfig::new(500, 100, Duration::from_secs(1), 0)?;
assert_eq!(config.capacity(), 500);
assert_eq!(config.initial(), 0);Implementations§
Source§impl BucketConfig
impl BucketConfig
Sourcepub fn new(
capacity: u32,
refill_amount: u32,
refill_period: Duration,
initial: u32,
) -> Result<Self, BucketError>
pub fn new( capacity: u32, refill_amount: u32, refill_period: Duration, initial: u32, ) -> Result<Self, BucketError>
Builds a validated configuration.
§Parameters
capacity— the maximum tokens the bucket holds (its burst size). Must be greater than zero.refill_amount— tokens added everyrefill_period. Must be greater than zero.refill_period— the period over whichrefill_amountaccrues. Must be non-zero.initial— tokens present at construction, clamped tocapacity.
§Errors
BucketError::ZeroCapacityifcapacityis0.BucketError::ZeroRefillAmountifrefill_amountis0.BucketError::ZeroRefillPeriodifrefill_periodis zero.
§Examples
use better_bucket::BucketConfig;
use std::time::Duration;
let config = BucketConfig::new(100, 100, Duration::from_secs(1), 100)?;
assert_eq!(config.capacity(), 100);initial larger than capacity is clamped rather than rejected:
use better_bucket::BucketConfig;
use std::time::Duration;
let config = BucketConfig::new(100, 100, Duration::from_secs(1), 999)?;
assert_eq!(config.initial(), 100); // clamped to capacitySourcepub const fn capacity(&self) -> u32
pub const fn capacity(&self) -> u32
The maximum number of tokens the bucket holds (its burst ceiling).
Sourcepub const fn refill_amount(&self) -> u32
pub const fn refill_amount(&self) -> u32
The number of tokens added each refill_period.
Sourcepub const fn refill_period(&self) -> Duration
pub const fn refill_period(&self) -> Duration
The period over which refill_amount accrues.
Trait Implementations§
Source§impl Clone for BucketConfig
impl Clone for BucketConfig
Source§fn clone(&self) -> BucketConfig
fn clone(&self) -> BucketConfig
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BucketConfig
impl Debug for BucketConfig
Source§impl PartialEq for BucketConfig
impl PartialEq for BucketConfig
Source§fn eq(&self, other: &BucketConfig) -> bool
fn eq(&self, other: &BucketConfig) -> bool
self and other values to be equal, and is used by ==.