#[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(&self) -> f64
pub fn rate(&self) -> f64
Get current rate (events per second in current window)
Note: This method is #[must_use]
. The returned rate conveys the
current measurement; ignoring it may indicate a logic bug.
Sourcepub fn rate_per_second(&self) -> f64
pub fn rate_per_second(&self) -> f64
Get rate per second
Note: #[must_use]
— see Self::rate
.
Sourcepub fn rate_per_minute(&self) -> f64
pub fn rate_per_minute(&self) -> f64
Get rate per minute
Note: #[must_use]
. The returned value reflects the current window
state; dropping it may hide incorrect flow.
Sourcepub fn rate_per_hour(&self) -> f64
pub fn rate_per_hour(&self) -> f64
Get rate per hour
Note: #[must_use]
. The returned value reflects the current window
state; dropping it may hide incorrect flow.
Sourcepub fn total(&self) -> u64
pub fn total(&self) -> u64
Get total events since creation
Note: #[must_use]
. Total is often used for invariants and sanity
checks; ignoring it may indicate a bug.
Sourcepub fn exceeds_rate(&self, limit: f64) -> bool
pub fn exceeds_rate(&self, limit: f64) -> bool
Check if rate exceeds limit
Note: #[must_use]
. The boolean result determines control flow; if you
don’t branch on it, consider whether the call is needed.
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
Note: #[must_use]
. The boolean result determines control flow; if you
don’t branch on it, consider whether the call is needed.
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
Note: #[must_use]
. The boolean result indicates whether a tick was
recorded; ignoring it may hide throttling decisions.
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
Note: #[must_use]
. The boolean result indicates whether a burst was
recorded; ignoring it may hide throttling decisions.
Sourcepub fn stats(&self) -> RateStats
pub fn stats(&self) -> RateStats
Get comprehensive statistics
Note: #[must_use]
. Statistics summarize current state; dropping the
result may indicate a logic bug.