[][src]Function async_listen::backpressure::new

pub fn new(initial_limit: usize) -> (Sender, Receiver)

Create a new pair of backpressure structures

These structures are called Sender and Receiver similar to channels. The Receiver should be used to throttle, either by applying it to a stream or using it directly. The Sender is a way to create throtting tokens (the stream is paused when there are tokens >= limit), and to change the limit.

See ListenExt for example usage

Direct Use Example

use async_listen::ListenExt;
use async_listen::backpressure;

let listener = TcpListener::bind("127.0.0.1:0").await?;
let (tx, mut rx) = backpressure::new(10);
let mut incoming = listener.incoming()
    .handle_errors(Duration::from_millis(100));

loop {
    rx.has_capacity().await;
    let conn = match incoming.next().await {
        Some(conn) => conn,
        None => break,
    };
    let token = tx.token();  // should be created before spawn
    task::spawn(async {
        connection_loop(conn).await;
        drop(token);  // should be dropped after
    });
}