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
Arc
s).
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.
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.
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.
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
Auto Trait Implementations
impl<C> RefUnwindSafe for Limiter<C> where
C: RefUnwindSafe,
impl<C> UnwindSafe for Limiter<C> where
C: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more