| Crates.io | invocation-counter |
| lib.rs | invocation-counter |
| version | 1.0.0 |
| created_at | 2024-12-06 16:06:43.398885+00 |
| updated_at | 2025-08-08 12:15:35.077955+00 |
| description | Datastructure to answer to: how many times a function has been called in the last X minutes? |
| homepage | https://crates.io/crates/invocation-counter |
| repository | https://github.com/oramasearch/invocation-counter |
| max_upload_size | |
| id | 1474422 |
| size | 37,504 |
A high-performance, thread-safe data structure for tracking function invocation counts over sliding time windows. Perfect for rate limiting, monitoring, and analytics use cases.
Add this to your Cargo.toml:
[dependencies]
invocation-counter = "*"
use invocation_counter::InvocationCounter;
// Create a counter with 8 slots × 16 time units = 128-unit sliding window
let counter = InvocationCounter::new(3, 4); // 2^3 slots, 2^4 time units per slot
// Register function calls
counter.register(10); // Called at time 10
counter.register(25); // Called at time 25
counter.register(50); // Called at time 50
// Query how many calls in the last 128 time units
assert_eq!(counter.count_in(0, 100), 3); // All calls within range
Time units are abstract - they can represent milliseconds, seconds, minutes, or any consistent measurement. The counter works with u64 timestamps where larger values represent later points in time.
The counter tracks invocations within a sliding time window that moves forward with each query. The window size is determined by:
Window Size = 2^slot_count_exp × 2^slot_size_exp
slot_count_exp: Exponent for number of slots (2^slot_count_exp total slots)slot_size_exp: Exponent for time units per slot (2^slot_size_exp time units per slot)Trade-offs:
The counter provides approximate counts due to its interval-based design:
Licensed under the Apache License, Version 2.0. See LICENSE file for details.