-
Notifications
You must be signed in to change notification settings - Fork 18
/
contexthelper.go
120 lines (99 loc) · 2.55 KB
/
contexthelper.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
package contexthelper
import (
"context"
"reflect"
"sync"
"time"
)
type CombinedContext struct {
contexts []context.Context
deadline time.Time
deadlineOk bool
mu sync.RWMutex
done chan struct{}
err error
}
type droppedCancelContext struct {
ctx context.Context
}
// Deadline return minimum of contextx deadlines, if present
func (cc *CombinedContext) Deadline() (deadline time.Time, ok bool) {
return cc.deadline, cc.deadlineOk
}
// Done return channel, that will close when first of initial contexts will closed
func (cc *CombinedContext) Done() <-chan struct{} {
return cc.done
}
// Err return error, of context, which close handled first
func (cc *CombinedContext) Err() error {
cc.mu.RLock()
res := cc.err
cc.mu.RUnlock()
return res
}
// Value return value of key. It iterate over initial context one by one and return first not nil value.
// If all of contextx return nil - return nil too
func (cc *CombinedContext) Value(key interface{}) interface{} {
for _, ctx := range cc.contexts {
val := ctx.Value(key)
if val != nil {
return val
}
}
return nil
}
// CombineContext return combined context: common deadline, values, close.
// Minimum one of underly contexts MUST be closed for prevent memory leak.
func CombineContext(contexts ...context.Context) *CombinedContext {
res := &CombinedContext{
contexts: contexts,
done: make(chan struct{}),
}
var deadlineMin time.Time
var deadlineOk bool
for _, ctx := range contexts {
deadline, ok := ctx.Deadline()
if !ok {
continue
}
if deadlineOk && deadlineMin.Before(deadline) {
continue
}
deadlineOk = true
deadlineMin = deadline
}
res.deadline, res.deadlineOk = deadlineMin, deadlineOk
// handlepanic: no external call
go res.waitCloseAny()
return res
}
func (cc *CombinedContext) waitCloseAny() {
selectCases := make([]reflect.SelectCase, len(cc.contexts))
for i, ctx := range cc.contexts {
selectCases[i] = reflect.SelectCase{
Dir: reflect.SelectRecv,
Chan: reflect.ValueOf(ctx.Done()),
}
}
index, _, _ := reflect.Select(selectCases)
err := cc.contexts[index].Err()
cc.mu.Lock()
cc.err = err
close(cc.done)
cc.mu.Unlock()
}
func DropCancelContext(ctx context.Context) context.Context {
return droppedCancelContext{ctx}
}
func (d droppedCancelContext) Deadline() (deadline time.Time, ok bool) {
return time.Time{}, false
}
func (d droppedCancelContext) Done() <-chan struct{} {
return nil
}
func (d droppedCancelContext) Err() error {
return nil
}
func (d droppedCancelContext) Value(key interface{}) interface{} {
return d.ctx.Value(key)
}