diff --git a/Cargo.lock b/Cargo.lock index 3ef8117..5ef834c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,6 +127,15 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "async-rate-limiter" +version = "0.1.0" +dependencies = [ + "async-std", + "futures", + "tokio", +] + [[package]] name = "async-std" version = "1.12.0" @@ -619,15 +628,6 @@ dependencies = [ "proc-macro2", ] -[[package]] -name = "rate-limiter" -version = "0.1.0" -dependencies = [ - "async-std", - "futures", - "tokio", -] - [[package]] name = "rustc-demangle" version = "0.1.24" diff --git a/Cargo.toml b/Cargo.toml index 09ad01a..a65b1e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "rate-limiter" +name = "async-rate-limiter" version = "0.1.0" edition = "2021" license-file = "LICENSE" description = "Implements a token bucket algorithm that can be used to limit API access frequency. Written in pure Rust." -homepage = "https://github.com/mindeng/rate-limiter-rs" -repository = "https://github.com/mindeng/rate-limiter-rs" +homepage = "https://github.com/mindeng/async-rate-limiter" +repository = "https://github.com/mindeng/async-rate-limiter" [dependencies] futures = "0.3.30" diff --git a/README.md b/README.md index 53ced2c..ab96043 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,32 @@ -# rate-limiter +# async-rate-limit -[![crates.io](https://img.shields.io/crates/v/rate-limiter.svg)](https://crates.io/crates/rate-limiter) -[![Documentation](https://docs.rs/rate-limiter/badge.svg)](https://docs.rs/rate-limiter) +[![crates.io](https://img.shields.io/crates/v/async-rate-limiter.svg)](https://crates.io/crates/async-rate-limiter) +[![Documentation](https://docs.rs/async-rate-limiter/badge.svg)](https://docs.rs/async-rate-limiter) [![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) -[![CI](https://github.com/mindeng/rate-limiter-rs/actions/workflows/rust.yml/badge.svg)](https://github.com/mindeng/rate-limiter-rs/actions) +[![CI](https://github.com/mindeng/async-rate-limiter/actions/workflows/rust.yml/badge.svg)](https://github.com/mindeng/async-rate-limiter/actions) -rate-limiter implements a [token bucket +async-rate-limiter implements a [token bucket algorithm](https://en.wikipedia.org/wiki/Token_bucket) that can be used to limit API access frequency. -Thanks to Rust’s async capabilities, this feature is very simple to use. Just -put your operations after [`RateLimiter::acquire()`].`await`, then the -frequency control of the operations has been done. +# 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.39.2", features = ["rt-tokio"] } +``` + +Thanks to Rust’s async functionality, this crate is very simple to use. +Just put your function call after [`RateLimiter::acquire()`].`await`, then +the function will be called with the specified rate limit. Here is a simple example: ```rust -use rate_limiter::RateLimiter; +use async_rate_limiter::RateLimiter; use std::time::Duration; #[tokio::main] @@ -26,14 +36,15 @@ async fn main() { let ok = rl.acquire(None).await; assert!(ok); println!("Do something that you want to limit the rate ..."); - + + // acquire with a timeout let ok = rl.acquire(Some(Duration::from_secs(10))).await; if ok { println!("Do something that you want to limit the rate ..."); } } + ``` -Please refer to -[![Documentation](https://docs.rs/rate-limiter/badge.svg)](https://docs.rs/rate-limiter) -for more details. +async-rate-limiter can support different async runtimes, tokio & async-std +are supported currently. You can use features to switch async runtimes. diff --git a/src/lib.rs b/src/lib.rs index 95b97e9..3540abe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,15 +1,25 @@ -//! rate-limiter implements a [token bucket +//! async-rate-limiter implements a [token bucket //! algorithm](https://en.wikipedia.org/wiki/Token_bucket) that can be used to //! limit API access frequency. //! -//! Thanks to Rust’s async capabilities, this feature is very simple to use. -//! Just put your operations after [`RateLimiter::acquire()`].`await`, then the -//! frequency control of the operations has been done. +//! # 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.39.2", features = ["rt-tokio"] } +//! ``` +//! +//! Thanks to Rust’s async functionality, this crate is very simple to use. +//! Just put your function call after [`RateLimiter::acquire()`].`await`, then +//! the function will be called with the specified rate limit. //! //! Here is a simple example: //! //! ```rust -//! use rate_limiter::RateLimiter; +//! use async_rate_limiter::RateLimiter; //! use std::time::Duration; //! //! #[tokio::main] @@ -19,7 +29,8 @@ //! let ok = rl.acquire(None).await; //! assert!(ok); //! println!("Do something that you want to limit the rate ..."); -//! +//! +//! // acquire with a timeout //! let ok = rl.acquire(Some(Duration::from_secs(10))).await; //! if ok { //! println!("Do something that you want to limit the rate ..."); @@ -27,6 +38,9 @@ //! } //! //! ``` +//! +//! async-rate-limiter can support different async runtimes, tokio & async-std +//! are supported currently. You can use features to switch async runtimes. mod token_bucket; pub use token_bucket::TokenBucketRateLimiter as RateLimiter;