Rate limit guard
Lazy rate limit semaphore (a.k.a fixed window algorithm without queueing) implementation to control your asynchronous code frequency execution
Documentation: lib.rs/raliguard
Overview
use ;
use Semaphore;
// Create a semaphore with restriction `5 tasks per 1 second`
let originl_sem = new;
// Make it sharable between treads (or you can share between tasks)
let shared_sem = new;
// This is a counter that increments when a thread completed
let shared_done_count = new;
// Spawn 15 threads
for _ in 0..15
// So sleep 1 second (add some millis to let threads complete incrementing)
sleep;
let cloned_done_count = shared_done_count.clone;
let current_done = cloned_done_count.lock.unwrap;
// And then maximum 10 threads should be completed
// after 1 second sleeping
// (the first 5 with no delay and the another 5 after 1 second)
assert_eq!;
Features
- Calculated delay for sleep can be used with any async/await runtime backend or with threads
- Minimum memory used to save calls data