RateMeter

Struct RateMeter 

Source
#[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

Source

pub fn new() -> Self

Create new rate meter with 1-second window

Source

pub fn with_window(window: Duration) -> Self

Create rate meter with custom window size

Source

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
Source

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);
Source

pub fn tick_n(&self, n: u32)

Record N events at once

Source

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, or current_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);
Source

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.

Source

pub fn rate_per_second(&self) -> f64

Get rate per second

Note: #[must_use] — see Self::rate.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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());
Source

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.

Source

pub fn reset(&self)

Reset all counters

Source

pub fn stats(&self) -> RateStats

Get comprehensive statistics

Note: #[must_use]. Statistics summarize current state; dropping the result may indicate a logic bug.

Source

pub fn age(&self) -> Duration

Get age since creation

Note: #[must_use]. Age is frequently used in rate calculations; don’t call this for side effects.

Source

pub fn is_empty(&self) -> bool

Check if rate meter is empty (no events recorded)

Note: #[must_use]. The boolean result determines control flow.

Trait Implementations§

Source§

impl Debug for RateMeter

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for RateMeter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for RateMeter

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Send for RateMeter

Source§

impl Sync for RateMeter

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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.