Skip to main content

timeout

Function timeout 

Source
pub fn timeout<C, Fut>(
    duration: Duration,
    capture: C,
    fut: Fut,
) -> TimeoutFuture<C, Fut> 
Expand description

Drive the future fut to completion, while limiting its run time to duration. If fut fails to finish within furation, returns span traces for all active await points within fut. Use capture to specify which kind of trace should be captured.

§Examples

tracing_subscriber::registry()
    .with(tracing_error::ErrorLayer::default())
    .init();

let result = timeout(Duration::from_secs(1), CaptureSpanTrace, long_computation()).await;
assert_eq!(filter_span_trace(result.err().unwrap().to_string()), "timeout elapsed at:
trace 0:
   0: example::step_2
             at example.rs:123
   1: example::long_computation
             at example.rs:123
");

#[instrument]
async fn long_computation() {
    step_1().await;
    step_2().await;
}
#[instrument]
async fn step_1() {
    sleep(Duration::from_millis(10)).await;
}
#[instrument]
async fn step_2() {
    sleep(Duration::from_secs(2)).await;
}