Parallel.any
func Parallel.any[T, E](fs: Array[func(Context): Result[T, E]], ctx: Context | null = null): Result[T, Array[E]] | nullParallel.any calls every function of fs at once with the scope context and returns the first Ok one returns, cancelling the scope for the rest. An Err cancels nothing; when every function returns Err, it returns Err of the errors in input order, and Err([]) when fs is empty. It returns null when the scope ends before an Ok or the last Err.
Details
- The call returns only after every callback has ended, the losers included. A loser stops early only if it passes the scope context to its waits.
- When
ctxis done already andfsis not empty, no callback runs and the call returnsnull. - A panic in any callback, a loser after the winner included, is raised in the caller.
Basic examples
The first Ok wins. An Err from another callback does not stop the call:
func primary(ctx: Context): Result[string, Error] { Result.fail("primary is down") }
func mirror(ctx: Context): Result[string, Error] { Result.ok("reply from the mirror") }
println(Parallel.any([primary, mirror]))Ok{ value: "reply from the mirror" }When every callback fails, the result is Err of all the errors:
func primary(ctx: Context): Result[string, Error] { Result.fail("primary is down") }
func mirror(ctx: Context): Result[string, Error] { Result.fail("mirror is down") }
println(Parallel.any([primary, mirror]))Err{ error: [Error{ message: "primary is down" }, Error{ message: "mirror is down" }] }Scope
Lambdas and the scope context
A lambda callback gets the scope context as x. Once the fast lookup wins, the call cancels the scope, and the slow lookup's sleep returns early:
func lookup(server: string, delay: Time.Duration, ctx: Context): Result[string, Error] {
Time.sleep(delay, ctx: ctx)
if ctx.isDone() {
println("${server} stopped: ${ctx.reason()}")
return Result.fail("${server}: ${ctx.reason()}")
}
Result.ok("answer from ${server}")
}
let answer = Parallel.any([
{ lookup("slow", 1s, x) },
{ lookup("fast", 10ms, x) },
])
println(answer)slow stopped: cancelled
Ok{ value: "answer from fast" }A fast failure waits for a slow success
An Err cancels nothing, so a quick failure leaves the others running:
func failFast(ctx: Context): Result[int, string] { Result.err("refused") }
func answerLater(ctx: Context): Result[int, string] {
Time.sleep(20ms, ctx: ctx)
Result.ok(42)
}
println(Parallel.any([failFast, answerLater]))Ok{ value: 42 }Errors in input order
The error list follows fs. Here the first callback fails last, and its error still comes first:
func slowFailure(ctx: Context): Result[int, string] {
Time.sleep(20ms, ctx: ctx)
Result.err("slow")
}
func quickFailure(ctx: Context): Result[int, string] { Result.err("quick") }
println(Parallel.any([slowFailure, quickFailure]))Err{ error: ["slow", "quick"] }Empty input
An empty list has no callback to succeed, so the result is Err([]):
let none: Array[func(Context): Result[int, string]] = []
println(Parallel.any(none))Err{ error: [] }Named arguments
ctx
The scope is a child of ctx. When ctx ends before any callback succeeds, the call returns null:
func lookup(server: string, ctx: Context): Result[string, Error] {
Time.sleep(1s, ctx: ctx)
if ctx.isDone() { return Result.fail("${server}: ${ctx.reason()}") }
Result.ok("answer from ${server}")
}
let ctx = Context.new(20ms)
let answer = Parallel.any([{ lookup("east", x) }, { lookup("west", x) }], ctx: ctx)
println(answer)
println(ctx.reason())null
timeoutA context that is done already runs no callback:
var calls = 0
func count(ctx: Context): Result[int, string] {
calls += 1
Result.ok(calls)
}
let done = Context.new()
done.cancel()
println(Parallel.any([count], ctx: done))
println(calls)null
0Applications
Read the first file that exists
Look for a file in several places at once. The Err of every miss lists all the places tried:
func readFirst(paths: Array[string]): Result[string, Error] {
let readers = paths.map(func(path: string): func(Context): Result[string, Error] {
{ File.read(path) }
})
let found = Parallel.any(readers) ?? return Result.fail("cancelled")
found | Result.mapErr({ Error{ message: x.map({ x.message }) | String.join("; ") } })
}
println(readFirst(["missing.json", "local.json"]))
println(readFirst(["a.json", "b.json"]))Ok{ value: "{\"port\": 8080}\n" }
Err{ error: Error{ message: "open a.json: no such file or directory; open b.json: no such file or directory" } }Ask replicas with a deadline
Ask every replica, take the first answer, and give up after a deadline. The function owns its deadline context and cancels it on return:
func ask(replica: string, delay: Time.Duration, ctx: Context): Result[string, Error] {
Time.sleep(delay, ctx: ctx)
if ctx.isDone() { return Result.fail("${replica}: ${ctx.reason()}") }
Result.ok("quote from ${replica}")
}
func quote(delays: Array[Time.Duration]): Result[string, Error] {
let deadline = Context.new(100ms)
defer { deadline.cancel() }
let reply = Parallel.any([
{ ask("a", delays[0], x) },
{ ask("b", delays[1], x) },
], ctx: deadline) ?? return Result.fail("no replica answered in time")
reply | Result.mapErr({ Error{ message: "all ${x.length()} replicas failed" } })
}
println(quote([1s, 10ms]))
println(quote([1s, 1s]))Ok{ value: "quote from b" }
Err{ error: Error{ message: "no replica answered in time" } }Properties and relations
Parallel.race returns the first value of any kind, Parallel.any the first Ok. With a fast Err and a slower Ok, they disagree:
func refuse(ctx: Context): Result[int, string] { Result.err("refused") }
func answer(ctx: Context): Result[int, string] {
Time.sleep(20ms, ctx: ctx)
Result.ok(42)
}
let first = Parallel.race([refuse, answer])
let firstOk = Parallel.any([refuse, answer])
assert(first is Err && first.error == "refused")
assert(firstOk is Ok && firstOk.value == 42)When every callback fails, the result holds one error per callback:
func refuse(ctx: Context): Result[int, string] { Result.err("refused") }
let fs = [refuse, refuse, refuse]
let result = Parallel.any(fs)
assert(result is Err && result.error.length() == fs.length())Possible issues
A callback that ignores its context holds the call
The winner returns at once, but the call waits for the loser, which sleeps without the scope context. Pass x, or the ctx parameter, to every wait:
var stubbornFinished = false
func stubborn(ctx: Context): Result[string, Error] {
Time.sleep(50ms)
stubbornFinished = true
Result.ok("stubborn")
}
func quick(ctx: Context): Result[string, Error] { Result.ok("quick") }
println(Parallel.any([stubborn, quick]))
println(stubbornFinished)Ok{ value: "quick" }
trueA callback that waits on the caller's ctx misses the winner
The call cancels only its scope context, so a callback that sleeps on the caller's deadline does not see the fast winner and holds the call for the full second. Pass the scope context to its waits instead: the ctx parameter of a named function, or x in a lambda:
var slowFinished = false
let deadline = Context.new(5s)
defer { deadline.cancel() }
func slow(ctx: Context): Result[string, Error] {
Time.sleep(1s, ctx: deadline)
slowFinished = true
Result.fail("slow replica")
}
func fast(ctx: Context): Result[string, Error] { Result.ok("fast replica") }
println(Parallel.any([slow, fast], ctx: deadline))
println(slowFinished)Ok{ value: "fast replica" }
trueA loser's panic outweighs the winner
A panic in any callback reaches the caller, even after another callback has won. Here Panic.catch stops it in the caller:
func quick(ctx: Context): Result[string, Error] { Result.ok("quick") }
func broken(ctx: Context): Result[string, Error] {
Time.sleep(10ms, ctx: ctx)
panic("broken replica")
}
let caught = Panic.catch({ Parallel.any([quick, broken]) })
if caught is Err {
println("caught: ${caught.error.message}")
}caught: broken replicaA lambda that only builds a Result has no type
A lambda whose body is only Result.fail or Result.ok leaves a type parameter of the Result unknown. Give the callbacks a declared return type, as the named functions on this page do:
let answer = Parallel.any([
{ Result.fail("primary is down") },
// error: cannot infer the type parameter T of Result.fail; annotate the result
{ Result.ok("mirror") },
])See also
Parallel.race · Parallel.run · Parallel.map · Parallel.each · Context.new · Context.cancel · Result.mapErr · Concurrency guide
