pub struct RateLimitedFramework<M, R, T = Instant>{ /* 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>
impl<M, R, T> RateLimitedFramework<M, R, T>
Sourcepub fn new(framework: Framework<M, R, T>) -> Self
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
Sourcepub fn trigger_events(
&mut self,
events: &[TriggerEvent],
max_actions_per_second: f64,
current_time: T,
) -> impl Iterator<Item = &TriggerAction<T>>
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 frameworkmax_actions_per_second- Maximum allowed actions per secondcurrent_time- The current time for rate window calculations
§Returns
An iterator over the allowed actions (may be empty if rate limited)
Sourcepub fn framework(&self) -> &Framework<M, R, T>
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.
Sourcepub fn framework_mut(&mut self) -> &mut Framework<M, R, T>
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.