Skip to main content

RateLimitedFramework

Struct RateLimitedFramework 

Source
pub struct RateLimitedFramework<M, R, T = Instant>
where M: AsRef<[Machine]>, R: RngCore, T: Instant, T::Duration: Sub<Output = T::Duration>,
{ /* private fields */ }
Expand description

A rate-limited wrapper around the Maybenot framework.

This struct wraps a Framework and applies rate limiting to the actions returned by trigger_events. It uses a sliding window algorithm to track the rate of events and blocks actions when the rate exceeds the specified limit.

The rate limiter tracks events across a 1-second sliding window, using the previous window’s count and the current window’s count to calculate the effective rate. This approach prevents burst traffic from overwhelming the system while allowing sustained traffic up to the limit.

§Example

use maybenot::{Framework, RateLimitedFramework, Machine, TriggerEvent};
use std::time::Instant;

let machines = vec![];
let framework = Framework::new(machines, 0.0, 0.0, Instant::now(), rand::rng())?;
let mut rate_limited = RateLimitedFramework::new(framework);

let events = [TriggerEvent::NormalSent];
let actions: Vec<_> = rate_limited
    .trigger_events(&events, 10.0, Instant::now())
    .collect();

Implementations§

Source§

impl<M, R, T> RateLimitedFramework<M, R, T>
where M: AsRef<[Machine]>, R: RngCore, T: Instant, T::Duration: Sub<Output = T::Duration>,

Source

pub fn new(framework: Framework<M, R, T>) -> Self

Creates a new rate-limited framework wrapper.

Initializes the rate limiter with zero counts for both the previous and current windows, and sets the current window start time to the framework’s current time.

§Arguments
  • framework - The underlying framework to wrap with rate limiting
§Returns

A new RateLimitedFramework instance

Source

pub fn trigger_events( &mut self, events: &[TriggerEvent], max_actions_per_second: f64, current_time: T, ) -> impl Iterator<Item = &TriggerAction<T>>

Triggers events in the framework with rate limiting applied.

This method forwards events to the underlying framework and applies rate limiting to the returned actions. It uses a sliding window algorithm to track the rate of events over time and blocks actions when the rate exceeds the specified limit.

The sliding window calculation uses the formula from Cloudflare’s approach: rate = (prev * (1s - elapsed) / 1s) + current

Events are always processed (allowing machines to transition states), but actions may be dropped if the rate limit is exceeded.

§Arguments
  • events - The events to trigger in the framework
  • max_actions_per_second - Maximum allowed actions per second
  • current_time - The current time for rate window calculations
§Returns

An iterator over the allowed actions (may be empty if rate limited)

Source

pub fn framework(&self) -> &Framework<M, R, T>

Returns a reference to the underlying framework.

This provides read-only access to the wrapped framework instance.

Source

pub fn framework_mut(&mut self) -> &mut Framework<M, R, T>

Returns a mutable reference to the underlying framework.

This provides mutable access to the wrapped framework instance for advanced use cases that need to modify the framework state directly.

Auto Trait Implementations§

§

impl<M, R, T> Freeze for RateLimitedFramework<M, R, T>
where T: Freeze, R: Freeze, M: Freeze, <T as Instant>::Duration: Freeze,

§

impl<M, R, T> RefUnwindSafe for RateLimitedFramework<M, R, T>

§

impl<M, R, T> Send for RateLimitedFramework<M, R, T>
where T: Send, R: Send, M: Send, <T as Instant>::Duration: Send,

§

impl<M, R, T> Sync for RateLimitedFramework<M, R, T>
where T: Sync, R: Sync, M: Sync, <T as Instant>::Duration: Sync,

§

impl<M, R, T> Unpin for RateLimitedFramework<M, R, T>
where T: Unpin, R: Unpin, M: Unpin, <T as Instant>::Duration: Unpin,

§

impl<M, R, T> UnsafeUnpin for RateLimitedFramework<M, R, T>

§

impl<M, R, T> UnwindSafe for RateLimitedFramework<M, R, T>

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

Source§

fn vzip(self) -> V