Skip to main content

TokenBucket

Struct TokenBucket 

Source
pub struct TokenBucket { /* private fields */ }
Expand description

A token bucket: it earns a number of tokens an hour up to its size, and a relay spends one on each message it lets through.

Implementations§

Source§

impl TokenBucket

Source

pub const fn new(reload_rate: u8, size: u16) -> TokenBucket

A bucket that earns reload_rate tokens an hour and holds size, starting full.

§Arguments
  • reload_rate - tokens earned an hour.
  • size - the most tokens it holds.
§Returns

The bucket.

Source

pub const fn coded( reload_rate: u8, bucket_size: u8, unlimited: u8, ) -> TokenBucket

A bucket as a relay command codes it, starting full.

§Arguments
  • reload_rate - tokens earned an hour, or unlimited for no limit.
  • bucket_size - the coded size multiplier, TS011-1.0.1 table 55; only its low two bits are read.
  • unlimited - the rate that means no limit: UNLIMITED_DEVICE_RATE or UNLIMITED_RELAY_RATE.
§Returns

The bucket.

Source

pub const fn unlimited() -> TokenBucket

A bucket that never runs out.

§Returns

The bucket.

Source

pub const fn reload_rate(&self) -> Option<u8>

Returns how many tokens the bucket earns an hour.

§Returns

The rate, or None for no limit.

Source

pub const fn size(&self) -> u16

Returns the most tokens the bucket holds.

§Returns

The size, 0 for a bucket with no limit.

Source

pub const fn tokens(&self) -> u16

Returns the tokens the bucket holds now.

§Returns

The count, 0 for a bucket with no limit.

Source

pub const fn has_token(&self) -> bool

Reports whether a message may pass.

§Returns

true for a bucket with no limit or a token left.

Source

pub const fn disabled(&self) -> bool

Reports whether the bucket is empty and never refills, which a relay reports as forwarding disabled.

§Returns

true for an empty bucket that earns nothing.

Source

pub fn take(&mut self) -> bool

Spends a token.

§Returns

false, spending nothing, when the bucket is empty.

Source

pub fn reload(&mut self, hours: u64)

Adds what the bucket earns over some hours, keeping no more than its size.

§Arguments
  • hours - the whole hours since the last reload.
Source

pub fn set_tokens(&mut self, tokens: u16)

Sets the tokens, keeping no more than the bucket’s size.

§Arguments
  • tokens - the count.
Source

pub fn refill_one_hour(&mut self)

Sets the tokens to one hour’s reload, as a relay does when it starts scanning.

Trait Implementations§

Source§

impl Clone for TokenBucket

Source§

fn clone(&self) -> TokenBucket

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 Copy for TokenBucket

Source§

impl Debug for TokenBucket

Source§

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

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

impl Eq for TokenBucket

Source§

impl Hash for TokenBucket

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for TokenBucket

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TokenBucket

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> Same for T

Source§

type Output = T

Should always be Self
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.