-
Notifications
You must be signed in to change notification settings - Fork 1
/
cancel-context.go
122 lines (105 loc) · 3.66 KB
/
cancel-context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
package parl
import (
"context"
"errors"
"github.com/haraldrudell/parl/perrors"
"github.com/haraldrudell/parl/pruntime"
)
const (
cancelNotifierFrames = 1
)
// ErrNotCancelContext indicates that InvokeCancel was provided a context
// not a return value from NewCancelContext or NewCancelContextFunc.
//
// Tets for ErrNotCancelContext:
//
// if errors.Is(err, parl.ErrNotCancelContext) …
var ErrNotCancelContext = errors.New("context chain does not have CancelContext")
// cancelContextKey is a unique named type used for access-function context.Context.Value.
// Because the cancelContextKey type is unique, there is no conflict with other values.
// cancelContextKey is used to store a context cancellation function as a context value
// so that Cancel can be invoked from a single context value.
type cancelContextKey string
// cancelKey is the value used for storing the cancel function with context.WIthValue.
var cancelKey cancelContextKey
type cancelNotifier string
var notifierKey cancelNotifier
func AddNotifier(ctx context.Context, notifierFn func(slice pruntime.StackSlice)) (
ctx2 context.Context) {
if ctx == nil {
panic(perrors.NewPF("ctx cannot be nil"))
}
if notifierFn == nil {
panic(perrors.NewPF("notifierFn cannot be nil"))
}
fnsAny := ctx.Value(notifierKey)
fns, _ := fnsAny.([]func(slice pruntime.StackSlice))
fns = append([]func(slice pruntime.StackSlice){notifierFn}, fns...)
return context.WithValue(ctx, notifierKey, fns)
}
// NewCancelContext creates a context that can be provided to InvokeCancel.
// the return value encapsulates a cancel function.
//
// ctx := NewCancelContext(context.Background())
// …
// InvokeCancel(ctx)
func NewCancelContext(ctx context.Context) (cancelCtx context.Context) {
return NewCancelContextFunc(context.WithCancel(ctx))
}
// NewCancelContextFunc stores the cancel function cancel in the context ctx.
// the returned context can be provided to InvokeCancel to cancel the context.
func NewCancelContextFunc(ctx context.Context, cancel context.CancelFunc) (cancelCtx context.Context) {
return context.WithValue(ctx, cancelKey, cancel)
}
func HasCancel(ctx context.Context) (hasCancel bool) {
if ctx == nil {
return
}
cancel, _ := ctx.Value(cancelKey).(context.CancelFunc)
return cancel != nil
}
// InvokeCancel finds the cancel method in the context chain and invokes it.
// ctx must have been returned by either NewCancelContext or NewCancelContextFunc.
// - ctx nil is panic
// - ctx not from NewCancelContext or NewCancelContextFunc is panic
// - thread-safe, idempotent
func InvokeCancel(ctx context.Context) {
invokeCancel(ctx)
}
func invokeCancel(ctx context.Context) {
if ctx == nil {
panic(perrors.NewPF("ctx cannot be nil"))
}
cancel, ok := ctx.Value(cancelKey).(context.CancelFunc)
if !ok {
panic(perrors.Errorf("%v", ErrNotCancelContext))
}
cancel()
// invoke notifier
fnsAny := ctx.Value(notifierKey)
fns, _ := fnsAny.([]func(slice pruntime.StackSlice))
if len(fns) == 0 {
return
}
cl := pruntime.NewStackSlice(cancelNotifierFrames)
for _, fn := range fns {
fn(cl)
}
}
// CancelOnError invokes InvokeCancel if errp has an error.
// CancelOnError is deferrable and thread-safe.
// ctx must have been returned by either NewCancelContext or NewCancelContextFunc.
// - errp == nil or *errp == nil means no error
// - ctx nil is panic
// - ctx not from NewCancelContext or NewCancelContextFunc is panic
// - thread-safe, idempotent
func CancelOnError(errp *error, ctx context.Context) {
if errp == nil || *errp == nil {
return // there was no error
}
invokeCancel(ctx)
}