async-io-throttling

Crates.ioasync-io-throttling
lib.rsasync-io-throttling
version0.1.0
created_at2025-10-11 10:38:42.316758+00
updated_at2025-10-11 10:38:42.316758+00
descriptionRate-limiting Async IO
homepagehttps://github.com/heilhead/async-io-throttling
repositoryhttps://github.com/heilhead/async-io-throttling.git
max_upload_size
id1878064
size60,023
Ivan Reshetnikov (heilhead)

documentation

README

async-io-throttling

Rate-limiting tokio async IO using governor.

Usage

Adding dependency:

[dependencies]
async-io-throttling = "0.1"

Throttling AsyncRead:

use {
    async_io_throttling::{BandwidthLimiter, prelude::*},
    std::time::Instant,
    tokio::io::AsyncReadExt,
};

#[tokio::main]
async fn main() {
    // Read 5MB of zeroes at 1MB/s rate.
    let mut junk_reader = tokio::io::repeat(0)
        .take(5 * 1024 * 1024)
        .with_read_limiter(BandwidthLimiter::new(1024 * 1024).unwrap());

    let file = tempfile::tempfile().unwrap();
    let mut file = tokio::fs::File::from_std(file);

    let time = Instant::now();
    tokio::io::copy(&mut junk_reader, &mut file).await.unwrap();
    println!("elapsed: {:?}", time.elapsed());
}

Throttling AsyncWrite:

use {
    async_io_throttling::{BandwidthLimiter, prelude::*},
    std::time::Instant,
    tokio::io::AsyncReadExt,
};

#[tokio::main]
async fn main() {
    // Read 5MB of zeroes and write them at 1MB/s rate.
    let mut junk_reader = tokio::io::repeat(0).take(5 * 1024 * 1024);

    let file = tempfile::tempfile().unwrap();
    let mut file = tokio::fs::File::from_std(file)
        .with_write_limiter(BandwidthLimiter::new(1024 * 1024).unwrap());

    let time = Instant::now();
    tokio::io::copy(&mut junk_reader, &mut file).await.unwrap();
    println!("elapsed: {:?}", time.elapsed());
}

Both of the above examples should complete in ~4s, due to the initial burst allowed by the rate limiter.

License

Apache 2.0

Commit count: 0

cargo fmt