pub struct TimeBucketedCounter<K> { /* private fields */ }tracker only.Expand description
Per-key event counter over a sliding time window.
The window is divided into fixed-width buckets; counts are
summed across buckets for queries. Lazily evicts buckets
older than now - window on demand.
Implementations§
Source§impl<K> TimeBucketedCounter<K>
impl<K> TimeBucketedCounter<K>
Sourcepub fn new(window: Duration, bucket_width: Duration, capacity: usize) -> Self
pub fn new(window: Duration, bucket_width: Duration, capacity: usize) -> Self
Construct a new counter.
window = total observation period; bucket_width =
resolution; capacity = max distinct keys held across
all buckets (best-effort, enforced by per-bucket cap).
bucket_width must be ≥ 1 ns; window should be
≥ bucket_width.
Sourcepub fn new_unbounded(window: Duration, bucket_width: Duration) -> Self
pub fn new_unbounded(window: Duration, bucket_width: Duration) -> Self
Unbounded capacity convenience constructor — equivalent
to Self::new(window, bucket_width, usize::MAX). Prefer
Self::new with an explicit cap when memory pressure
matters; this avoids the cap arithmetic in code paths
that don’t need it. New in 0.12.0.
Sourcepub fn count(&self, key: &K, now: Timestamp) -> u64
pub fn count(&self, key: &K, now: Timestamp) -> u64
Sum counts for key across buckets within
[now - window, now]. Buckets older than the window are
excluded.
Sourcepub fn entries_above(
&self,
threshold: u64,
now: Timestamp,
) -> impl Iterator<Item = (&K, u64)> + '_
pub fn entries_above( &self, threshold: u64, now: Timestamp, ) -> impl Iterator<Item = (&K, u64)> + '_
Iterate keys whose summed count is >= threshold over
the active window.
Sourcepub fn evict_expired(&mut self, now: Timestamp)
pub fn evict_expired(&mut self, now: Timestamp)
Drop buckets older than now - window.
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Current number of distinct keys held across all live buckets (approximate; double-counts a key that appears in multiple buckets).