6 releases

0.4.0 Jan 11, 2024
0.3.1 Jul 23, 2023
0.2.2 Jul 18, 2023

#3 in #limiter

Download history 122/week @ 2024-06-13 663/week @ 2024-06-20 157/week @ 2024-06-27 196/week @ 2024-07-04 114/week @ 2024-07-11 104/week @ 2024-07-18 363/week @ 2024-07-25 379/week @ 2024-08-01 337/week @ 2024-08-08 201/week @ 2024-08-15 202/week @ 2024-08-22 205/week @ 2024-08-29 370/week @ 2024-09-05 238/week @ 2024-09-12 503/week @ 2024-09-19 214/week @ 2024-09-26

1,370 downloads per month
Used in 3 crates (2 directly)

MIT license

190KB
231 lines

stream-rate-limiter

Stream combinator

.rate_limiter(opt: RateLimitOptions)

Provides way to limit stream element rate with constant intervals. It adds some level of customization in case of stream delays inaccessible in "standard" solution (tokio-timer)

Before you use this library, please consider using tokio-timer crate (you can look at tokio_interval example)

If you need to limit stream rate with constant intervals, with additional customisation (increasing permanent stream delay in case of stream delays, which prevents flooding stream after huge hickups.

For example you want to produce a stream of elements with constant rate of 1 element per second. You can use this library to set rate limit to 1 element and for example 1 second of accepted delay. Thus after your stream is stuck for like couple of seconds your event won't be flooding but they go back to rate at one per second fast. You can also react and close the stream if delay is too big.

Case study

x axis is element number, y axis is time, element number 40 is simulated to stuck for 2 seconds)

When you want to delay stream after hiccup

stream::iter(0..101)
    .rate_limit(
        RateLimitOptions::empty()
        .with_min_interval_sec(0.02)
        .with_interval_sec(0.1)
        .with_allowed_slippage_sec(0.5)
        .on_stream_delayed(|sdi| StreamBehavior::Delay(sdi.current_delay)),
    )

alt text

When you want to allow stream to catchup after hiccup (continue option):

stream::iter(0..101)
    .rate_limit(
        RateLimitOptions::empty()
            .with_min_interval_sec(0.02)
            .with_interval_sec(0.1)
            .with_allowed_slippage_sec(0.5)
            .on_stream_delayed(|_sdi| StreamBehavior::Continue),
    )

alt text

When you want to stop stream after hiccup (stop option): Note that last element may appear depending on if the delay was before or after rate_limit. Hiccups may occur on any side of the stream. This extension (.rate_limit) does not differentiate where hiccups occure.

stream::iter(0..101)
.rate_limit(
    RateLimitOptions::empty()
        .with_min_interval_sec(0.02)
        .with_interval_sec(0.1)
        .with_allowed_slippage_sec(0.5)
        .on_stream_delayed(|_sdi| StreamBehavior::Stop),
    )

alt text

Dependencies

~2–7.5MB
~47K SLoC