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(NonZero::new(64 * 1024).unwrap(), 512);
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();
});Re-exportsยง
use futures::AsyncWrite;๐use pin_project::pin_project;๐use std::io::Error;๐use std::num::NonZero;๐use std::pin::Pin;๐use std::task::Context;๐use std::task::Poll;๐use std::task::ready;๐use super::DirectionState;๐use crate::bw_pool::BandwidthAcquirer;๐
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.