Rate limit guard

Lazy rate limit semaphore implementation to control your asynchronous code frequency execution


Documentation: lib.rs/raliguard

Overview

```rust use std::{thread, sync, time};

use raliguard::Semaphore;

// Create a semaphore with restriction 5 tasks per 1 second let originlsem = Semaphore::new(5, time::Duration::fromsecs(1));

// Make it sharable between treads (or you can share between tasks) let sharedsem = sync::Arc::new( sync::Mutex::new(originlsem) );

// This is a counter that increments when a thread completed let shareddonecount = sync::Arc::new(sync::Mutex::new(0));

// Spawn 10 threads for _ in 0..10 { let clonedsem = sharedsem.clone(); let cloneddonestate = shareddonecount.clone(); let thread = thread::spawn(move || { let mut localsem = clonedsem.lock().unwrap();

    // Get required delay
    let calculated_delay = local_sem.calc_delay();
    drop(local_sem);

    // If delay exists, sleep it
    if let Some(delay) = calculated_delay {
        dbg!(&delay);
        thread::sleep(delay);
    }

    // Mark the thread is done
    let mut local_done_count = cloned_done_state.lock().unwrap();
    *local_done_count += 1;

});

}

// So sleep 1 second thread::sleep(time::Duration::new(1, 0)); let cloneddonecount = shareddonecount.clone(); let currentdone = cloneddone_count.lock().unwrap();

// And then maximum 5 threads should be completed // after 1 second sleeping // (<= for clocks infelicity) asserteq!(*currentdone <= 5, true); ```

Features