Context.new
func Context.new(timeout: Time.Duration | null = null, parent: Context | null = null): ContextContext.new returns a new context that is a child of parent, or of the run when parent is null. With a timeout it is done once the timeout passes; a child of a done parent is done at once with the parent's reason. It panics if timeout has years, months, or weeks, or is not positive.
Details
- A context without a timeout is done only when a call of Context.cancel cancels it, when its parent is done, or when the run is cancelled.
- A timeout of days counts each day as 24 hours.
- A context is done once and stays done. A timeout that has passed ends every later wait on that context at once.
- Cancelling a child leaves its parent running.
Basic examples
A new context is running, with no reason yet:
let ctx = Context.new()
ctx.isDone() // false
ctx.reason() // nullA context with a timeout becomes done when the timeout passes, with the reason timeout:
let ctx = Context.new(50ms)
ctx.wait()
println(ctx.reason())timeoutPass the context to a blocking call to bound its wait:
let replies: Channel[string] = Channel.new()
let ctx = Context.new(50ms)
println(replies.receive(ctx: ctx) ?? "no reply")no replyScope
Any duration of hours and smaller units works as a timeout, and so do days:
Context.new(1s500ms).isDone() // false
Context.new(Time.duration(days: 1)).isDone() // falseA context with a timeout has a deadline, the instant the timeout passes. Without a timeout, it has a deadline only when a parent has one:
println(Context.new(1h).deadline() != null)
println(Context.new().deadline())true
nullEach call makes a new context. Contexts compare by identity:
let ctx = Context.new()
ctx == ctx // true
Context.new() == Context.new() // falseNamed arguments
timeout
timeout: is the first parameter, so the name is optional. With the name, the call reads like an Http.get call that takes one:
let ctx = Context.new(timeout: 20ms)
ctx.wait()
println(ctx.reason())timeoutparent
With parent:, the new context is done when its parent is done, with the parent's reason:
let parent = Context.new()
let child = Context.new(parent: parent)
parent.cancel()
println(child.reason())cancelledA child of a parent with a timeout ends when the parent times out:
let parent = Context.new(20ms)
let child = Context.new(parent: parent)
child.wait()
println(child.reason())timeoutA child of a done parent is done at once, even with a timeout of its own:
let parent = Context.new()
parent.cancel()
let child = Context.new(1h, parent: parent)
child.isDone() // true
child.reason() // "cancelled"Combine timeout: and parent: to give one step a shorter budget inside a larger one:
let request = Context.new(5s)
let lookup = Context.new(100ms, parent: request)
lookup.wait()
println("lookup: ${lookup.reason()}")
println("request done: ${request.isDone()}")lookup: timeout
request done: falseApplications
Stop a group of goroutines
Give each goroutine a child of one context, and cancel that context to stop them all:
let run = Context.new()
let results: Channel[string] = Channel.new()
for name in ["fetch", "index"] {
let work = Context.new(parent: run)
go {
work.wait()
results.send("${name}: ${work.reason()}")
}
}
Time.sleep(20ms)
run.cancel()
let lines = [results.receive() ?? "", results.receive() ?? ""]
for line in lines.sort() {
println(line)
}fetch: cancelled
index: cancelledRetry with a timeout per attempt
Each attempt gets its own short timeout, and all of them stay under an overall budget:
func attempt(n: int, ctx: Context): Result[string, Error] {
let replies: Channel[string] = Channel.new(1)
if n == 3 { replies.send("pong") }
let reply = replies.receive(ctx: ctx) ?? return Result.fail("attempt ${n}: ${ctx.reason()}")
Result.ok(reply)
}
let overall = Context.new(1s)
for n in Iter.range(1, 5) {
let result = attempt(n, Context.new(20ms, parent: overall))
println(result)
if result.isOk() { break }
}Err{ error: Error{ message: "attempt 1: timeout" } }
Err{ error: Error{ message: "attempt 2: timeout" } }
Ok{ value: "pong" }Stop at Ctrl-C or after a budget
A child of Os.interrupt ends on the first interrupt or when its own timeout passes, whichever comes first. Its reason tells which:
let ctx = Context.new(50ms, parent: Os.interrupt())
while !ctx.isDone() {
// one step of work
Time.sleep(10ms, ctx: ctx)
}
println("stopped: ${ctx.reason()}")stopped: timeoutProperties and relations
A child's deadline is the earlier of its own and its parent's:
let outer = Context.new(100ms)
let inner = Context.new(1h, parent: outer)
assert(inner.deadline() == outer.deadline())Cancelling a child leaves its parent running:
let parent = Context.new()
let child = Context.new(parent: parent)
child.cancel()
assert(child.isDone())
assert(!parent.isDone())Possible issues
The parent is not the first argument
The first parameter is the timeout, so a parent passed by position is a compile error:
let parent = Context.new()
let child = Context.new(parent) // error: cannot use Context where Time.Duration | null is expectedName it: Context.new(parent: parent).
A timeout that is not positive panics
A zero or negative timeout panics, rather than making a context that is done at once:
Context.new(0s) // panic: Context.new: the timeout is zero or negativeTo make a context that is done now, cancel it:
let ctx = Context.new()
ctx.cancel()
ctx.isDone() // trueYears, months and weeks panic
They have no fixed length, so the type checker accepts them but the call panics:
Context.new(Time.duration(weeks: 1))
// panic: Context.new: the timeout has years, months or weeks, which have no fixed lengthUse days or hours, which Context.new takes as fixed spans: Time.duration(days: 7).
A shared timeout runs out for every later wait
The timeout belongs to the context, not to each wait. Once it passes, every wait on the context gives up at once:
let ctx = Context.new(30ms)
let replies: Channel[string] = Channel.new()
for n in Iter.range(1, 3) {
let started = Time.monotonic()
replies.receive(ctx: ctx)
let waited = Time.monotonic() - started >= 25
println("wait ${n}: waited ${waited}")
}wait 1: waited true
wait 2: waited falseMake a new context for each wait that needs a budget of its own, as in the retry example above.
See also
Context.cancel · Context.wait · Context.isDone · Context.reason · Context.deadline · Os.interrupt · Time.sleep · Parallel.map
