-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcontext_impl.go
80 lines (63 loc) · 1.68 KB
/
context_impl.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
package floc
import (
"context"
"sync"
)
type flowContext struct {
ctx context.Context
mu sync.RWMutex
}
// BorrowContext constructs new instance from the context given.
// The function panics if the context given is nil.
func BorrowContext(ctx context.Context) Context {
if ctx == nil {
panic("context is nil")
}
return &flowContext{
ctx: ctx,
mu: sync.RWMutex{},
}
}
// NewContext constructs new instance of TODO context.
func NewContext() Context {
return &flowContext{
ctx: context.TODO(),
mu: sync.RWMutex{},
}
}
// Release releases resources.
func (flowCtx *flowContext) Release() {
}
// Ctx returns the underlying context.
func (flowCtx *flowContext) Ctx() context.Context {
flowCtx.mu.RLock()
defer flowCtx.mu.RUnlock()
return flowCtx.ctx
}
// UpdateCtx sets the new underlying context.
func (flowCtx *flowContext) UpdateCtx(ctx context.Context) {
flowCtx.mu.Lock()
defer flowCtx.mu.Unlock()
flowCtx.ctx = ctx
}
// Done returns a channel that's closed when the flow done.
// Successive calls to Done return the same value.
func (flowCtx *flowContext) Done() <-chan struct{} {
flowCtx.mu.RLock()
defer flowCtx.mu.RUnlock()
return flowCtx.ctx.Done()
}
// Value returns the value associated with this context for key,
// or nil if no value is associated with key.
func (flowCtx *flowContext) Value(key interface{}) (value interface{}) {
flowCtx.mu.RLock()
ctx := flowCtx.ctx
flowCtx.mu.RUnlock()
return ctx.Value(key)
}
// AddValue creates a new context with value and make it the current.
func (flowCtx *flowContext) AddValue(key, value interface{}) {
flowCtx.mu.Lock()
defer flowCtx.mu.Unlock()
flowCtx.ctx = context.WithValue(flowCtx.ctx, key, value)
}