Skip to main content

Module writer

Module writer 

Source
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§

GlobalRateLimitedWriter
An AsyncWrite wrapper that acquires bandwidth before writing bytes.

Functions§

poll_write_limited 🔒
Helper: Rate-limited AsyncWrite::poll_write of inner using the given read direction state. This is used by multiple object hence why in a write helper.