Crates.io | tocket |
lib.rs | tocket |
version | 0.2.1 |
source | src |
created_at | 2022-02-28 12:47:34.697963 |
updated_at | 2022-05-08 22:25:05.887161 |
description | A implemetation of 'Token Bucket' algorithm |
homepage | |
repository | https://github.com/LazyMechanic/tocket |
max_upload_size | |
id | 540865 |
size | 58,750 |
This library provides implementation of token bucket algorithm and some storage implementations.
[dependencies]
tocket = "0.2"
use tocket::{TokenBucket, InMemoryStorage};
use std::sync::Arc;
use std::time::Duration;
fn main() {
let tb = TokenBucket::new(InMemoryStorage::new(100));
let tb = Arc::new(tb);
for i in 0..4 {
std::thread::spawn({
let tb = Arc::clone(&tb);
move || {
loop {
std::thread::sleep(Duration::from_secs(i));
match tb.try_acquire_one() {
Ok(_) => println!("token acquired, limit not exceeded"),
Err(err) => eprintln!("token acquiring failed: {}", err),
}
}
}
});
}
loop {}
}
redis-impl
- redis storage implementationdistributed-impl
- distributed storage implementation