2021-04-16 10:54:28 +02:00
|
|
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
|
|
|
// and other request-scoped values across API boundaries and between processes.
|
2021-09-27 16:52:20 +02:00
|
|
|
// Based on: https://github.com/golang/go/tree/master/src/context
|
2021-04-16 10:54:28 +02:00
|
|
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
2021-04-12 18:32:51 +02:00
|
|
|
module context
|
|
|
|
|
|
|
|
import rand
|
|
|
|
import sync
|
|
|
|
import time
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
pub type CancelFn = fn ()
|
|
|
|
|
2021-04-12 18:32:51 +02:00
|
|
|
pub interface Canceler {
|
|
|
|
id string
|
2021-10-11 14:41:31 +02:00
|
|
|
mut:
|
2021-04-16 10:54:28 +02:00
|
|
|
cancel(remove_from_parent bool, err IError)
|
2021-04-12 18:32:51 +02:00
|
|
|
done() chan int
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
[deprecated]
|
2021-10-11 14:41:31 +02:00
|
|
|
pub fn cancel(mut ctx Context) {
|
2021-04-12 18:32:51 +02:00
|
|
|
match mut ctx {
|
|
|
|
CancelContext {
|
|
|
|
ctx.cancel(true, canceled)
|
|
|
|
}
|
|
|
|
TimerContext {
|
|
|
|
ctx.cancel(true, canceled)
|
|
|
|
}
|
2021-04-13 06:04:13 +02:00
|
|
|
else {}
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// A CancelContext can be canceled. When canceled, it also cancels any children
|
|
|
|
// that implement Canceler.
|
|
|
|
pub struct CancelContext {
|
|
|
|
id string
|
|
|
|
mut:
|
|
|
|
context Context
|
|
|
|
mutex &sync.Mutex
|
|
|
|
done chan int
|
|
|
|
children map[string]Canceler
|
2021-04-16 10:54:28 +02:00
|
|
|
err IError
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// with_cancel returns a copy of parent with a new done channel. The returned
|
|
|
|
// context's done channel is closed when the returned cancel function is called
|
|
|
|
// or when the parent context's done channel is closed, whichever happens first.
|
|
|
|
//
|
|
|
|
// Canceling this context releases resources associated with it, so code should
|
|
|
|
// call cancel as soon as the operations running in this Context complete.
|
2021-10-11 14:41:31 +02:00
|
|
|
pub fn with_cancel(mut parent Context) (Context, CancelFn) {
|
2021-04-12 18:32:51 +02:00
|
|
|
mut c := new_cancel_context(parent)
|
2021-10-11 14:41:31 +02:00
|
|
|
propagate_cancel(mut parent, mut c)
|
2021-12-02 10:15:07 +01:00
|
|
|
cancel_fn := fn [mut c] () {
|
2021-09-27 16:52:20 +02:00
|
|
|
c.cancel(true, canceled)
|
|
|
|
}
|
2021-12-02 10:15:07 +01:00
|
|
|
return Context(c), CancelFn(cancel_fn)
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// new_cancel_context returns an initialized CancelContext.
|
|
|
|
fn new_cancel_context(parent Context) &CancelContext {
|
|
|
|
return &CancelContext{
|
|
|
|
id: rand.uuid_v4()
|
|
|
|
context: parent
|
|
|
|
mutex: sync.new_mutex()
|
2021-04-16 10:54:28 +02:00
|
|
|
done: chan int{cap: 2}
|
2021-07-30 02:18:20 +02:00
|
|
|
err: none
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
pub fn (ctx &CancelContext) deadline() ?time.Time {
|
2021-04-12 18:32:51 +02:00
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut ctx CancelContext) done() chan int {
|
|
|
|
ctx.mutex.@lock()
|
|
|
|
done := ctx.done
|
|
|
|
ctx.mutex.unlock()
|
|
|
|
return done
|
|
|
|
}
|
|
|
|
|
2021-04-16 10:54:28 +02:00
|
|
|
pub fn (mut ctx CancelContext) err() IError {
|
2021-04-12 18:32:51 +02:00
|
|
|
ctx.mutex.@lock()
|
|
|
|
err := ctx.err
|
|
|
|
ctx.mutex.unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
pub fn (ctx &CancelContext) value(key Key) ?Any {
|
2021-04-12 18:32:51 +02:00
|
|
|
if key == cancel_context_key {
|
2021-09-27 16:52:20 +02:00
|
|
|
return ctx
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
return ctx.context.value(key)
|
|
|
|
}
|
|
|
|
|
2021-09-27 16:52:20 +02:00
|
|
|
pub fn (ctx &CancelContext) str() string {
|
2021-04-12 18:32:51 +02:00
|
|
|
return context_name(ctx.context) + '.with_cancel'
|
|
|
|
}
|
|
|
|
|
2021-04-16 10:54:28 +02:00
|
|
|
fn (mut ctx CancelContext) cancel(remove_from_parent bool, err IError) {
|
2021-04-25 15:04:07 +02:00
|
|
|
if err is none {
|
2021-04-12 18:32:51 +02:00
|
|
|
panic('context: internal error: missing cancel error')
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.mutex.@lock()
|
2021-08-29 10:55:18 +02:00
|
|
|
if ctx.err !is none {
|
2021-04-12 18:32:51 +02:00
|
|
|
ctx.mutex.unlock()
|
|
|
|
// already canceled
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.err = err
|
|
|
|
|
|
|
|
if !ctx.done.closed {
|
2021-04-13 06:04:13 +02:00
|
|
|
ctx.done <- 0
|
2021-04-12 18:32:51 +02:00
|
|
|
ctx.done.close()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, child in ctx.children {
|
|
|
|
// NOTE: acquiring the child's lock while holding parent's lock.
|
2021-10-11 14:41:31 +02:00
|
|
|
mut c := child
|
|
|
|
c.cancel(false, err)
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx.children = map[string]Canceler{}
|
|
|
|
ctx.mutex.unlock()
|
|
|
|
|
|
|
|
if remove_from_parent {
|
2021-10-11 14:41:31 +02:00
|
|
|
mut cc := &ctx.context
|
|
|
|
remove_child(mut cc, ctx)
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 14:41:31 +02:00
|
|
|
fn propagate_cancel(mut parent Context, mut child Canceler) {
|
2021-04-12 18:32:51 +02:00
|
|
|
done := parent.done()
|
|
|
|
select {
|
|
|
|
_ := <-done {
|
|
|
|
// parent is already canceled
|
|
|
|
child.cancel(false, parent.err())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 14:41:31 +02:00
|
|
|
mut p := parent_cancel_context(mut parent) or {
|
|
|
|
go fn (mut parent Context, mut child Canceler) {
|
2021-04-12 18:32:51 +02:00
|
|
|
pdone := parent.done()
|
|
|
|
select {
|
|
|
|
_ := <-pdone {
|
|
|
|
child.cancel(false, parent.err())
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 14:41:31 +02:00
|
|
|
}(mut parent, mut child)
|
2021-04-12 18:32:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-25 15:04:07 +02:00
|
|
|
if p.err is none {
|
2021-09-27 16:52:20 +02:00
|
|
|
p.children[child.id] = child
|
2021-04-25 15:04:07 +02:00
|
|
|
} else {
|
2021-04-12 18:32:51 +02:00
|
|
|
// parent has already been canceled
|
|
|
|
child.cancel(false, p.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parent_cancel_context returns the underlying CancelContext for parent.
|
|
|
|
// It does this by looking up parent.value(&cancel_context_key) to find
|
|
|
|
// the innermost enclosing CancelContext and then checking whether
|
|
|
|
// parent.done() matches that CancelContext. (If not, the CancelContext
|
|
|
|
// has been wrapped in a custom implementation providing a
|
|
|
|
// different done channel, in which case we should not bypass it.)
|
2021-10-11 14:41:31 +02:00
|
|
|
fn parent_cancel_context(mut parent Context) ?&CancelContext {
|
2021-04-12 18:32:51 +02:00
|
|
|
done := parent.done()
|
|
|
|
if done.closed {
|
|
|
|
return none
|
|
|
|
}
|
2022-05-13 05:56:21 +02:00
|
|
|
mut p := parent.value(cancel_context_key)?
|
2021-09-27 16:52:20 +02:00
|
|
|
match mut p {
|
|
|
|
CancelContext {
|
2021-04-12 18:32:51 +02:00
|
|
|
pdone := p.done()
|
|
|
|
if done == pdone {
|
2021-09-27 16:52:20 +02:00
|
|
|
return p
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-09-27 16:52:20 +02:00
|
|
|
else {}
|
2021-04-12 18:32:51 +02:00
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove_child removes a context from its parent.
|
2021-10-11 14:41:31 +02:00
|
|
|
fn remove_child(mut parent Context, child Canceler) {
|
|
|
|
mut p := parent_cancel_context(mut parent) or { return }
|
2021-04-12 18:32:51 +02:00
|
|
|
p.children.delete(child.id)
|
|
|
|
}
|