Skip to main content

Budget

Struct Budget 

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

One connection’s send and receive allowance, refilling each window.

A RATE, not a lifetime total. The thing worth bounding is a peer flooding the kernel — how fast bytes arrive, and how fast the kernel is asked to produce them. A running total bounds that, but it bounds every legitimate use with it: a subscription is meant to run for days and a blob read moves as many bytes as the blob holds, so under a total both die at a threshold that says nothing about whether they misbehaved.

Exceeding the allowance therefore WAITS rather than failing. “Too fast” is answered by going slower; a connection is only killed when it has done something a slower version of would still be wrong. The one exception is a frame larger than a whole window’s allowance, which no amount of waiting would ever admit — that is a misconfiguration, and it fails loudly instead of hanging forever.

Both directions are charged the FULL frame — prefix, kind byte, and body — because that is what the connection actually costs to move. Charging only the body would let a peer spend the allowance on a million one-byte frames and be told it had used a megabyte.

ponytail: a fixed window, so a peer can spend a full allowance at the end of one and again at the start of the next — a 2x burst across the boundary. For a flood guard that is immaterial; a token bucket if smoothing ever matters.

Implementations§

Source§

impl Budget

Source

pub fn new(ingress: usize, egress: usize) -> Self

Source

pub fn with_window(ingress: usize, egress: usize, window: Duration) -> Self

The same allowance over a caller-chosen window. Tests use a short one so a refill is observable; nothing in the daemon does.

Trait Implementations§

Source§

impl Debug for Budget

Source§

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

Formats the value using the given formatter. Read more

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

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more