-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergedcontext.go
46 lines (37 loc) · 995 Bytes
/
mergedcontext.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
package yarxyarx
import (
"context"
"time"
)
// based loosely on https://medium.com/@dlagoza/playing-with-multiple-contexts-in-go-9f72cbcff56e
type mergedContext struct {
mainCtx context.Context
xrayCtx context.Context
err error
}
// X-Ray doesn't use these three methods
func (c *mergedContext) Done() <-chan struct{} {
return c.mainCtx.Done()
}
func (c *mergedContext) Err() error {
return c.mainCtx.Err()
}
func (c *mergedContext) Deadline() (deadline time.Time, ok bool) {
return c.mainCtx.Deadline()
}
func (c *mergedContext) Value(key interface{}) interface{} {
// if the regular context doesn't have this value, it's probably in the X-Ray context
v := c.mainCtx.Value(key)
if v == nil {
v = c.xrayCtx.Value(key)
}
return v
}
func (c *mergedContext) run() {
<-c.mainCtx.Done()
}
func mergeContexts(mainCtx, otherCtx context.Context) context.Context {
c := &mergedContext{mainCtx: mainCtx, xrayCtx: otherCtx }
go c.run()
return c
}