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(NonZero::new(64 * 1024).unwrap(), 512);
    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();
});

Re-exportsยง

use futures::AsyncRead; ๐Ÿ”’
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ยง

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.