Skip to main content

Module timeout

Module timeout 

Source
Available on crate feature time only.
Expand description

A request timeout that works natively and on WebAssembly: TimeoutLayer.

tower’s own TimeoutLayer sleeps with tokio, which has no timers on WebAssembly (wasm32-unknown-unknown), and fetch has no timeout of its own. This one waits with the timer the rest of the crate uses: tokio’s natively (so tests can pause time), the JavaScript host’s setTimeout on WebAssembly.

Where it sits decides what it bounds. Client::layer wraps the current stack, so a timeout added to Client::default() bounds the whole call, retries and their waits included. Built from the bare service, a timeout added before the retries bounds each attempt:

use std::time::Duration;
use timvw_rust_runtime::retry::{RetryLayer, RetryPolicy};
use timvw_rust_runtime::timeout::TimeoutLayer;
use timvw_rust_runtime::{Client, ReqwestService};

// The whole call, retries included.
let client = Client::default().layer(TimeoutLayer::new(Duration::from_secs(120)));

// Each attempt, retried when it times out; and the whole call.
let client = Client::from_service(ReqwestService::default())
    .layer(TimeoutLayer::new(Duration::from_secs(30)))
    .layer(RetryLayer::new(RetryPolicy::default().retry_timeouts(true)))
    .layer(TimeoutLayer::new(Duration::from_secs(120)));

The timeout runs until the response’s status and headers arrive: reading the body is not part of it. A request that times out is dropped, which cancels it (reqwest aborts the fetch on WebAssembly), and fails with an Error whose is_timeout is true.

Structs§

ResponseFuture
The future of a Timeout service.
Timeout
The service of a TimeoutLayer.
TimeoutLayer
Fails a request whose response does not arrive within a time limit; see the module.