Crates.io | ratelimit_rs |
lib.rs | ratelimit_rs |
version | 0.1.4 |
source | src |
created_at | 2021-03-27 03:28:59.906489 |
updated_at | 2021-04-01 15:14:39.989516 |
description | The ratelimit package provides an efficient token bucket implementation |
homepage | |
repository | https://github.com/jimizai/ratelimit_rs |
max_upload_size | |
id | 374057 |
size | 9,071 |
-- use ratelimit::Bucket;
The ratelimit package provides an efficient token bucket implementation. See http://en.wikipedia.org/wiki/Token_bucket.
Submit bugs See https://github.com/jimizai/ratelimit_rs/issues. I will deal with it in time.
Bucket represents a token bucket that fills at a predetermined rate. Methods on Bucket may be called concurrently.
let bucket = Bucket::new(fill_interval: Duration, capacity: u64, quantum: u64, available_tokens: u64);
Bucket::new returns a new token bucket that fills at the rate of one token every fillInterval, up to the given maximum capacity. Both arguments must be positive. The bucket is initially full.
bucket.take_available(count: u64) -> u64;
TakeAvailable takes up to count immediately available tokens from the bucket. It returns the number of tokens removed, or zero if there are no available tokens. It does not block.
bucket.take_max_duration(count: u64, max_wait: Duration) -> (Duration, bool);
TakeMaxDuration is take, except that it will only take tokens from the bucket if the wait time for the tokens is no greater than maxWait.
If it would take longer than maxWait for the tokens to become available, it does nothing and reports false, otherwise it returns the time that the caller should wait until the tokens are actually available, and reports true.
bucket.wait_max_duration(count: u64, max_wait: Duration) -> bool;
WaitMaxDuration is like Wait except that it will only take tokens from the bucket if it needs to wait for no greater than maxWait. It reports whether any tokens have been removed from the bucket If no tokens have been removed, it returns immediately.