#async-stream #rate-limiting #stream #future #async #throttle #throttling

stream_throttle

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

6 releases (breaking)

0.5.1 Sep 5, 2023
0.4.0 Apr 7, 2021
0.3.1 Feb 9, 2020
0.2.1 Nov 11, 2018
0.1.1 May 31, 2018

#169 in Asynchronous

Download history 12579/week @ 2024-07-22 10222/week @ 2024-07-29 9993/week @ 2024-08-05 9503/week @ 2024-08-12 11425/week @ 2024-08-19 10134/week @ 2024-08-26 11278/week @ 2024-09-02 10435/week @ 2024-09-09 7537/week @ 2024-09-16 9982/week @ 2024-09-23 8512/week @ 2024-09-30 10951/week @ 2024-10-07 13160/week @ 2024-10-14 12771/week @ 2024-10-21 12536/week @ 2024-10-28 12479/week @ 2024-11-04

51,090 downloads per month
Used in 3 crates

MIT license

14KB
205 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.7–6.5MB
~36K SLoC