diff --git a/README.md b/README.md index 310fd9b..758ebc5 100644 --- a/README.md +++ b/README.md @@ -24,16 +24,17 @@ You can use the fine-grained rate limiter like so: ```rust #[tokio::main] async fn main() { - let period = std::time::Duration::from_secs(5); - let rate_limiter = MultiRateLimiter::new(period); - - // This completes instantly - rate_limiter.throttle("foo", || computation()).await; + let period = std::time::Duration::from_secs(5); + let rate_limiter = MultiRateLimiter::new(period); - // This completes instantly - rate_limiter.throttle("bar", || computation()).await; + // This completes instantly + rate_limiter.throttle("foo", || computation()).await; - // This takes 5 seconds to complete because the key "foo" is rate limited - rate_limiter.throttle("foo", || computation()).await; + // This completes instantly + rate_limiter.throttle("bar", || computation()).await; + + // This takes 5 seconds to complete because the key "foo" is rate limited + rate_limiter.throttle("foo", || computation()).await; } -``` \ No newline at end of file +``` + diff --git a/src/lib.rs b/src/lib.rs index e46e8e2..1b9b972 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,17 +11,17 @@ //! //! #[tokio::main] //! async fn main() { -//! let period = std::time::Duration::from_secs(5); -//! let rate_limiter = MultiRateLimiter::new(period); -//! -//! // This completes instantly -//! rate_limiter.throttle("foo", || computation()).await; +//! let period = std::time::Duration::from_secs(5); +//! let rate_limiter = MultiRateLimiter::new(period); //! -//! // This completes instantly -//! rate_limiter.throttle("bar", || computation()).await; +//! // This completes instantly +//! rate_limiter.throttle("foo", || computation()).await; //! -//! // This takes 5 seconds to complete because the key "foo" is rate limited -//! rate_limiter.throttle("foo", || computation()).await; +//! // This completes instantly +//! rate_limiter.throttle("bar", || computation()).await; +//! +//! // This takes 5 seconds to complete because the key "foo" is rate limited +//! rate_limiter.throttle("foo", || computation()).await; //! } //! //! async fn computation() { } diff --git a/src/multi.rs b/src/multi.rs index 62814b7..e9b66a6 100644 --- a/src/multi.rs +++ b/src/multi.rs @@ -17,17 +17,17 @@ use std::time::Duration; /// /// #[tokio::main] /// async fn main() { -/// let period = std::time::Duration::from_secs(5); -/// let rate_limiter = MultiRateLimiter::new(period); -/// -/// // This completes instantly -/// rate_limiter.throttle("foo", || computation()).await; +/// let period = std::time::Duration::from_secs(5); +/// let rate_limiter = MultiRateLimiter::new(period); /// -/// // This completes instantly -/// rate_limiter.throttle("bar", || computation()).await; +/// // This completes instantly +/// rate_limiter.throttle("foo", || computation()).await; /// -/// // This takes 5 seconds to complete because the key "foo" is rate limited -/// rate_limiter.throttle("foo", || computation()).await; +/// // This completes instantly +/// rate_limiter.throttle("bar", || computation()).await; +/// +/// // This takes 5 seconds to complete because the key "foo" is rate limited +/// rate_limiter.throttle("foo", || computation()).await; /// } /// /// async fn computation() { } @@ -67,7 +67,7 @@ impl MultiRateLimiter { /// async fn do_work() { /* some computation */ } /// /// async fn throttle_by_key(the_key: u32, limiter: Arc>) { - /// limiter.throttle(the_key, || do_work()).await + /// limiter.throttle(the_key, || do_work()).await /// } pub async fn throttle(&self, key: K, f: F) -> T where diff --git a/src/single.rs b/src/single.rs index 6cb5a08..dbdb235 100644 --- a/src/single.rs +++ b/src/single.rs @@ -11,13 +11,13 @@ use tokio::time::{interval, Interval}; /// /// #[tokio::main] /// async fn main() { -/// let period = std::time::Duration::from_millis(10); -/// let rate_limiter = RateLimiter::new(period); -/// -/// // Takes 90ms to complete, the first iteration is instant, the next 9 iterations take 100ms -/// for _ in 0..10 { -/// rate_limiter.throttle(|| async { /* work */ }).await; -/// } +/// let period = std::time::Duration::from_millis(10); +/// let rate_limiter = RateLimiter::new(period); +/// +/// // Takes 90ms to complete, the first iteration is instant, the next 9 iterations take 10ms +/// for _ in 0..10 { +/// rate_limiter.throttle(|| async { /* work */ }).await; +/// } /// } pub struct RateLimiter { /// The mutex that will be locked when the rate limiter is waiting for the interval to tick. @@ -65,7 +65,7 @@ impl RateLimiter { /// async fn do_work() { /* some computation */ } /// /// async fn do_throttle(limiter: Arc) { - /// limiter.throttle(|| do_work()).await + /// limiter.throttle(|| do_work()).await /// } pub async fn throttle(&self, f: F) -> T where