Skip to main content

Module race

Module race 

Source
Expand description

Racing and timeouts on fibers: Effect::race, race_all, Effect::race_first, Effect::timeout and Effect::timeout_fail.

Every racer runs as a child fiber of the racing fiber (so the racer’s interruption reaches them, and its exit awaits them). Semantics follow Effect.ts:

  • race / race_all (Effect.race, Effect.raceAll): the first success wins. The other racers are interrupted and awaited, so their finalizers have run before the result is returned. A failure does not end the race; when every racer fails, the race fails with all the causes combined by Cause::Both in input order (race(a, b) fails with Both(cause_a, cause_b), like Effect.ts Cause.parallel). Effect.ts raceAll keeps only the last failure; here race_all keeps every one, so race_all([a, b]) and a.race(b) agree.
  • race_first (Effect.raceFirst): the first racer to complete wins, success or failure, and the other is interrupted and awaited.
  • timeout (Effect.timeout) is race_first against a sleep on the fiber’s executor clock (virtual time on TestExecutor). The Rust shape returns Option<A>: Some(a) when the effect finished first, None when the deadline passed. The Effect.ts 3.x TimeoutException error channel is the separate Effect::timeout_fail, which fails with an error built by the caller instead. A failure of the effect before the deadline is re-raised as is. Because the interrupted racer is awaited, an uninterruptible (masked) effect delays the timeout until it completes; the result is still None.

race_all of no effects dies (as Effect.ts raceAll of an empty iterable does).

Functions§

race_all
Races every effect (Effect.ts Effect.raceAll): the first success wins, and the others are interrupted and awaited (their finalizers have run) before the result is returned. When every effect fails, the race fails with all the causes combined by Cause::Both in input order. An empty input dies.