rmqtt-utils
Common utilities for the RMQTT MQTT broker: byte sizes, durations, timestamps, node addresses, env var expansion, and atomic counters.
Type aliases
pub type NodeId = u64;
pub type Addr = ByteString;
pub type Timestamp = i64;
pub type TimestampMillis = i64;
Bytesize — human-readable byte size
;
// From<usize>, TryFrom<&str>, FromStr, Deref<Target=usize>, DerefMut
NodeAddr — cluster node address (ID@host:port)
// FromStr: "1@127.0.0.1:1883".parse::<NodeAddr>()?
// Serialize, Deserialize
Free functions
;
// Supported suffixes: G, M, K, B. E.g. "2G512K" -> 2148007936
;
// Supported: ms, s, m, h, d, w, f (fortnight). E.g. "1h30m15s" -> 5415s
; // SystemTime::now().duration_since(UNIX_EPOCH)
; // i64 seconds
;// i64 milliseconds
; // "%Y-%m-%d %H:%M:%S"
;
; // "%Y-%m-%d %H:%M:%S%.3f"
;
;
// Expands ${ENV:VAR_NAME} placeholders using regex. Logs warning for unset vars.
Serde helpers
;
;
;
;
;
;
;
;
Counter / StatsMergeMode
;
// (current, max, merge_mode)
Safety
#![deny(unsafe_code)] — zero unsafe code.
RateCounter — lock‑free throughput & in‑flight tracker
use Duration;
use RateCounter;
let rc = new;
// Task arrives: track throughput, in-flight, and peak
rc.incs;
assert_eq!;
assert_eq!;
assert_eq!;
// Higher peak
rc.incs;
assert_eq!;
// Task completes: in-flight decreases, peak unchanged
rc.decs;
assert_eq!;
assert_eq!;
// Compute per-second rate over a 3 s interval
rc.tick;
assert!;
A pure‑atomics, zero‑lock rate counter that tracks:
total: Cumulative count since construction or reset.speed: Per‑second throughput, computed by callingtick(interval)at a known sampling interval.current: Current in‑flight / active count (incremented byinc/incs, decremented bydec/decs).max: Historical peak of thecurrentfield.
Clone shares the underlying atomics via Arc — ideal for passing into tokio::spawn.
snapshot() creates an independent deep copy with the same values but new atomics.
Serde serialises/deserialises as a snapshot (safe for cross‑node transfer).
| Method | Effect |
|---|---|
inc() / incs(n) |
Increment total and current; update max |
dec() / decs(n) |
Decrement current only |
tick(interval) |
Compute speed = (total - last_total) / interval |
reset() |
Zero all counters |
total() / speed() / current() / max() |
Read individual counters |
snapshot() |
Create independent deep copy |
License
MIT OR Apache-2.0