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
impl RateCounter
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new RateCounter with all values initialised to zero
and StatsMergeMode::None.
Sourcepub fn new_with_mode(mode: StatsMergeMode) -> Self
pub fn new_with_mode(mode: StatsMergeMode) -> Self
Creates a new RateCounter with a given merge mode.
Sourcepub fn snapshot(&self) -> Self
pub fn snapshot(&self) -> Self
Creates an independent deep copy (new atomics) with the same values.
Sourcepub fn speed(&self) -> f64
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.
Sourcepub fn tick(&self, interval: Duration)
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.