Skip to main content

BucketConfig

Struct BucketConfig 

Source
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

Source

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 every refill_period. Must be greater than zero.
  • refill_period — the period over which refill_amount accrues. Must be non-zero.
  • initial — tokens present at construction, clamped to capacity.
§Errors
§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 capacity
Source

pub const fn capacity(&self) -> u32

The maximum number of tokens the bucket holds (its burst ceiling).

Source

pub const fn refill_amount(&self) -> u32

The number of tokens added each refill_period.

Source

pub const fn refill_period(&self) -> Duration

The period over which refill_amount accrues.

Source

pub const fn initial(&self) -> u32

The number of tokens the bucket starts with.

Trait Implementations§

Source§

impl Clone for BucketConfig

Source§

fn clone(&self) -> BucketConfig

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BucketConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for BucketConfig

Source§

fn eq(&self, other: &BucketConfig) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for BucketConfig

Source§

impl Eq for BucketConfig

Source§

impl StructuralPartialEq for BucketConfig

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error