Skip to main content

Pacer

Struct Pacer 

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

A token bucket in bits, refilled from elapsed time.

Unlike upstream’s rate.Limiter, nothing here reads a clock: the budget is a pure function of the instants handed in. That is what makes a release schedule reproducible in a test rather than merely eventually-correct — pion cannot assert its own schedule without a fake clock.

Implementations§

Source§

impl Pacer

Source

pub fn new(bits_per_second: f64) -> Self

A bucket paced at bits_per_second, starting full.

Starting full rather than empty lets a connection send immediately instead of waiting out one burst’s worth of accumulation, which is what upstream’s limiter does too.

Source

pub fn with_burst_bits(self, burst_bits: f64) -> Self

Override the burst size, in bits.

A burst set here is kept across rate changes. Deriving it from the rate is only a default; once the caller has said how much it is willing to release at once, an estimator raising the rate must not quietly widen that.

Source

pub fn target_bitrate(&self) -> f64

The rate currently being paced at, in bits per second.

Source

pub fn set_target_bitrate(&mut self, bits_per_second: f64)

Change the rate.

Synchronous and immediate, because this is what a bandwidth estimator drives: it computes a new target and the very next release must respect it. Accumulated budget is kept but clamped to the new burst, so lowering the rate cannot leave a large budget behind that would let a burst out at the old rate.

A burst set through Pacer::with_burst_bits survives this; only a burst that was derived from the rate follows the rate.

Source

pub fn refill(&mut self, now: Instant)

Bring the budget up to now.

Source

pub fn can_afford(&self, bits: f64) -> bool

Whether bits can be sent now.

Source

pub fn consume(&mut self, bits: f64)

Spend bits from the budget.

The budget is allowed to go negative on a packet larger than a full burst; otherwise such a packet could never be sent at all, and a stalled queue is worse than a momentary overshoot.

Source

pub fn time_until_affordable(&self, bits: f64) -> Option<Duration>

How long until bits becomes affordable.

Duration::ZERO when it already is. At a zero rate nothing ever becomes affordable, which the caller must treat as “no deadline” rather than waiting forever.

Source

pub fn affordable_at(&self, bits: f64) -> Option<Instant>

The instant bits becomes affordable, given the last refill.

Source

pub fn budget_bits(&self) -> f64

The available budget, in bits.

Source

pub fn can_release(&self, bits: f64) -> bool

Whether bits may be released now.

A packet larger than a full burst can never be afforded — the budget caps at the burst — so it is released once the budget has refilled to that cap, which is as long as waiting can possibly help. Gating on a full budget rather than releasing unconditionally is what keeps a run of oversized packets paced: each drives the budget negative and the next must wait for it to recover, so they leave at the target rate instead of all at once.

Source

pub fn releasable_at(&self, bits: f64) -> Option<Instant>

The instant bits may be released, per Pacer::can_release.

Source

pub fn burst_bits(&self) -> f64

The maximum the budget can accumulate to, in bits.

Anything larger than this can never be afforded by waiting, however long the wait — the budget caps here — which is why release is asked for through Pacer::can_release rather than Pacer::can_afford.

Trait Implementations§

Source§

impl Clone for Pacer

Source§

fn clone(&self) -> Pacer

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 Pacer

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Pacer

§

impl RefUnwindSafe for Pacer

§

impl Send for Pacer

§

impl Sync for Pacer

§

impl Unpin for Pacer

§

impl UnsafeUnpin for Pacer

§

impl UnwindSafe for Pacer

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 = !

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.