-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
92 lines (76 loc) · 2.28 KB
/
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
package op
import (
"context"
"os"
"os/signal"
"sync"
"syscall"
)
type ctxKey uint8
const (
opKey ctxKey = 0
)
// CancelFunc is a function that cancel a running context before returning an
// os.Signal if one has been received. After being called once, the function
// will continue to return the same response. Multiple concurrent calls are
// safe.
type CancelFunc = func() os.Signal
// closedchan is a reusable closed channel.
var closedchan = make(chan struct{})
func init() {
close(closedchan)
}
// ProgramContext returns a context that is canceled if one of the following
// signals are received by the program: os.Interrupt, syscall.SIGTERM,
// syscall.SIGQUIT. This is equivalent to passing a channel of size 1 to
// ContextWithCancelSignals that is notified by the same signals.
func ProgramContext() (context.Context, CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
return ContextWithCancelSignals(context.Background(), c)
}
// ContextWithCancelSignals returns a context that is cancelled when a signal
// is received on c (or if c is closed).
func ContextWithCancelSignals(parent context.Context, c <-chan os.Signal) (context.Context, CancelFunc) {
ctx, cancel := context.WithCancel(parent)
var mu sync.Mutex
var s os.Signal
// Lock signal retrival until we can confirm that s will no longer be
// modified.
mu.Lock()
go func() {
defer mu.Unlock()
// Run until signal received or the (parent) context is canceled.
select {
case <-ctx.Done():
case s = <-c:
cancel()
}
}()
// f will cancel ctx and return the value of s.
f := func() os.Signal {
cancel()
mu.Lock()
defer mu.Unlock()
return s
}
return ctx, f
}
// ContextKey returns a concatenated operation key from context. Operation keys
// are added to context when an operation is started by a Handler. In the case
// of nested operations, keys are joined by a single dot (.). If there are no
// operation keys in context, an empty string is returned.
func ContextKey(ctx context.Context) string {
s, _ := ctx.Value(opKey).(string)
return s
}
func contextWithKey(ctx context.Context, key string) context.Context {
if key == "" {
return ctx
}
s := ContextKey(ctx)
if s != "" {
key = s + "." + key
}
return context.WithValue(ctx, opKey, key)
}