https://github.com/mikecaines/stream-throttle
Rust Stream combinator, to limit the rate at which items are produced
https://github.com/mikecaines/stream-throttle
async futures rust stream throttle
Last synced: 3 months ago
JSON representation
Rust Stream combinator, to limit the rate at which items are produced
- Host: GitHub
- URL: https://github.com/mikecaines/stream-throttle
- Owner: mikecaines
- License: mit
- Created: 2018-05-22T22:37:45.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2023-09-04T18:52:18.000Z (almost 3 years ago)
- Last Synced: 2025-11-17T05:23:35.129Z (7 months ago)
- Topics: async, futures, rust, stream, throttle
- Language: Rust
- Size: 35.2 KB
- Stars: 28
- Watchers: 1
- Forks: 6
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# stream_throttle
Provides a
[Rust](https://www.rust-lang.org)
[`Stream`](https://docs.rs/futures/0.3/futures/stream/trait.Stream.html)
combinator, to limit the rate at which items are produced.
[](https://crates.io/crates/stream_throttle)
[](https://docs.rs/stream_throttle/)
## Key Features
- Throttling is implemented via
[`poll()`](https://docs.rs/futures/0.3/futures/future/trait.Future.html#tymethod.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`
```rust
// 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`
```rust
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;
```