[][src]Struct async_speed_limit::limiter::Limiter

pub struct Limiter<C: Clock = StandardClock> { /* fields omitted */ }

A type to control the maximum speed limit of multiple streams.

When a Limiter is cloned, the instances would share the same queue. Multiple tasks can cooperatively respect a global speed limit via clones. Cloning a Limiter is cheap (equals to cloning two Arcs).

The speed limit is imposed by awaiting consume(). The method returns a future which sleeps until rate falls below the limit.

Examples

Upload some small files atomically in parallel, while maintaining a global speed limit of 1 MiB/s.

use async_speed_limit::Limiter;
use futures_util::future::try_join_all;

let limiter = <Limiter>::new(1_048_576.0);
let processes = files
    .iter()
    .map(|file| {
        let limiter = limiter.clone();
        async move {
            limiter.consume(file.len()).await;
            upload(file).await?;
            Ok(())
        }
    });
try_join_all(processes).await?;

Methods

impl<C: Clock> Limiter<C>[src]

pub fn new(speed_limit: f64) -> Self[src]

Creates a new speed limiter.

Use infinity to make the speed unlimited.

pub fn builder(speed_limit: f64) -> Builder<C>[src]

Makes a Builder for further configurating this limiter.

Use infinity to make the speed unlimited.

pub fn clock(&self) -> &C[src]

Returns the clock associated with this limiter.

pub fn set_speed_limit(&self, speed_limit: f64)[src]

Dynamically changes the speed limit. The new limit applies to all clones of this instance.

Use infinity to make the speed unlimited.

This change will not affect any tasks scheduled before this call.

pub fn speed_limit(&self) -> f64[src]

Returns the current speed limit.

This method returns infinity if the speed is unlimited.

pub fn total_bytes_consumed(&self) -> usize[src]

Obtains the total number of bytes consumed by this limiter so far.

If more than usize::MAX bytes have been consumed, the count will wrap around.

pub fn reset_statistics(&self)[src]

Resets the total number of bytes consumed to 0.

Important traits for Consume<C, R>
pub fn consume(&self, byte_size: usize) -> Consume<C, ()>[src]

Consumes several bytes from the speed limiter.

The consumption happens at the beginning, before the speed limit is applied. The returned future is fulfilled after the speed limit is satified.

pub fn limit<R, T>(self, resource: R) -> Resource<R, T, C>[src]

Wraps a streaming resource with speed limiting. See documentation of Resource for details.

If you want to reuse the limiter after calling this function, clone() the limiter first.

impl<C: BlockingClock> Limiter<C>[src]

pub fn blocking_consume(&self, byte_size: usize)[src]

Consumes several bytes, and sleeps the current thread to maintain the speed limit.

The consumption happens at the beginning, before the speed limit is applied. This method blocks the current thread (e.g. using std::thread::sleep() given a StandardClock), and must not be used in async context.

Prefer using this method instead of futures_executor::block_on(limiter.consume(size)).

Trait Implementations

impl<C: Clone + Clock> Clone for Limiter<C> where
    C::Instant: Clone
[src]

impl<C: Debug + Clock> Debug for Limiter<C> where
    C::Instant: Debug
[src]

Auto Trait Implementations

impl<C> RefUnwindSafe for Limiter<C> where
    C: RefUnwindSafe

impl<C> Send for Limiter<C> where
    C: Send,
    <C as Clock>::Instant: Send

impl<C> Sync for Limiter<C> where
    C: Sync,
    <C as Clock>::Instant: Send

impl<C> Unpin for Limiter<C> where
    C: Unpin

impl<C> UnwindSafe for Limiter<C> where
    C: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.