Struct async_speed_limit::limiter::Limiter [−][src]
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
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]
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.
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>(self, resource: R) -> Resource<R, 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]
C::Instant: Clone,
impl<C: Debug + Clock> Debug for Limiter<C> where
C::Instant: Debug,
[src]
C::Instant: Debug,
Auto Trait Implementations
impl<C> RefUnwindSafe for Limiter<C> where
C: RefUnwindSafe,
C: RefUnwindSafe,
impl<C> Send for Limiter<C> where
C: Send,
<C as Clock>::Instant: Send,
C: Send,
<C as Clock>::Instant: Send,
impl<C> Sync for Limiter<C> where
C: Sync,
<C as Clock>::Instant: Send,
C: Sync,
<C as Clock>::Instant: Send,
impl<C> Unpin for Limiter<C> where
C: Unpin,
C: Unpin,
impl<C> UnwindSafe for Limiter<C> where
C: UnwindSafe,
C: UnwindSafe,
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,
pub 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.
pub fn to_owned(&self) -> T
[src]
pub 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.
pub 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>,