pub struct RateLimiter { /* private fields */ }
Expand description

A token-bucket rate limiter.

Implementations§

source§

impl RateLimiter

source

pub fn builder() -> Builder

Construct a new Builder for a RateLimiter.

Examples
use leaky_bucket::RateLimiter;
use std::time::Duration;

let limiter = RateLimiter::builder()
    .initial(100)
    .refill(100)
    .max(1000)
    .interval(Duration::from_millis(250))
    .fair(false)
    .build();
source

pub fn refill(&self) -> usize

Get the refill amount of this rate limiter as set through Builder::refill.

Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .refill(1024)
    .build();

assert_eq!(limiter.refill(), 1024);
source

pub fn interval(&self) -> Duration

Get the refill interval of this rate limiter as set through Builder::interval.

Examples
use std::time::Duration;

use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .interval(Duration::from_millis(1000))
    .build();

assert_eq!(limiter.interval(), Duration::from_millis(1000));
source

pub fn max(&self) -> usize

Get the max value of this rate limiter as set through Builder::max.

Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .max(1024)
    .build();

assert_eq!(limiter.max(), 1024);
source

pub fn is_fair(&self) -> bool

Test if the current rate limiter is fair as specified through Builder::fair.

Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .fair(true)
    .build();

assert_eq!(limiter.is_fair(), true);
source

pub fn balance(&self) -> usize

Get the current token balance.

This indicates how many tokens can be requested without blocking.

Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .initial(100)
    .build();

assert_eq!(limiter.balance(), 100);
limiter.acquire(10).await;
assert_eq!(limiter.balance(), 90);
source

pub fn acquire_one(&self) -> Acquire<'_>

Acquire a single permit.

Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .initial(10)
    .build();

limiter.acquire_one().await;
source

pub fn acquire(&self, permits: usize) -> Acquire<'_>

Acquire the given number of permits, suspending the current task until they are available.

If zero permits are specified, this function never suspends the current task.

Examples
use leaky_bucket::RateLimiter;

let limiter = RateLimiter::builder()
    .initial(10)
    .build();

limiter.acquire(10).await;
source

pub fn acquire_owned(self: Arc<Self>, permits: usize) -> AcquireOwned

Acquire a permit using an owned future.

If zero permits are specified, this function never suspends the current task.

This required the RateLimiter to be wrapped inside of an [std::sync::Arc] but will in contrast permit the acquire operation to be owned by another struct making it more suitable for embedding.

Examples
use leaky_bucket::RateLimiter;
use std::sync::Arc;

let limiter = Arc::new(RateLimiter::builder().initial(10).build());

limiter.acquire_owned(10).await;

Example when embedded into another future. This wouldn’t be possible with RateLimiter::acquire since it would otherwise hold a reference to the corresponding RateLimiter instance.

use leaky_bucket::{AcquireOwned, RateLimiter};
use pin_project::pin_project;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::time::Duration;

#[pin_project]
struct MyFuture {
    limiter: Arc<RateLimiter>,
    #[pin]
    acquire: Option<AcquireOwned>,
}

impl Future for MyFuture {
    type Output = ();

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let mut this = self.project();

        loop {
            if let Some(acquire) = this.acquire.as_mut().as_pin_mut() {
                futures::ready!(acquire.poll(cx));
                return Poll::Ready(());
            }

            this.acquire.set(Some(this.limiter.clone().acquire_owned(100)));
        }
    }
}

let limiter = Arc::new(RateLimiter::builder().initial(100).build());

let future = MyFuture { limiter, acquire: None };
future.await;

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more