| Crates.io | async-io-throttling |
| lib.rs | async-io-throttling |
| version | 0.1.0 |
| created_at | 2025-10-11 10:38:42.316758+00 |
| updated_at | 2025-10-11 10:38:42.316758+00 |
| description | Rate-limiting Async IO |
| homepage | https://github.com/heilhead/async-io-throttling |
| repository | https://github.com/heilhead/async-io-throttling.git |
| max_upload_size | |
| id | 1878064 |
| size | 60,023 |
Rate-limiting tokio async IO using governor.
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.