2021-04-12 19:32:51 +03:00
|
|
|
# Context
|
|
|
|
|
|
|
|
This module defines the Context type, which carries deadlines, cancellation signals,
|
|
|
|
and other request-scoped values across API boundaries and between processes.
|
|
|
|
|
|
|
|
Incoming requests to a server should create a Context, and outgoing calls to servers
|
|
|
|
should accept a Context. The chain of function calls between them must propagate the
|
|
|
|
Context, optionally replacing it with a derived Context created using with_cancel,
|
|
|
|
with_deadline, with_timeout, or with_value. When a Context is canceled, all Contexts
|
|
|
|
derived from it are also canceled.
|
|
|
|
|
|
|
|
The with_cancel, with_deadline, and with_timeout functions take a Context (the parent)
|
2021-04-13 07:04:13 +03:00
|
|
|
and return a derived Context (the child). Calling the cancel function
|
2021-04-12 19:32:51 +03:00
|
|
|
cancels the child and its children, removes the parent's reference to the child,
|
2021-04-13 07:04:13 +03:00
|
|
|
and stops any associated timers.
|
2021-04-12 19:32:51 +03:00
|
|
|
|
|
|
|
Programs that use Contexts should follow these rules to keep interfaces consistent
|
|
|
|
across different modules.
|
|
|
|
|
|
|
|
Do not store Contexts inside a struct type; instead, pass a Context explicitly
|
|
|
|
to each function that needs it. The Context should be the first parameter,
|
|
|
|
typically named ctx, just to make it more consistent.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
In this section you can see some usage examples for this module
|
|
|
|
|
|
|
|
### Context With Cancellation
|
|
|
|
|
|
|
|
```v
|
|
|
|
import context
|
|
|
|
|
|
|
|
// This example demonstrates the use of a cancelable context to prevent a
|
|
|
|
// routine leak. By the end of the example function, the routine started
|
|
|
|
// by gen will return without leaking.
|
|
|
|
fn example_with_cancel() {
|
|
|
|
// gen generates integers in a separate routine and
|
|
|
|
// sends them to the returned channel.
|
|
|
|
// The callers of gen need to cancel the context once
|
|
|
|
// they are done consuming generated integers not to leak
|
|
|
|
// the internal routine started by gen.
|
2021-04-13 07:04:13 +03:00
|
|
|
gen := fn (ctx context.Context) chan int {
|
2021-04-12 19:32:51 +03:00
|
|
|
dst := chan int{}
|
2021-04-13 07:04:13 +03:00
|
|
|
go fn (ctx context.Context, dst chan int) {
|
|
|
|
mut v := 0
|
2021-04-12 19:32:51 +03:00
|
|
|
ch := ctx.done()
|
2021-04-13 07:04:13 +03:00
|
|
|
for {
|
2021-04-12 19:32:51 +03:00
|
|
|
select {
|
|
|
|
_ := <-ch {
|
|
|
|
// returning not to leak the routine
|
2021-04-13 07:04:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dst <- v {
|
|
|
|
v++
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-13 07:04:13 +03:00
|
|
|
}(ctx, dst)
|
2021-04-12 19:32:51 +03:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2021-04-13 07:04:13 +03:00
|
|
|
ctx := context.with_cancel(context.background())
|
2021-04-12 19:32:51 +03:00
|
|
|
defer {
|
2021-04-13 07:04:13 +03:00
|
|
|
context.cancel(ctx)
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
|
2021-04-13 07:04:13 +03:00
|
|
|
ch := gen(ctx)
|
2021-04-12 19:32:51 +03:00
|
|
|
for i in 0 .. 5 {
|
|
|
|
v := <-ch
|
|
|
|
assert i == v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context With Deadline
|
|
|
|
|
|
|
|
```v
|
|
|
|
import context
|
|
|
|
import time
|
|
|
|
|
|
|
|
const (
|
|
|
|
// a reasonable duration to block in an example
|
|
|
|
short_duration = 1 * time.millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
// This example passes a context with an arbitrary deadline to tell a blocking
|
|
|
|
// function that it should abandon its work as soon as it gets to it.
|
|
|
|
fn example_with_deadline() {
|
|
|
|
dur := time.now().add(short_duration)
|
2021-04-13 07:04:13 +03:00
|
|
|
ctx := context.with_deadline(context.background(), dur)
|
2021-04-12 19:32:51 +03:00
|
|
|
|
|
|
|
defer {
|
|
|
|
// Even though ctx will be expired, it is good practice to call its
|
|
|
|
// cancellation function in any case. Failure to do so may keep the
|
|
|
|
// context and its parent alive longer than necessary.
|
2021-04-13 07:04:13 +03:00
|
|
|
context.cancel(ctx)
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx_ch := ctx.done()
|
|
|
|
select {
|
2021-04-25 16:04:07 +03:00
|
|
|
_ := <-ctx_ch {}
|
|
|
|
> 1 * time.second {
|
|
|
|
panic('This should not happen')
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context With Timeout
|
|
|
|
|
|
|
|
```v
|
|
|
|
import context
|
|
|
|
import time
|
|
|
|
|
|
|
|
const (
|
|
|
|
// a reasonable duration to block in an example
|
|
|
|
short_duration = 1 * time.millisecond
|
|
|
|
)
|
|
|
|
|
|
|
|
// This example passes a context with a timeout to tell a blocking function that
|
|
|
|
// it should abandon its work after the timeout elapses.
|
|
|
|
fn example_with_timeout() {
|
|
|
|
// Pass a context with a timeout to tell a blocking function that it
|
|
|
|
// should abandon its work after the timeout elapses.
|
2021-04-13 07:04:13 +03:00
|
|
|
ctx := context.with_timeout(context.background(), short_duration)
|
2021-04-12 19:32:51 +03:00
|
|
|
defer {
|
2021-04-13 07:04:13 +03:00
|
|
|
context.cancel(ctx)
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx_ch := ctx.done()
|
|
|
|
select {
|
2021-04-25 16:04:07 +03:00
|
|
|
_ := <-ctx_ch {}
|
|
|
|
> 1 * time.second {
|
|
|
|
panic('This should not happen')
|
2021-04-12 19:32:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context With Value
|
|
|
|
|
|
|
|
```v
|
|
|
|
import context
|
|
|
|
|
|
|
|
type ValueContextKey = string
|
|
|
|
|
|
|
|
// This example demonstrates how a value can be passed to the context
|
|
|
|
// and also how to retrieve it if it exists.
|
|
|
|
fn example_with_value() {
|
2021-04-13 07:04:13 +03:00
|
|
|
f := fn (ctx context.Context, key ValueContextKey) string {
|
2021-04-12 19:32:51 +03:00
|
|
|
if value := ctx.value(key) {
|
|
|
|
if !isnil(value) {
|
|
|
|
return *(&string(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 'key not found'
|
|
|
|
}
|
|
|
|
|
|
|
|
key := ValueContextKey('language')
|
|
|
|
value := 'VAL'
|
|
|
|
ctx := context.with_value(context.background(), key, &value)
|
|
|
|
|
|
|
|
assert value == f(ctx, key)
|
|
|
|
assert 'key not found' == f(ctx, ValueContextKey('color'))
|
|
|
|
}
|
|
|
|
```
|