-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
52 lines (45 loc) · 1.57 KB
/
context.go
File metadata and controls
52 lines (45 loc) · 1.57 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
package tracing
import (
"context"
)
type cloneContext struct {
context.Context // embedded context
parent context.Context
}
func (c *cloneContext) Value(key any) any {
// look for value in new context
val := c.Context.Value(key)
if val != nil {
return val
}
// if not found return from old context
return c.parent.Value(key)
}
// Deprecated: Use [NewContextWithParentValues] instead.
//
//go:fix inline
func CloneContextValues(parent context.Context) context.Context {
return NewContextWithParentValues(parent)
}
// NewContextWithParentValues clones a given context values and returns a new context obj which is not affected by Cancel, Deadline etc
// can be used to pass context values to a new context which is not affected by the parent context cancel/deadline etc from parent
func NewContextWithParentValues(parent context.Context) context.Context {
return &cloneContext{
parent: parent,
Context: context.Background(),
}
}
// Deprecated: Use [MergeContextValues] instead.
//
//go:fix inline
func MergeParentContext(parent context.Context, main context.Context) context.Context {
return MergeContextValues(parent, main)
}
// MergeContextValues merged the given main context with a parent context, Cancel/Deadline etc are used from the main context and values are looked in both the contexts
// can be use to merge a parent context with a new context, the new context will have the values from both the contexts
func MergeContextValues(parent context.Context, main context.Context) context.Context {
return &cloneContext{
parent: parent,
Context: main,
}
}