#async #futures #stream #throttle

stream_throttle

Provides a Stream combinator, to limit the rate at which items are produced

4 releases (2 breaking)

✓ Uses Rust 2018 edition

0.3.1 Feb 9, 2020
0.2.1 Nov 11, 2018
0.1.1 May 31, 2018
0.1.0 May 27, 2018

#66 in Asynchronous

Download history 162/week @ 2019-11-08 47/week @ 2019-11-15 172/week @ 2019-11-22 34/week @ 2019-11-29 56/week @ 2019-12-06 32/week @ 2019-12-13 26/week @ 2019-12-20 41/week @ 2019-12-27 42/week @ 2020-01-03 81/week @ 2020-01-10 68/week @ 2020-01-17 63/week @ 2020-01-24 41/week @ 2020-01-31 77/week @ 2020-02-07 104/week @ 2020-02-14

254 downloads per month

MIT license

12KB
175 lines

stream_throttle

Provides a Rust Stream combinator, to limit the rate at which items are produced.

Crates.io API Documentation

Key Features

  • Throttling is implemented via poll(), and not via any sort of buffering.
  • The throttling behaviour can be applied to both Stream's and Future's.
  • Multiple streams/futures can be throttled together as a group.
  • Feature flags to use various timer implementations.

Feature Flags

  • timer-tokio: Uses the tokio::time::delay_for() timer.
  • timer-futures-timer: Uses the futures_timer::Delay timer.

If you don't use the default timer (tokio), make sure to set default-features = false in your Cargo.toml, when you add stream_throttle as a dependency.

Example throttling of Stream

// allow no more than 5 items every 1 second
let rate = ThrottleRate::new(5, Duration::new(1, 0));
let pool = ThrottlePool::new(rate);

let work = stream::repeat(())
  .throttle(pool)
  .then(|_| futures::future::ready("do something else"))
  .for_each(|_| futures::future::ready(()));
  
work.await;

Example throttling of Future

let rate = ThrottleRate::new(5, Duration::new(1, 0));
let pool = ThrottlePool::new(rate);

let work = pool.queue()
  .then(|_| futures::future::ready("do something else"));
  
work.await;

Dependencies

~0.9–1.3MB
~25K SLoC