/
context.go
60 lines (52 loc) · 1.39 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
package rpc
import (
"context"
"github.com/smallnest/rpcx/share"
)
type ExtraContext struct {
context.Context
}
func NewContextFrom(c context.Context) *ExtraContext {
return &ExtraContext{c}
}
func NewContext() *ExtraContext {
return NewContextFrom(context.Background())
}
func (c *ExtraContext) PutReqExtra(k string, v string) *ExtraContext {
mate := c.Context.Value(share.ReqMetaDataKey)
if mate == nil {
mate = map[string]string{}
c.Context = context.WithValue(c.Context, share.ReqMetaDataKey, mate)
}
m := c.Context.Value(share.ReqMetaDataKey).(map[string]string)
m[k] = v
return c
}
func (c *ExtraContext) PutResExtra(k string, v string) *ExtraContext {
mate := c.Context.Value(share.ResMetaDataKey)
if mate == nil {
mate = map[string]string{}
c.Context = context.WithValue(c.Context, share.ResMetaDataKey, mate)
}
m := c.Context.Value(share.ResMetaDataKey).(map[string]string)
m[k] = v
return c
}
func (c *ExtraContext) GetReqExtra(k string) (string, bool) {
mate := c.Context.Value(share.ReqMetaDataKey)
if mate == nil {
return "", false
}
m := c.Context.Value(share.ReqMetaDataKey).(map[string]string)
v, ok := m[k]
return v, ok
}
func (c *ExtraContext) GetResExtra(k string) (string, bool) {
mate := c.Context.Value(share.ResMetaDataKey)
if mate == nil {
return "", false
}
m := c.Context.Value(share.ResMetaDataKey).(map[string]string)
v, ok := m[k]
return v, ok
}