Skip to content

couple of small fixes for documentation #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
```
```

18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down
20 changes: 10 additions & 10 deletions src/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() { }
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<K: Eq + Hash + Clone> MultiRateLimiter<K> {
/// async fn do_work() { /* some computation */ }
///
/// async fn throttle_by_key(the_key: u32, limiter: Arc<MultiRateLimiter<u32>>) {
/// limiter.throttle(the_key, || do_work()).await
/// limiter.throttle(the_key, || do_work()).await
/// }
pub async fn throttle<Fut, F, T>(&self, key: K, f: F) -> T
where
Expand Down
16 changes: 8 additions & 8 deletions src/single.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -65,7 +65,7 @@ impl RateLimiter {
/// async fn do_work() { /* some computation */ }
///
/// async fn do_throttle(limiter: Arc<RateLimiter>) {
/// limiter.throttle(|| do_work()).await
/// limiter.throttle(|| do_work()).await
/// }
pub async fn throttle<Fut, F, T>(&self, f: F) -> T
where
Expand Down