Skip to main content

SlidingWindowLog

Struct SlidingWindowLog 

Source
pub struct SlidingWindowLog<C = SystemClock>
where C: Clock,
{ /* private fields */ }
Available on crate feature std only.
Expand description

An exact sliding-window-log rate limiter: at most limit units in any trailing window of window.

Construct with new (or per_second), then use the non-blocking try_acquire / peek or the waiting acquire. Time comes from an injectable Clock.

§Examples

use std::time::Duration;
use throttle_net::SlidingWindowLog;

// At most 5 requests in any 1-second window.
let limiter = SlidingWindowLog::new(5, Duration::from_secs(1));
for _ in 0..5 {
    assert!(limiter.try_acquire());
}
assert!(!limiter.try_acquire()); // the 6th in this window is refused

Implementations§

Source§

impl SlidingWindowLog<SystemClock>

Source

pub fn new(limit: u32, window: Duration) -> Self

Creates a limiter admitting at most limit units per trailing window.

A limit of 0 admits nothing; a zero window makes every grant expire immediately (so it behaves as a per-instant limit of limit).

§Examples
use std::time::Duration;
use throttle_net::SlidingWindowLog;

let limiter = SlidingWindowLog::new(100, Duration::from_secs(60)); // 100/min
assert_eq!(limiter.capacity(), 100);
Source

pub fn per_second(rate: u32) -> Self

Creates a limiter admitting at most rate units in any one-second window.

§Examples
use throttle_net::SlidingWindowLog;

let limiter = SlidingWindowLog::per_second(50);
assert!(limiter.try_acquire());
Source§

impl<C> SlidingWindowLog<C>
where C: Clock + Clone,

Source

pub fn with_clock<C2>(self, clock: C2) -> SlidingWindowLog<C2>
where C2: Clock + Clone,

Replaces the time source, for deterministic tests with a ManualClock. The log is reset.

§Examples
use std::sync::Arc;
use std::time::Duration;
use clock_lib::ManualClock;
use throttle_net::SlidingWindowLog;

let clock = Arc::new(ManualClock::new());
let limiter = SlidingWindowLog::new(2, Duration::from_secs(1)).with_clock(clock.clone());

assert!(limiter.try_acquire());
assert!(limiter.try_acquire());
assert!(!limiter.try_acquire());
clock.advance(Duration::from_secs(1)); // the window slides past both grants
assert!(limiter.try_acquire());
Source

pub fn try_acquire(&self) -> bool

Attempts to take one unit without waiting.

Source

pub fn try_acquire_with_cost(&self, cost: u32) -> bool

Attempts to take cost units without waiting (all-or-nothing).

Source

pub fn peek(&self, cost: u32) -> Decision

Reports whether cost units would be admitted now, without recording.

Source

pub fn available(&self) -> u32

Units still admissible in the current window.

Source

pub fn capacity(&self) -> u32

The window limit (the most units admissible at once).

Source§

impl<C> SlidingWindowLog<C>
where C: Clock + Clone,

Source

pub async fn acquire(&self) -> Result<(), ThrottleError>

Available on crate feature runtime only.

Takes one unit, waiting until the window has room.

§Errors

Returns ThrottleError::CostExceedsCapacity when the limit is zero.

Source

pub async fn acquire_with_cost(&self, cost: u32) -> Result<(), ThrottleError>

Available on crate feature runtime only.

Takes cost units, waiting until the window has room.

§Errors

Returns ThrottleError::CostExceedsCapacity when cost exceeds the limit, so it can never be admitted.

Trait Implementations§

Source§

impl<C> Limiter for SlidingWindowLog<C>
where C: Clock + Clone + Send + Sync,

Source§

fn peek(&self, cost: u32) -> Decision

Reports whether cost tokens would be granted now, without taking them. Read more
Source§

fn acquire_cost(&self, cost: u32) -> Decision

Attempts to take cost tokens now, deducting them on success. Read more
Source§

fn available(&self) -> u32

Returns the number of whole tokens available right now. Read more
Source§

fn capacity(&self) -> u32

Returns the most tokens this limiter can ever hold at once (its burst ceiling). A request whose cost exceeds this can never be granted.

Auto Trait Implementations§

§

impl<C = SystemClock> !Freeze for SlidingWindowLog<C>

§

impl<C> RefUnwindSafe for SlidingWindowLog<C>
where C: RefUnwindSafe,

§

impl<C> Send for SlidingWindowLog<C>

§

impl<C> Sync for SlidingWindowLog<C>

§

impl<C> Unpin for SlidingWindowLog<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for SlidingWindowLog<C>
where C: UnsafeUnpin,

§

impl<C> UnwindSafe for SlidingWindowLog<C>
where C: 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
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