Skip to main content

RateLimit

Struct RateLimit 

Source
pub struct RateLimit {
    pub budget: Budget,
    pub window_duration: u64,
    pub max_clock: u64,
    pub window_start: u64,
    pub clock: u64,
}
Expand description

A per-window operation bound: at most max_per_window acquires per window_duration clock units, the window re-anchored at window_start.

Fields§

§budget: Budget

Budget component for operations admitted in the current window.

§window_duration: u64

WindowDuration (constant): the window length in clock units.

§max_clock: u64

MaxClock (constant): the model’s clock bound (Tick’s guard).

§window_start: u64

window_start ∈ Nat: when the current window was anchored.

§clock: u64

clock ∈ Nat: the runtime-given clock.

Implementations§

Source§

impl RateLimit

Source

pub fn new( max_per_window: u64, window_duration: u64, max_clock: u64, ) -> RateLimit

Construct the initial state: count = 0, window_start = 0, clock = 0. Realises the TLA+ Init predicate and establishes all three invariants.

Source

pub fn try_acquire(&mut self) -> bool

Try to acquire one operation. The whole three-branch TLA+ IF is this one method: on an expired window, re-anchor AND grant in the same step (window_start’ = clock, count’ = 1); on headroom, grant (count’ + 1); otherwise reject (UNCHANGED). Returns whether the acquire was granted.

Source

pub fn tick(&mut self)

Advance the runtime-given clock by one. Realises the TLA+ Tick action: its guard (clock < MaxClock) is a requires, so the action is callable exactly when the TLA+ action is enabled.

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.