Crate stream_limiter

source ·
Expand description

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);

Structs

  • A Limiter is a wrapper around a stream that implement Read and Write that limits the rate at which it can be read or written. The rate is given in byte/s.