Skip to main content

Module reader

Module reader 

Source
Expand description

A rate limited async reader.

An AsyncRead wrapper that rate limits the bytes it yields by acquiring bandwidth from a crate::bw_pool::BandwidthPool before each read. One byte is one token.

§Example

use std::num::NonZero;
use futures::AsyncReadExt as _;
use tor_async_utils::global_rate_limit::GlobalRateLimitedReader;
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 reader =
        GlobalRateLimitedReader::new(&b"hello world"[..], pool.new_acquirer(), max_chunk);

    // The pool starts full so this is served from the fast path.
    let mut buf = [0; 10];
    reader.read_exact(&mut buf).await.unwrap();
});

Structs§

GlobalRateLimitedReader
An AsyncRead wrapper that acquires bandwidth before yielding bytes.

Functions§

poll_read_limited 🔒
Helper: Rate-limited AsyncRead::poll_read of inner using the given read direction state. This is used by multiple object hence why in a read helper.