Context.deadline
func Context.deadline(ctx: Context): Time.Instant | nullContext.deadline returns the earliest deadline of ctx and its parents, the run's included, or null if none has one.
Details
- A child never reports a later deadline than its parent: with a longer timeout, it reports the parent's.
- The deadline stays after the context is done, so it does not tell whether the context is done; use Context.isDone for that.
Basic examples
A context with a timeout has a deadline, the moment the timeout passes:
let ctx = Context.new(timeout: 2s)
Context.deadline(ctx) is Time.Instant // trueA context without a timeout, and without a parent that has one, has none:
let ctx = Context.new()
println(Context.deadline(ctx))nullScope
The deadline lies the timeout after the call to Context.new:
let before = Time.now()
let ctx = Context.new(timeout: 1h)
let deadline = Context.deadline(ctx) ?? return
let timeout = Time.until(before, deadline, largestUnit: Time.Hour{})
println(Time.roundDuration(timeout, smallestUnit: Time.Minute{}))PT1HA child without a timeout reports its parent's deadline:
let request = Context.new(timeout: 5s)
let step = Context.new(parent: request)
step.deadline() == request.deadline() // trueA child with a shorter timeout has its own, earlier deadline:
let request = Context.new(timeout: 1h)
let step = Context.new(timeout: 1s, parent: request)
println(step.deadline() == request.deadline())
let stepAt = step.deadline() ?? return
let requestAt = request.deadline() ?? return
println(Time.compare(stepAt, requestAt))false
-1A child with a longer timeout keeps its parent's deadline, the earlier one:
let request = Context.new(timeout: 1s)
let step = Context.new(timeout: 1h, parent: request)
step.deadline() == request.deadline() // trueThe deadline reaches every generation:
let outer = Context.new(timeout: 30s)
let middle = Context.new(parent: outer)
let inner = Context.new(parent: middle)
inner.deadline() == outer.deadline() // trueA parent without a deadline gives its child none:
let parent = Context.new()
println(Context.new(parent: parent).deadline())nullA cancelled context keeps its deadline:
let ctx = Context.new(timeout: 1h)
ctx.cancel()
ctx.isDone() // true
ctx.deadline() != null // trueSo does one whose deadline has passed:
let ctx = Context.new(timeout: 20ms)
ctx.wait()
println(ctx.reason())
println(ctx.deadline() != null)timeout
trueApplications
Skip work that cannot finish in time
Compare the time left with what a step needs, and skip it before starting. A context without a deadline runs every step:
func runStep(name: string, needs: Time.Duration, ctx: Context): void {
let deadline = ctx.deadline()
if deadline != null && Time.compareDuration(Time.until(Time.now(), deadline), needs) < 0 {
println("skip ${name}: not enough time")
return
}
println("run ${name}")
}
let ctx = Context.new(timeout: 1s)
runStep("index", 100ms, ctx)
runStep("reindex", 1h, ctx)
runStep("reindex", 1h, Context.new())run index
skip reindex: not enough time
run reindexTell a server how long to try
Turn the time left into whole seconds, such as for a timeout header on a request, and send nothing when there is no deadline:
func timeoutHeader(ctx: Context): string | null {
let deadline = ctx.deadline() ?? return null
let left = Time.until(Time.now(), deadline)
"${int(Math.ceil(left.total(Time.Second{})))}"
}
println(timeoutHeader(Context.new(timeout: 2s)))
println(timeoutHeader(Context.new()))2
nullProperties and relations
A child's deadline is never after its parent's:
let parent = Context.new(timeout: 10s)
let parentAt = parent.deadline() ?? return
for timeout in [1s, 10s, 1m] {
let childAt = Context.new(timeout: timeout, parent: parent).deadline() ?? return
assert(Time.compare(childAt, parentAt) <= 0)
}The deadline of a context made with a timeout lies between the times read just before and just after the call plus the timeout:
let before = Time.now()
let ctx = Context.new(timeout: 5s)
let after = Time.now()
let deadline = ctx.deadline() ?? return
assert(Time.compare(deadline, before.add(5s)) >= 0)
assert(Time.compare(deadline, after.add(5s)) <= 0)Possible issues
The result can be null
Context.deadline returns null when no context in the chain has a timeout, so the result is not a Time.Instant until the null is handled:
let ctx = Context.new(timeout: 1s)
let at: Time.Instant = ctx.deadline()
// error: cannot use Time.Instant | null where Time.Instant is expectedHandle it with ?? or a guard:
let ctx = Context.new(timeout: 1s)
let at = ctx.deadline() ?? return
println(Time.compare(at, Time.now()))1Durations do not compare with <
The time left is a Time.Duration, which < does not order:
func isShort(ctx: Context): bool {
let deadline = ctx.deadline() ?? return false
let left = Time.until(Time.now(), deadline)
left < 100ms
// error: < applies to a single numeric kind, string, or a named primitive type over one, not to Time.Duration
}Use Time.compareDuration:
let ctx = Context.new(timeout: 1s)
let deadline = ctx.deadline() ?? return
let left = Time.until(Time.now(), deadline)
println(Time.compareDuration(left, 100ms) < 0)falseTime left after the deadline is negative
Once the deadline passes, the time left is negative, and a new context with it as the timeout panics:
let ctx = Context.new(timeout: 10ms)
ctx.wait()
let deadline = ctx.deadline() ?? return
let left = Time.until(Time.now(), deadline)
let next = Context.new(timeout: left) // panic: Context.new: the timeout is zero or negativeTo give work the rest of a deadline, pass the context as the parent instead; a child of a done parent is done at once:
let ctx = Context.new(timeout: 10ms)
ctx.wait()
let next = Context.new(parent: ctx)
println(next.reason())timeoutSee also
Context.new · Context.isDone · Context.reason · Context.wait · Time.now · Time.until · Time.compareDuration · Time.Instant · Context
