https://github.com/mindeng/async-rate-limiter
Implements a token bucket algorithm that can be used to limit API access frequency. Written in pure Rust.
https://github.com/mindeng/async-rate-limiter
crate frequency-control rate-limit rate-limiter rate-limiting rust
Last synced: 10 months ago
JSON representation
Implements a token bucket algorithm that can be used to limit API access frequency. Written in pure Rust.
- Host: GitHub
- URL: https://github.com/mindeng/async-rate-limiter
- Owner: mindeng
- License: mit
- Created: 2024-08-16T16:58:32.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-08-22T23:09:19.000Z (almost 2 years ago)
- Last Synced: 2024-10-31T11:59:01.958Z (over 1 year ago)
- Topics: crate, frequency-control, rate-limit, rate-limiter, rate-limiting, rust
- Language: Rust
- Homepage:
- Size: 71.3 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# async-rate-limiter
[](https://crates.io/crates/async-rate-limiter)
[](https://docs.rs/async-rate-limiter)
[](LICENSE)
[](https://github.com/mindeng/async-rate-limiter/actions)
async-rate-limiter implements a [token bucket
algorithm](https://en.wikipedia.org/wiki/Token_bucket) that can be used to
limit API access frequency.
## Features
- Simple to use
- Support concurrent access
- Low overhead, the number of tokens is calculated over time
Thanks to Rust’s `async` / `await`, this crate is very simple to use. Just put
your function call after
[`RateLimiter::acquire()`](https://docs.rs/async-rate-limiter/latest/async_rate_limiter/struct.RateLimiter.html#method.acquire).`await`,
then the function will be called with the specified rate limit.
[`RateLimiter`] also implements `Clone` trait, so you can use it in
multiple tasks environment easily.
## Example
Update your `Cargo.toml`:
```toml
[dependencies]
# Change features to ["rt-async-std"] if you are using async-std runtime.
async-rate-limiter = { version = "1", features = ["rt-tokio"] }
```
Here is a simple example:
```rust
use async_rate_limiter::RateLimiter;
use std::time::Duration;
use tokio::spawn;
#[tokio::main]
async fn main() {
let rl = RateLimiter::new(3);
// You can change `burst` at anytime
rl.burst(5);
rl.acquire().await;
println!("Do something that you want to limit the rate ...");
let res = rl.try_acquire();
if res.is_ok() {
println!("Do something that you want to limit the rate ...");
}
// acquire with a timeout
let ok = rl.acquire_with_timeout(Duration::from_secs(10)).await;
if ok {
println!("Do something that you want to limit the rate ...");
}
// Concurrent use
let rl = rl.clone();
spawn(async move {
let res = rl.acquire_with_timeout(Duration::from_millis(340)).await;
assert!(res);
});
}
```
async-rate-limiter can support different async runtimes, tokio & async-std
are supported currently. You can use features to switch async runtimes.