Code Monkey home page Code Monkey logo

stream-throttle's Introduction

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;

stream-throttle's People

Contributors

davideagen avatar marcelbuesing avatar mikecaines avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

stream-throttle's Issues

Make crate independent of tokio runtime

I'm currently having a problem with functionality because I try to execute futures in async-std runtime.

I would suggest to use futures-timer crate instead. It is runtime independent and has 0 dependencies.

@mikecaines What do you think?

Cut a crates.io release with `queue_with_hold`

Hello, just wondering if you have plans to cut a release to crates.io including the queue_with_hold method? I can pin to the git sha for now but would prefer a proper version. Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.