broker_tokio::time

Function throttle

Source
pub fn throttle<T>(duration: Duration, stream: T) -> Throttle<T>
where T: Stream,
Available on crate features stream and time only.
Expand description

Slow down a stream by enforcing a delay between items. They will be produced not more often than the specified interval.

ยงExample

Create a throttled stream.

use std::time::Duration;
use tokio::stream::StreamExt;
use tokio::time::throttle;

let mut item_stream = throttle(Duration::from_secs(2), futures::stream::repeat("one"));

loop {
    // The string will be produced at most every 2 seconds
    println!("{:?}", item_stream.next().await);
}