Struct leaky_bucket_lite::LeakyBucket [−][src]
pub struct LeakyBucket { /* fields omitted */ }
Expand description
The leaky bucket.
Implementations
Get the current number of tokens available.
Errors
Returns an Error
when communicating with the actor fails.
Acquire a single token.
This is identical to acquire
with an argument of 1.0
.
Example
use leaky_bucket_lite::LeakyBucket;
use std::{error::Error, time::Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let rate_limiter = LeakyBucket::builder()
.max(5.0)
.tokens(0.0)
.refill_interval(Duration::from_secs(5))
.refill_amount(1.0)
.build();
println!("Waiting for permit...");
// should take about 5 seconds to acquire.
rate_limiter.acquire_one().await?;
println!("I made it!");
Ok(())
}
Errors
Returns an Error
when communicating with the actor fails.
Acquire the given amount
of tokens.
Example
use leaky_bucket_lite::LeakyBucket;
use std::{error::Error, time::Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let rate_limiter = LeakyBucket::builder()
.max(5.0)
.tokens(0.0)
.refill_interval(Duration::from_secs(5))
.refill_amount(1.0)
.build();
println!("Waiting for permit...");
// should take about 25 seconds to acquire.
rate_limiter.acquire(5.0).await?;
println!("I made it!");
Ok(())
}
Errors
Returns an Error
when communicating with the actor fails.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for LeakyBucket
impl Send for LeakyBucket
impl Sync for LeakyBucket
impl Unpin for LeakyBucket
impl !UnwindSafe for LeakyBucket
Blanket Implementations
Mutably borrows from an owned value. Read more