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

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

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?;

Implementations

Creates a new speed limiter.

Use infinity to make the speed unlimited.

Makes a Builder for further configurating this limiter.

Use infinity to make the speed unlimited.

Returns the clock associated with this limiter.

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.

Returns the current speed limit.

This method returns infinity if the speed is unlimited.

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.

Resets the total number of bytes consumed to 0.

Consumes several bytes from the speed limiter, returns the duration needed to sleep to maintain the speed limit.

Reverts the consumption of the given bytes size.

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.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.