#token-bucket #rate-limiting #stream #data-stream #rate #synchronous #byte-stream

stream_limiter

Synchronously speed-limiting streams based on token bucket algorithm

8 stable releases

3.2.0 Jul 3, 2023
3.1.1 Jul 3, 2023
3.0.1 Jun 19, 2023
2.0.0 May 22, 2023
1.0.0 Feb 24, 2023

#10 in #token-bucket

Download history 23/week @ 2024-07-22 103/week @ 2024-07-29 7/week @ 2024-08-05 9/week @ 2024-08-12 27/week @ 2024-08-19 5/week @ 2024-08-26 16/week @ 2024-09-02 26/week @ 2024-09-09 37/week @ 2024-09-16 97/week @ 2024-09-23 170/week @ 2024-09-30 109/week @ 2024-10-07 77/week @ 2024-10-14 64/week @ 2024-10-21 65/week @ 2024-10-28 44/week @ 2024-11-04

256 downloads per month

MIT/Apache

44KB
1K SLoC

stream_limiter

Synchronously speed-limiting streams.

This crate provides a Limiter struct that can be used to limit the rate at which a stream can be read or written.

This crate is based on the token bucket algorithm. When we want to read data and we are rate limited the packet aren't drop but we sleep.

Example:

    use stream_limiter::Limiter;
    use std::time::Duration;
    use std::io::prelude::*;
    use std::fs::File;

    let mut file = File::open("tests/resources/test.txt").unwrap();
    let mut limiter = Limiter::new(file, 1, Duration::from_secs(1));
    let mut buf = [0u8; 10];
    let now = std::time::Instant::now();
    limiter.read(&mut buf).unwrap();
    assert_eq!(now.elapsed().as_secs(), 9);

lib.rs:

This crate provides a Limiter struct that can be used to limit the rate at which a stream can be read or written. This crate is based on the token bucket algorithm. When we want to read data and we are rate limited the packet aren't drop but we sleep. Example:

use stream_limiter::{Limiter, LimiterOptions};
use std::time::Duration;
use std::io::prelude::*;
use std::fs::File;

let mut file = File::open("test_resources/test.txt").unwrap();
let mut limiter = Limiter::new(file, Some(LimiterOptions::new(1, Duration::from_secs(1), 1)), None);
let mut buf = [0u8; 10];
let now = std::time::Instant::now();
limiter.read(&mut buf).unwrap();
assert_eq!(now.elapsed().as_secs(), 10);

No runtime deps