#[repr(align(64))]pub struct RateMeter { /* private fields */ }
Expand description
Ultra-fast rate meter with sliding window calculations
Tracks events per second/minute/hour with minimal overhead. Cache-line aligned to prevent false sharing.
Implementations§
Source§impl RateMeter
impl RateMeter
Sourcepub fn with_window(window: Duration) -> Self
pub fn with_window(window: Duration) -> Self
Create rate meter with custom window size
Sourcepub fn tick(&self)
pub fn tick(&self)
Record an event - THE FASTEST PATH
This is optimized for maximum speed:
- Single atomic increment for total
- Lazy window updates only when needed
- Branch prediction friendly
Sourcepub fn try_tick(&self) -> Result<()>
pub fn try_tick(&self) -> Result<()>
Try to record a single event with overflow checks
Returns Err(MetricsError::Overflow)
if incrementing the total or any
current window counter would overflow. On success returns Ok(())
.
Example
use metrics_lib::{RateMeter, MetricsError};
let m = RateMeter::new();
m.try_tick().unwrap();
assert_eq!(m.total(), 1);
Sourcepub fn try_tick_n(&self, n: u32) -> Result<()>
pub fn try_tick_n(&self, n: u32) -> Result<()>
Try to record N events at once with overflow checks
- Returns
Ok(())
on success. - Returns
Err(MetricsError::Overflow)
if incrementing any of the involved counters would overflow (total_events
,current_second_events
,current_minute_events
, orcurrent_hour_events
).
Example
use metrics_lib::{RateMeter, MetricsError};
let m = RateMeter::new();
assert!(m.try_tick_n(5).is_ok());
assert_eq!(m.total(), 5);
Sourcepub fn rate_per_second(&self) -> f64
pub fn rate_per_second(&self) -> f64
Get rate per second
Sourcepub fn rate_per_minute(&self) -> f64
pub fn rate_per_minute(&self) -> f64
Get rate per minute
Sourcepub fn rate_per_hour(&self) -> f64
pub fn rate_per_hour(&self) -> f64
Get rate per hour
Sourcepub fn exceeds_rate(&self, limit: f64) -> bool
pub fn exceeds_rate(&self, limit: f64) -> bool
Check if rate exceeds limit
Sourcepub fn can_allow(&self, n: u32, limit: f64) -> bool
pub fn can_allow(&self, n: u32, limit: f64) -> bool
Check if we can allow N more events without exceeding limit
Sourcepub fn tick_if_under_limit(&self, limit: f64) -> bool
pub fn tick_if_under_limit(&self, limit: f64) -> bool
Rate limiting - tick only if under limit
Sourcepub fn try_tick_if_under_limit(&self, limit: f64) -> Result<bool>
pub fn try_tick_if_under_limit(&self, limit: f64) -> Result<bool>
Try to tick if under limit, with overflow checks
Attempts to record a single event only if the current rate would not
exceed limit
. Returns:
Ok(true)
if the event was recorded.Ok(false)
if the event would exceed the limit (no change made).Err(MetricsError::Overflow)
if counters would overflow while recording.
Example
use metrics_lib::RateMeter;
let m = RateMeter::new();
assert!(m.try_tick_if_under_limit(10.0).unwrap());
Sourcepub fn tick_burst_if_under_limit(&self, n: u32, limit: f64) -> bool
pub fn tick_burst_if_under_limit(&self, n: u32, limit: f64) -> bool
Burst rate limiting - allow N events if under limit