forked from jacexh/ultron
/
context.go
74 lines (63 loc) · 1.68 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
package ultron
import (
"context"
"time"
)
type (
// executorSharedContext is designed to carry the fire-scoped values.
executorSharedContext struct {
parentCtx context.Context
counter int32
resources map[int32]map[string]interface{}
}
)
func newExecutorSharedContext(ctx context.Context) context.Context {
return &executorSharedContext{
parentCtx: ctx,
resources: make(map[int32]map[string]interface{}),
}
}
func (m *executorSharedContext) Deadline() (time.Time, bool) {
return m.parentCtx.Deadline()
}
func (m *executorSharedContext) Done() <-chan struct{} {
return m.parentCtx.Done()
}
func (m *executorSharedContext) Err() error {
return m.parentCtx.Err()
}
func (m *executorSharedContext) Value(key interface{}) interface{} {
return m.parentCtx.Value(key)
}
func FromContext(ctx context.Context, key string) (interface{}, bool) {
entity, ok := ctx.(*executorSharedContext)
if ok {
if res, ok := entity.resources[entity.counter]; ok {
return res[key], true
}
}
return nil, false
}
func StoreInContext(ctx context.Context, key string, value interface{}) bool {
if entity, ok := ctx.(*executorSharedContext); ok {
if _, ok := entity.resources[entity.counter]; !ok {
entity.resources[entity.counter] = make(map[string]interface{})
}
entity.resources[entity.counter][key] = value
return true
}
return false
}
func ClearStorageInContext(ctx context.Context) {
if entity, ok := ctx.(*executorSharedContext); ok {
delete(entity.resources, entity.counter)
}
}
func AllocateStorageInContext(ctx context.Context) context.Context {
if entity, ok := ctx.(*executorSharedContext); ok {
entity.counter++
} else {
ctx = newExecutorSharedContext(ctx)
}
return ctx
}