Struct HTB

Source
pub struct HTB<T> {
    pub unit_cost: u64,
    /* private fields */
}
Expand description

Hierarchical Token Bucket structure

You can advance time for HTB structure using advance and advance_ns and examine/alter internal state using peek/take.

When several buckets are feeding from a single parent earlier one gets a priority

Fields§

§unit_cost: u64

Normalized unit cost, each nanosecond corresponds to this many units

Implementations§

Source§

impl<T> HTB<T>
where T: Copy + Eq + PartialEq, usize: From<T>,

Source

pub fn new(tokens: &[BucketCfg<T>]) -> Result<Self, Error>

Create HTB for a given bucket configuration

Buckets should be given in depth first search traversal order:

  • root with parent set to None
  • higher priority child of the root
  • followed by high priority child of the child, if any, etc.
  • followed by the next child
§Errors

If bucket configuration is invalid - returns an Error type describing a problem

Source

pub fn drain(&mut self)

Drain all the available tokens

Source

pub fn refill(&mut self)

Refill all the available buckets to full capacity

Source

pub fn advance_ns(&mut self, time_diff: u64)

Advance time by number of nanoseconds

Updates internal structure, see also advance

§Performance

Update cost is O(N) where N is number of buckets

Source

pub fn advance(&mut self, time_diff: Duration)

Advance time by Duration

Updates internal structure, see also advance_ns

Source

pub fn peek(&self, label: T) -> bool

Check if there’s at least one token available at index T

See also peek_n

Source

pub fn available(&self, label: T) -> u64

Shows how many tokens are available at

Source

pub fn peek_n(&self, label: T, cnt: usize) -> bool

Check if there’s at least cnt tokens available at index T

See also peek

Source

pub fn take(&mut self, label: T) -> bool

Consume a single token from T

See also take_n

Source

pub fn take_n(&mut self, label: T, cnt: usize) -> bool

Consume cnt tokens from T

See also take

Trait Implementations§

Source§

impl<T: Clone> Clone for HTB<T>

Source§

fn clone(&self) -> HTB<T>

Returns a copy of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for HTB<T>

Source§

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

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

impl<T> Index<T> for HTB<T>
where usize: From<T>,

Source§

type Output = u64

The returned type after indexing.
Source§

fn index(&self, index: T) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T: PartialEq> PartialEq for HTB<T>

Source§

fn eq(&self, other: &HTB<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<T> StructuralPartialEq for HTB<T>

Auto Trait Implementations§

§

impl<T> Freeze for HTB<T>

§

impl<T> RefUnwindSafe for HTB<T>
where T: RefUnwindSafe,

§

impl<T> Send for HTB<T>
where T: Send,

§

impl<T> Sync for HTB<T>
where T: Sync,

§

impl<T> Unpin for HTB<T>
where T: Unpin,

§

impl<T> UnwindSafe for HTB<T>
where T: UnwindSafe,

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.