tocket

Crates.iotocket
lib.rstocket
version0.2.1
sourcesrc
created_at2022-02-28 12:47:34.697963
updated_at2022-05-08 22:25:05.887161
descriptionA implemetation of 'Token Bucket' algorithm
homepage
repositoryhttps://github.com/LazyMechanic/tocket
max_upload_size
id540865
size58,750
Artem (LazyMechanic)

documentation

https://docs.rs/tocket

README

Tocket

crates.io docs.rs build

This library provides implementation of token bucket algorithm and some storage implementations.

[dependencies]
tocket = "0.2"

Example

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 {}
}

Features

  • redis-impl - redis storage implementation
  • distributed-impl - distributed storage implementation

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Commit count: 29

cargo fmt