pub struct TimeWindowPolicy { /* private fields */ }Expand description
Time-window rate limiting policy.
Allows up to K events within a sliding time window. Events outside the window are automatically expired.
§Example
use tracing_throttle::{TimeWindowPolicy, RateLimitPolicy};
use std::time::{Duration, Instant};
let mut policy = TimeWindowPolicy::new(2, Duration::from_secs(60)).unwrap();
let now = Instant::now();
// First 2 events allowed
assert!(policy.register_event(now).is_allow());
assert!(policy.register_event(now).is_allow());
// 3rd event suppressed (within window)
assert!(policy.register_event(now).is_suppress());
// After window expires, events are allowed again
let after_window = now + Duration::from_secs(61);
assert!(policy.register_event(after_window).is_allow());
assert!(policy.register_event(after_window).is_allow());Implementations§
Source§impl TimeWindowPolicy
impl TimeWindowPolicy
Sourcepub fn new(
max_events: usize,
window_duration: Duration,
) -> Result<Self, PolicyError>
pub fn new( max_events: usize, window_duration: Duration, ) -> Result<Self, PolicyError>
Create a new time-window policy.
§Arguments
max_events- Maximum events allowed in the window (must be > 0)window_duration- Length of the sliding time window (must be > 0)
§Errors
Returns PolicyError::ZeroMaxEvents if max_events is 0.
Returns PolicyError::ZeroWindowDuration if window_duration is 0.
Trait Implementations§
Source§impl Clone for TimeWindowPolicy
impl Clone for TimeWindowPolicy
Source§fn clone(&self) -> TimeWindowPolicy
fn clone(&self) -> TimeWindowPolicy
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TimeWindowPolicy
impl Debug for TimeWindowPolicy
Source§impl PartialEq for TimeWindowPolicy
impl PartialEq for TimeWindowPolicy
Source§impl RateLimitPolicy for TimeWindowPolicy
impl RateLimitPolicy for TimeWindowPolicy
Source§fn register_event(&mut self, timestamp: Instant) -> PolicyDecision
fn register_event(&mut self, timestamp: Instant) -> PolicyDecision
Register a new event occurrence and decide whether to allow or suppress it. Read more
impl StructuralPartialEq for TimeWindowPolicy
Auto Trait Implementations§
impl Freeze for TimeWindowPolicy
impl RefUnwindSafe for TimeWindowPolicy
impl Send for TimeWindowPolicy
impl Sync for TimeWindowPolicy
impl Unpin for TimeWindowPolicy
impl UnwindSafe for TimeWindowPolicy
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more