Skip to main content

Module sink

Module sink 

Source
Expand description

A rate limited sink.

A Sink wrapper that rate limits the items it forwards by acquiring bandwidth from a crate::bw_pool::BandwidthPool before each item is sent.

§Example

use futures::{SinkExt as _, channel::mpsc};
use tor_async_utils::global_rate_limit::GlobalRateLimitedSink;
use tor_async_utils::bw_pool::BandwidthPool;

futures::executor::block_on(async {
    let (pool, _refiller) = BandwidthPool::new(64 * 1024);
    let (tx, _rx) = mpsc::channel::<Vec<u8>>(8);
    let mut sink = GlobalRateLimitedSink::new(tx, pool.new_acquirer(), 512);

    // The pool starts full so this is served from the fast path.
    sink.send(vec![0; 512]).await.unwrap();
});

Structs§

GlobalRateLimitedSink
A Sink wrapper that acquires bandwidth before forwarding each item.