[−][src]Struct leaky_bucket::LeakyBucket
The leaky bucket.
Implementations
impl LeakyBucket
[src]
pub fn builder() -> Builder<'static>
[src]
Construct a new leaky bucket through a builder.
pub fn tokens(&self) -> usize
[src]
Query how many tokens are available.
This is just a best-effort estimate, calling this to ensure that there are enough tokens available to avoid blocking does not guarantee that the acquire operation won't block.
Tokens is always reported as less than or equal to max
.
Example
use leaky_bucket::LeakyBucket; use std::{error::Error, time::Duration}; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { let rate_limiter = LeakyBucket::builder() .max(5) .tokens(0) .build()?; assert_eq!(0, rate_limiter.tokens()); println!("Waiting for permit..."); // should take about 5 seconds to acquire. rate_limiter.acquire_one().await?; println!("I made it!"); assert!(rate_limiter.tokens() >= 0 && rate_limiter.tokens() <= rate_limiter.max()); Ok(()) }
pub fn max(&self) -> usize
[src]
Get the max number of tokens this rate limiter is configured for.
pub async fn acquire_one<'_>(&'_ self) -> Result<(), Error>
[src]
Acquire a single token.
This is identical to acquire
with an argument of 1
.
Example
use leaky_bucket::LeakyBucket; use std::{error::Error, time::Duration}; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { let rate_limiter = LeakyBucket::builder() .max(5) .tokens(0) .build()?; assert_eq!(0, rate_limiter.tokens()); println!("Waiting for permit..."); // should take about 5 seconds to acquire. rate_limiter.acquire_one().await?; println!("I made it!"); assert_eq!(0, rate_limiter.tokens()); Ok(()) }
pub async fn acquire<'_>(&'_ self, amount: usize) -> Result<(), Error>
[src]
Acquire the given amount
of tokens.
Note that you are allowed to acquire more tokens than the current
max
, but the acquire will have to suspend the task until enough
tokens has built up to satisfy the request.
Example
use leaky_bucket::LeakyBucket; use std::{error::Error, time::Duration}; #[tokio::main] async fn main() -> Result<(), Box<dyn Error>> { let rate_limiter = LeakyBucket::builder() .max(5) .tokens(5) .build()?; assert_eq!(5, rate_limiter.tokens()); println!("Waiting for permit..."); // should take about 5 seconds to acquire. rate_limiter.acquire(10).await?; println!("I made it!"); assert_eq!(0, rate_limiter.tokens()); Ok(()) }
Errors
The returned future will fail with an Error::TokenOverflow
if the
tracked number of tokens attempts to overflow the internal token
counter, which is an usize
.
For this reason you should prefer to use smaller values in the amount acquired.
Trait Implementations
impl Clone for LeakyBucket
[src]
fn clone(&self) -> LeakyBucket
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl Debug for LeakyBucket
[src]
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,