Skip to main content

RateCounter

Struct RateCounter 

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

A lock-free, thread-safe rate counter with current in-flight tracking and peak water mark.

Wraps five atomic values in Arc so the counter is Clone — clones share the same underlying counters, making it easy to pass into concurrent tasks (e.g. [tokio::spawn]).

The speed field stores a true per-second rate as an f64 (encoded via f64::to_bits / f64::from_bits in the AtomicU64), normalised by the sampling interval passed to tick.

The current field tracks the current in-flight or active count. inc / incs increment both total and current simultaneously and update max to the new peak. dec / decs only decrement current without affecting max. tick does not affect current or max.

§Serialisation

RateCounter serialises as a snapshot of the current counter values (total, speed, current, max). Deserialisation produces a fresh, independent counter initialised to those values — it does NOT share atomics with the original. This makes it safe for cross-node transfer (e.g. via gRPC).

Implementations§

Source§

impl RateCounter

Source

pub fn new() -> Self

Creates a new RateCounter with all values initialised to zero and StatsMergeMode::None.

Source

pub fn new_with_mode(mode: StatsMergeMode) -> Self

Creates a new RateCounter with a given merge mode.

Source

pub fn snapshot(&self) -> Self

Creates an independent deep copy (new atomics) with the same values.

Source

pub fn inc(&self)

Increments total, current, and potentially updates max by 1.

Source

pub fn incs(&self, n: u64)

Increments total, current, and potentially updates max by n.

Source

pub fn total(&self) -> u64

Returns the cumulative total count.

Source

pub fn speed(&self) -> f64

Returns the per-second rate computed by the most recent [tick].

Returns 0.0 if [tick] has never been called.

Source

pub fn tick(&self, interval: Duration)

Computes the per-second rate: speed = (total - last_total) / interval.

Call this periodically at a known interval (e.g. every 3 s) to get a true per-second throughput, regardless of the sampling duration.

Source

pub fn reset(&self)

Resets all counters to zero.

Source

pub fn current(&self) -> i64

Returns the current (in-flight / active) count.

Source

pub fn max(&self) -> i64

Returns the peak (historical maximum) of the current count.

Source

pub fn to_json(&self) -> Value

Converts the rate counter to JSON format.

Source

pub fn add(&self, other: &Self)

Sums total, current and max from other into self.

Source

pub fn set(&self, other: &Self)

Replaces all fields with the values from other.

Source

pub fn merge(&self, other: &Self)

Merges other into self according to self.mode.

  • None — no-op.
  • Sum — totals are summed, current / max take the larger value.
  • Max / Min — only current and max are affected.
Source

pub fn dec(&self)

Decrements the current count by 1 (total and max unchanged).

Source

pub fn decs(&self, n: i64)

Decrements the current count by n (total and max unchanged).

Trait Implementations§

Source§

impl Clone for RateCounter

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RateCounter

Source§

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

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

impl Default for RateCounter

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for RateCounter

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for RateCounter

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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.