invocation-counter

Crates.ioinvocation-counter
lib.rsinvocation-counter
version0.1.0
sourcesrc
created_at2024-12-06 16:06:43.398885
updated_at2024-12-06 16:06:43.398885
descriptionDatastructure to answer to: how many times a function has been called in the last X minutes?
homepagehttps://crates.io/crates/invocation-counter
repositoryhttps://github.com/oramasearch/invocation-counter
max_upload_size
id1474422
size35,164
Michele Riva (micheleriva)

documentation

https://docs.rs/invocation-counter

README

Invocation counter

Rust

This data structure responses the following question: how many times a function has been called in the last X minutes?

Installation

cargo add invocation-counter

Example

use invocation_counter::Counter;

fn main() {
    // 4 is the group_shift_factor
    // 16 is the number of buckets
    let counter = Counter::<16, 4>::new(4);

    let mut now = 0; // Instant::now().elapsed().as_secs();
    counter.increment_by_one(now);

    now += 1; // Simulate a second passing
    counter.increment_by_one(now);

    assert_eq!(counter.get_count_till(now), 2);

    now += 2_u64.pow(4); // Simulate 16 seconds passing
    counter.increment_by_one(now);

    now += 1; // Simulate a second passing
    counter.increment_by_one(now);

    assert_eq!(counter.get_count_till(now), 4);

    now += 2_u64.pow(4) * 16; // Move foward for a while...
    counter.increment_by_one(now);
    assert_eq!(counter.get_count_till(now), 1); // The counter should have reset
}
Commit count: 4

cargo fmt