fluvio_future/
future.rs

1use std::time;
2
3use anyhow::Result;
4
5use crate::timer;
6
7pub use futures_lite::future::*;
8
9/// If the future completes before the duration has elapsed, then the completed value is returned.
10/// Otherwise, an error is returned and the future is canceled.
11pub async fn timeout<F, T>(duration: time::Duration, future: F) -> Result<T>
12where
13    F: Future<Output = T>,
14    T: Send + 'static,
15{
16    race(async move { Ok(future.await) }, async move {
17        let _ = timer::sleep(duration).await;
18        Err(anyhow::anyhow!("Future timed out after {:?}", duration))
19    })
20    .await
21}