forked from bytewayio/cypress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
69 lines (58 loc) · 1.33 KB
/
context.go
File metadata and controls
69 lines (58 loc) · 1.33 KB
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
package cypress
import (
"context"
"sync"
"time"
)
// TraceActivityIDKey context key for trace activity id
const (
TraceActivityIDKey = "TraceActivityID"
UserPrincipalKey = "UserPrincipal"
SessionKey = "UserSession"
)
type multiValueCtx struct {
lock *sync.RWMutex
values map[string]interface{}
parent context.Context
}
func extentContext(ctx context.Context) *multiValueCtx {
if ctx == nil {
panic("source context cannot be nil")
}
return &multiValueCtx{
lock: &sync.RWMutex{},
values: make(map[string]interface{}),
parent: ctx,
}
}
// Deadline deadline of the context
func (ctx *multiValueCtx) Deadline() (deadline time.Time, ok bool) {
return ctx.parent.Deadline()
}
// Done done channel
func (ctx *multiValueCtx) Done() <-chan struct{} {
return ctx.parent.Done()
}
// Err error of context
func (ctx *multiValueCtx) Err() error {
return ctx.parent.Err()
}
// Value value for the given key
func (ctx *multiValueCtx) Value(contextKey interface{}) interface{} {
ctx.lock.RLock()
defer ctx.lock.RUnlock()
key, ok := contextKey.(string)
if ok {
value, ok := ctx.values[key]
if ok {
return value
}
}
return ctx.parent.Value(contextKey)
}
func (ctx *multiValueCtx) withValue(key string, value interface{}) *multiValueCtx {
ctx.lock.Lock()
defer ctx.lock.Unlock()
ctx.values[key] = value
return ctx
}