Expand description
A rate limited async writer.
An AsyncWrite wrapper that rate limits the bytes it forwards by acquiring bandwidth
from a crate::bw_pool::BandwidthPool before each write. One byte is one token.
§Example
use std::num::NonZero;
use futures::AsyncWriteExt as _;
use tor_async_utils::global_rate_limit::GlobalRateLimitedWriter;
use tor_async_utils::bw_pool::BandwidthPool;
futures::executor::block_on(async {
let (pool, _refiller) = BandwidthPool::new(64 * 1024);
let max_chunk = NonZero::new(1024).unwrap();
let mut writer = GlobalRateLimitedWriter::new(Vec::new(), pool.new_acquirer(), max_chunk);
// The pool starts full so this is served from the fast path.
writer.write_all(&[0; 512]).await.unwrap();
});Structs§
- Global
Rate Limited Writer - An
AsyncWritewrapper that acquires bandwidth before writing bytes.
Functions§
- poll_
write_ 🔒limited - Helper: Rate-limited
AsyncWrite::poll_writeofinnerusing the given read directionstate. This is used by multiple object hence why in a write helper.