Function bmrng::channel_with_timeout [−][src]
pub fn channel_with_timeout<Req, Res>(
buffer: usize,
timeout_duration: Duration
) -> (RequestSender<Req, Res>, RequestReceiver<Req, Res>)
Expand description
Creates a bounded mpsc request-response channel for communicating between asynchronous tasks with backpressure and a request timeout
Panics
Panics if the buffer capacity is 0, just like the Tokio MPSC channel
Examples
use tokio::time::{Duration, sleep};
#[tokio::main]
async fn main() {
let (tx, mut rx) = bmrng::channel_with_timeout::<i32, i32>(100, Duration::from_millis(100));
tokio::spawn(async move {
match rx.recv().await {
Ok((input, mut responder)) => {
sleep(Duration::from_millis(200)).await;
let res = responder.respond(input * input);
assert_eq!(res.is_ok(), true);
}
Err(err) => {
println!("all request senders dropped");
}
}
});
let response = tx.send_receive(8).await;
assert_eq!(response, Err(bmrng::error::RequestError::<i32>::RecvTimeoutError));
}