TimeWindow

Struct TimeWindow 

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

Time window for tracking events within a specific time period.

§Examples

use chie_shared::TimeWindow;

let mut window = TimeWindow::new(1000, 100); // 1 second window, max 100 events

// Track events at different timestamps
window.add_event(1000);
window.add_event(1100);
window.add_event(1200);

// Count events within the window
assert_eq!(window.count_events(1500), 3);

// Events older than window size are cleaned up
assert_eq!(window.count_events(2500), 0);

// Check rate limiting
let mut limiter = TimeWindow::new(1000, 5);
for i in 0..5 {
    limiter.add_event(1000 + i * 10);
}
assert!(limiter.would_exceed_limit(1050, 5));

Implementations§

Source§

impl TimeWindow

Source

pub fn new(window_size_ms: u64, max_events: usize) -> Self

Create a new time window.

§Arguments
  • window_size_ms - Window size in milliseconds
  • max_events - Maximum number of events to track
Source

pub fn add_event(&mut self, timestamp_ms: i64)

Add an event at the current time.

Source

pub fn count_events(&mut self, now_ms: i64) -> usize

Get the count of events within the window from the given timestamp.

Source

pub fn would_exceed_limit(&mut self, now_ms: i64, limit: usize) -> bool

Check if adding a new event would exceed the given limit.

Source

pub fn events_per_second(&mut self, now_ms: i64) -> f64

Get events per second rate.

Source

pub fn clear(&mut self)

Clear all events.

Source

pub fn oldest_event_age_ms(&self, now_ms: i64) -> Option<u64>

Get the age of the oldest event in milliseconds.

Source

pub fn is_full(&self) -> bool

Check if the window is full.

Trait Implementations§

Source§

impl Clone for TimeWindow

Source§

fn clone(&self) -> TimeWindow

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TimeWindow

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.