stream_shared/stats.rs
1use std::sync::{
2 atomic::{AtomicU64, Ordering},
3 Arc,
4};
5
6/// Runtime metrics for a `SharedStream`.
7///
8/// A lightweight, read-only view exposing the number of active clones.
9/// Obtain a `Stats` handle via `SharedStream::stats()`. Values use relaxed
10/// atomics and are intended for diagnostics.
11#[cfg_attr(docsrs, doc(cfg(feature = "stats")))]
12#[derive(Debug, Clone)]
13pub struct Stats {
14 active_clones: Arc<AtomicU64>,
15}
16
17impl Stats {
18 // Create a new, empty stats instance.
19 pub(crate) fn new() -> Self {
20 Self {
21 active_clones: Arc::new(AtomicU64::new(1)),
22 }
23 }
24
25 pub(crate) fn increment(&self) {
26 self.active_clones.fetch_add(1, Ordering::Relaxed);
27 }
28
29 pub(crate) fn decrement(&self) {
30 self.active_clones.fetch_sub(1, Ordering::Relaxed);
31 }
32
33 /// Returns the number of active clones for the associated `SharedStream`.
34 ///
35 /// The count includes the instance this `Stats` handle was obtained from,
36 /// so a value of `1` indicates there are no additional clones.
37 pub fn active_clones(&self) -> u64 {
38 self.active_clones.load(Ordering::Relaxed)
39 }
40}