Skip to main content

Module timeout

Module timeout 

Source
Expand description

A ceiling on how long a handler may run.

Without one, a handler waiting on a database that has stopped answering holds its connection, its task and its client for as long as the client is willing to wait — which for a browser is minutes, and for a retrying service is forever. With one, the wait ends at a known point and the client gets an answer it can act on.

r.group("/api", |api| {
    api.middleware(Timeout::after(Duration::from_secs(10)));
    …
});

The response is a 503. Not 504, which is for a gateway whose upstream was slow, and not 408, which tells the client it was slow to send. A 503 says the service could not answer in time, which is the truth, and carries no Retry-After, because nothing here knows when it would be safe to.

What times out is dropped. A handler half-way through a database write is abandoned at whatever .await it was parked on; the write either landed or it did not, and the connection goes back to the pool in the state the driver leaves it. Keep the limit generous enough that only a genuinely stuck request hits it.

Structs§

Timeout