2021-04-12 18:32:51 +02: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 06:04:13 +02:00
|
|
|
and return a derived Context (the child). Calling the cancel function
|
2021-04-12 18:32:51 +02:00
|
|
|
cancels the child and its children, removes the parent's reference to the child,
|
2021-04-13 06:04:13 +02:00
|
|
|
and stops any associated timers.
|
2021-04-12 18:32:51 +02: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
|
2022-03-26 12:25:45 +01:00
|
|
|
import time
|
2021-04-12 18:32:51 +02:00
|
|
|
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.
|
2022-03-26 12:25:45 +01:00
|
|
|
fn main() {
|
2021-04-12 18:32:51 +02:00
|
|
|
// 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-10-11 14:41:31 +02:00
|
|
|
gen := fn (mut ctx context.Context) chan int {
|
2021-04-12 18:32:51 +02:00
|
|
|
dst := chan int{}
|
2021-10-11 14:41:31 +02:00
|
|
|
go fn (mut ctx context.Context, dst chan int) {
|
2021-04-13 06:04:13 +02:00
|
|
|
mut v := 0
|
2021-04-12 18:32:51 +02:00
|
|
|
ch := ctx.done()
|
2021-04-13 06:04:13 +02:00
|
|
|
for {
|
2021-04-12 18:32:51 +02:00
|
|
|
select {
|
|
|
|
_ := <-ch {
|
|
|
|
// returning not to leak the routine
|
2022-03-26 12:25:45 +01:00
|
|
|
eprintln('> go thread returns because ctx was canceled/done')
|
2021-04-13 06:04:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
dst <- v {
|
|
|
|
v++
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 14:41:31 +02:00
|
|
|
}(mut ctx, dst)
|
2021-04-12 18:32:51 +02:00
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:41:31 +02:00
|
|
|
mut background := context.background()
|
2021-10-14 12:32:42 +02:00
|
|
|
mut ctx, cancel := context.with_cancel(mut &background)
|
2021-04-12 18:32:51 +02:00
|
|
|
defer {
|
2021-09-27 16:52:20 +02:00
|
|
|
cancel()
|
2022-03-26 12:25:45 +01:00
|
|
|
time.sleep(2 * time.millisecond) // give a small time window, in which the go thread routine has a chance to return
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-11 14:41:31 +02:00
|
|
|
mut mut_ctx := ctx
|
|
|
|
mut ctx2 := &mut_ctx
|
|
|
|
ch := gen(mut ctx2)
|
2021-04-12 18:32:51 +02:00
|
|
|
for i in 0 .. 5 {
|
|
|
|
v := <-ch
|
2022-03-26 12:25:45 +01:00
|
|
|
println('> received value: $v')
|
2021-04-12 18:32:51 +02:00
|
|
|
assert i == v
|
|
|
|
}
|
2022-03-26 12:25:45 +01:00
|
|
|
println('> main is finished here')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context With Deadline
|
|
|
|
|
|
|
|
```v
|
|
|
|
import time
|
2022-03-26 12:25:45 +01:00
|
|
|
import context
|
2021-04-12 18:32:51 +02:00
|
|
|
|
2022-03-26 12:25:45 +01:00
|
|
|
const short_duration = 2 * time.millisecond // a reasonable duration to block in an example
|
2021-04-12 18:32:51 +02:00
|
|
|
|
|
|
|
// 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.
|
2022-03-26 12:25:45 +01:00
|
|
|
fn main() {
|
2021-04-12 18:32:51 +02:00
|
|
|
dur := time.now().add(short_duration)
|
2021-10-11 14:41:31 +02:00
|
|
|
mut background := context.background()
|
2021-10-14 12:32:42 +02:00
|
|
|
mut ctx, cancel := context.with_deadline(mut &background, dur)
|
2021-04-12 18:32:51 +02: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-09-27 16:52:20 +02:00
|
|
|
cancel()
|
2022-03-26 12:25:45 +01:00
|
|
|
time.sleep(short_duration) // give a small time window, in which the go thread routine has a chance to return
|
|
|
|
eprintln('> defer block finishes')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx_ch := ctx.done()
|
|
|
|
select {
|
2022-03-26 12:25:45 +01:00
|
|
|
_ := <-ctx_ch {
|
|
|
|
println('>> read from ctx_ch succeeded')
|
|
|
|
}
|
2021-07-23 22:24:27 +02:00
|
|
|
1 * time.second {
|
2021-04-25 15:04:07 +02:00
|
|
|
panic('This should not happen')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-26 12:25:45 +01:00
|
|
|
eprintln('> main finishes')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context With Timeout
|
|
|
|
|
|
|
|
```v
|
|
|
|
import time
|
2022-03-26 12:25:45 +01:00
|
|
|
import context
|
2021-04-12 18:32:51 +02:00
|
|
|
|
2022-03-26 12:25:45 +01:00
|
|
|
const short_duration = 2 * time.millisecond // a reasonable duration to block in an example
|
2021-04-12 18:32:51 +02:00
|
|
|
|
|
|
|
// This example passes a context with a timeout to tell a blocking function that
|
|
|
|
// it should abandon its work after the timeout elapses.
|
2022-03-26 12:25:45 +01:00
|
|
|
fn main() {
|
2021-04-12 18:32:51 +02:00
|
|
|
// Pass a context with a timeout to tell a blocking function that it
|
|
|
|
// should abandon its work after the timeout elapses.
|
2021-10-11 14:41:31 +02:00
|
|
|
mut background := context.background()
|
2021-10-14 12:32:42 +02:00
|
|
|
mut ctx, cancel := context.with_timeout(mut &background, short_duration)
|
2021-04-12 18:32:51 +02:00
|
|
|
defer {
|
2021-09-27 16:52:20 +02:00
|
|
|
cancel()
|
2022-03-26 12:25:45 +01:00
|
|
|
eprintln('> defer finishes')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx_ch := ctx.done()
|
|
|
|
select {
|
2022-03-26 12:25:45 +01:00
|
|
|
_ := <-ctx_ch {
|
|
|
|
eprintln('> reading from ctx_ch succeeded')
|
|
|
|
}
|
2021-07-23 22:24:27 +02:00
|
|
|
1 * time.second {
|
2021-04-25 15:04:07 +02:00
|
|
|
panic('This should not happen')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-26 12:25:45 +01:00
|
|
|
eprintln('> main finishes')
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
### Context With Value
|
|
|
|
|
|
|
|
```v
|
|
|
|
import context
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
const not_found_value = &Value{
|
|
|
|
val: 'key not found'
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Value {
|
|
|
|
val string
|
|
|
|
}
|
2021-04-12 18:32:51 +02:00
|
|
|
|
|
|
|
// This example demonstrates how a value can be passed to the context
|
|
|
|
// and also how to retrieve it if it exists.
|
2022-03-26 12:25:45 +01:00
|
|
|
fn main() {
|
2021-09-27 16:52:20 +02:00
|
|
|
f := fn (ctx context.Context, key context.Key) &Value {
|
2021-04-12 18:32:51 +02:00
|
|
|
if value := ctx.value(key) {
|
2021-09-27 16:52:20 +02:00
|
|
|
match value {
|
|
|
|
Value {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
else {}
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 16:52:20 +02:00
|
|
|
return not_found_value
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
key := 'language'
|
|
|
|
value := &Value{
|
|
|
|
val: 'VAL'
|
|
|
|
}
|
|
|
|
ctx := context.with_value(context.background(), key, value)
|
2021-04-12 18:32:51 +02:00
|
|
|
|
2022-03-26 12:25:45 +01:00
|
|
|
assert value == dump(f(ctx, key))
|
|
|
|
assert not_found_value == dump(f(ctx, 'color'))
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
```
|