-
Notifications
You must be signed in to change notification settings - Fork 1
/
ctx_keys.go
77 lines (60 loc) · 1.68 KB
/
ctx_keys.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
75
76
77
package gmicro
import (
"context"
"google.golang.org/grpc/metadata"
)
// CtxKey ctx key type.
type CtxKey string
const (
// XRequestID request_id
XRequestID CtxKey = "x-request-id"
// GRPCClientIP grpc client_ip
GRPCClientIP CtxKey = "client-ip"
// RequestMethod request method
RequestMethod CtxKey = "request_method"
// RequestURI request uri
RequestURI CtxKey = "request_uri"
)
// String returns string
func (c CtxKey) String() string {
return string(c)
}
// GetIncomingMD returns metadata.MD from incoming ctx
// get request metadata
// this method is mainly used at the server end to get the relevant metadata data
func GetIncomingMD(ctx context.Context) metadata.MD {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return metadata.MD{}
}
return md
}
// GetOutgoingMD returns metadata.MD from outgoing ctx
// Use this method when you pass ctx to a downstream service
func GetOutgoingMD(ctx context.Context) metadata.MD {
md, ok := metadata.FromOutgoingContext(ctx)
if !ok {
return metadata.MD{}
}
return md
}
// GetSliceFromMD returns []string from md
func GetSliceFromMD(md metadata.MD, key CtxKey) []string {
return md.Get(key.String())
}
// GetSliceFromMD returns string from md
func GetStringFromMD(md metadata.MD, key CtxKey) string {
values := md.Get(key.String())
if len(values) > 0 {
return values[0]
}
return ""
}
// SetCtxValue returns ctx when you set key/value into ctx
func SetCtxValue(ctx context.Context, key CtxKey, val interface{}) context.Context {
return context.WithValue(ctx, key.String(), val)
}
// GetCtxValue returns ctx when you set key/value into ctx
func GetCtxValue(ctx context.Context, key CtxKey) interface{} {
return ctx.Value(key)
}