Timeouts
The problem
ask waits until the actor answers. An actor that is slow, stuck behind other work, or holding the message in its
stash keeps the asker waiting as long as that lasts. Most callers want a bound.
Ask with a timeout
askWithin :: Int -> Address a -> (Reply r -> Message a) -> Either Fault r
askWithin 200 door (\r -> Knock r) -- Left (Fault "Timeout" "no answer in time")
On an engine reference the same is A.askRefWithin ms ref make; a host’s client takes its timeout as an argument
(askRoute client route typeName payload ms).
How it works
An ask makes a one-shot reply channel, puts the message in the actor’s mailbox, and waits on the channel. With a timeout it waits on the channel or a clock, whichever comes first. The fiber that waits is parked; nothing else stops.
What happens on timeout
The asker gets Left (Fault "Timeout" ...) and moves on. The message is still in the actor’s mailbox (or stash),
and the actor still handles it; its late answer goes to a channel no one reads any more. A timeout says “no answer
in time”, not “nothing happened”: make messages that change state safe to repeat if you retry them.
Handling the result
Every answer is a value, so timeouts are handled with the other failures, by matching:
case askWithin 500 account (\r -> Balance r) of
Right n -> showInt n
Left (Fault "Timeout" detail) -> "the account is busy"
Left (Fault kind detail) -> kind
A stopped actor answers at once with ActorStopped; it never costs the timeout.
A circuit breaker
When a dependency keeps failing, asking it again only adds load. Actor.Breaker is a breaker as a value: it opens
after a number of failures in a row, refuses calls while open, and lets one call try once its reset time has passed.
import Actor.Breaker as Br
import System.Time as T
-- Two failures in a row open it for a second.
let b0 = Br.breaker 2 1000 in
let c1 = Br.call (T.nowMillis ()) b0 (\u -> askWithin 50 door (\r -> Knock r)) in
let c2 = Br.call (T.nowMillis ()) (Br.calledBreaker c1) (\u -> askWithin 50 door (\r -> Knock r)) in
let c3 = Br.call (T.nowMillis ()) (Br.calledBreaker c2) (\u -> askWithin 50 door (\r -> Knock r)) in
...
-- Br.calledResult c1, c2, c3: Timeout, Timeout, CircuitBreakerOpen
call now breaker work returns the breaker’s next value (calledBreaker) and the result (calledResult); a call
refused while open is Left (Fault "CircuitBreakerOpen" ...) and does not run work. Time is passed in, so a
breaker’s rules are tested without waiting. Keep the breaker in the state of the actor that makes the calls.
Retrying
A saga step can retry with backoff (retryWithBackoff); see Sagas & Monitoring. For a single ask,
retry by calling again on Left (Fault "Timeout" _), a bounded number of times.
Best practices
- Give every ask that crosses a network a timeout;
askRouterequires one. - Pick timeouts from what the caller can wait, not from how long the work usually takes.
- Treat a timeout as unknown, not as failed: the work may still happen.
- Put a breaker in front of a dependency that can fail for a long time.