Crates.io | rhythm |
lib.rs | rhythm |
version | 0.1.0 |
source | src |
created_at | 2023-07-29 06:27:56.452359 |
updated_at | 2023-07-30 17:44:46.461836 |
description | Rate limiter which allows VIPs, written in Rust |
homepage | |
repository | |
max_upload_size | |
id | 929128 |
size | 32,764 |
Rhythm's RateLimiter
is a thread-safe rate limiting library implemented in Rust. It allows you to limit the rate of operations, such as requests to a server, by associating these operations with keys. Each key has a "bucket" of tokens, and each operation consumes a token. When the bucket is empty, the operations are limited.
Mutex
to ensure that it can be safely used from multiple threads.First, create a new RateLimiter
, tuned to your use case:
let rate_limiter: RateLimiter<MyKeyType> = RateLimiter::new(
bucket_size,
refill_rate,
refill_interval
);
Then, use the request
method to perform an operation:
if rate_limiter.request(my_key) {
// The operation is allowed.
} else {
// The operation is not allowed.
}
In this example, my_key
is the key associated with the operation. The request
method returns true
if the operation is allowed (i.e., the bucket associated with the key is not empty), and false
otherwise.
By default, all keys will have the rate limiter's default bucket size, refill rate, and refill interval.
To customize this, you can mark a key as a VIP:
rate_limiter.set_vip(
vip_key.clone(),
vip_bucket_size,
vip_refill_rate
);
Add the following to your Cargo.toml:
Run cargo add rhythm
to add the latest version to your Cargo.toml
.
Then, run cargo build
to build your project.
Performance improvements:
Functionality improvements:
Rhythm's Rate Limiter is licensed under the MIT License. See LICENSE for more information.