Skip to main content

RateLimiter

Struct RateLimiter 

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

Token bucket rate limiter.

Implements the token bucket algorithm for rate limiting:

  • Tokens are added at a steady rate up to a maximum (burst) capacity
  • Each operation consumes one token
  • If no tokens are available, behavior depends on backpressure strategy

Implementations§

Source§

impl RateLimiter

Source

pub fn new(config: RateLimitConfig) -> Self

Creates a new rate limiter with the given configuration.

Source

pub fn with_rate(entities_per_second: f64) -> Self

Creates a rate limiter with a simple rate (entities per second).

Source

pub fn disabled() -> Self

Creates a disabled rate limiter (always allows).

Source

pub fn acquire(&mut self) -> RateLimitAction

Acquires a token, blocking if necessary.

Returns the action taken to acquire the token.

Source

pub fn try_acquire(&mut self) -> Option<RateLimitAction>

Tries to acquire a token without blocking.

Returns Some(action) if a token was acquired, None if rate limited.

Source

pub fn acquire_timeout(&mut self, timeout: Duration) -> Option<RateLimitAction>

Acquires a token with a timeout.

Returns the action taken, or None if the timeout was exceeded.

Source

pub fn stats(&self) -> RateLimiterStats

Returns the current statistics.

Source

pub fn reset(&mut self)

Resets the rate limiter to initial state.

Source

pub fn available_tokens(&self) -> f64

Returns the current number of available tokens.

Source

pub fn config(&self) -> &RateLimitConfig

Returns the configuration.

Source

pub fn set_rate(&mut self, entities_per_second: f64)

Updates the rate limit.

Source

pub fn set_enabled(&mut self, enabled: bool)

Enables or disables the rate limiter.

Source

pub fn process_buffer(&mut self) -> Vec<Duration>

Processes the buffer, releasing items as tokens become available.

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 = 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V