-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
172 lines (151 loc) · 4.88 KB
/
metadata.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package grpc
import (
"github.com/golang/glog"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
vexpb "github.com/binchencoder/gateway-proto/data"
"github.com/binchencoder/letsgo/hashring"
"github.com/binchencoder/letsgo/ident"
"github.com/binchencoder/letsgo/trace"
)
const (
mdKeyAid = "mdkey_aid"
mdKeyClientName = "mdkey_client_name"
mdKeyCid = "mdkey_cid"
mdKeyUid = "mdkey_uid"
mdKeyCustomid = "mdkey_customid"
mdKeyHashKey = "mdkey_hashkey"
mdKeyTraceid = "mdkey_traceid"
)
// FromMetadataIncoming returns a copy of incoming parent context in which the
// known context values (trace ID, user entities, etc.) will be copied from
// a gRPC metadata to context. Also the client's name will be return.
// If the metadata doesn't exist, the incoming parent context will be returned
// with an empty string for client's name.
//
// If the metadata in the incoming parent context is unknown, it will not be
// passed down.
func FromMetadataIncoming(incoming context.Context) (context.Context, string) {
md, ok := metadata.FromIncomingContext(incoming)
if !ok {
return incoming, ""
}
glog.V(4).Infof("FromMetadataIncoming: %#v", md)
var userDetails *vexpb.UserDetails
ctx := incoming
if cid, ok := md[mdKeyCid]; ok {
if uid, ok := md[mdKeyUid]; ok {
userDetails = &vexpb.UserDetails{
CompanyId: getLastEle(cid),
UserId: getLastEle(uid),
}
}
}
if aid, ok := md[mdKeyAid]; ok {
if userDetails == nil {
userDetails = &vexpb.UserDetails{
AccountId: getLastEle(aid),
}
} else {
userDetails.AccountId = getLastEle(aid)
}
}
if userDetails != nil {
ctx = ident.WithUserDetails(ctx, userDetails)
}
if custId, ok := md[mdKeyCustomid]; ok {
ctx = ident.WithCustomIdent(ctx, getLastEle(custId))
}
if tid, ok := md[mdKeyTraceid]; ok {
ctx = trace.WithTraceId(ctx, getLastEle(tid))
} else {
ctx = trace.NewTraceId(ctx)
}
if hashKey, ok := md[mdKeyHashKey]; ok {
ctx = hashring.WithHashKey(ctx, getLastEle(hashKey))
}
var clientName string
if cname, ok := md[mdKeyClientName]; ok {
clientName = getLastEle(cname)
}
return ctx, clientName
}
// ToMetadataOutgoing returns a copy of outgoing parent context in which the known
// context values (trace ID, user entities, etc.) will be copied to
// a gRPC metadata, being able to transmitted to server side.
//
// If the outgoing parent context already has metadata, the metadata will be
// joined.
func ToMetadataOutgoing(outgoing context.Context, clientName string) context.Context {
values := make(map[string]string)
if clientName != "" {
values[mdKeyClientName] = clientName
}
// User identity.
if userDetails, ok := ident.GetUserDetails(outgoing); ok {
if userDetails.CompanyId != "" {
values[mdKeyCid] = userDetails.CompanyId
}
if userDetails.UserId != "" {
values[mdKeyUid] = userDetails.UserId
}
if userDetails.AccountId != "" {
values[mdKeyAid] = userDetails.AccountId
}
}
// Custom identity.
if custId, ok := ident.GetCustomIdent(outgoing); ok {
values[mdKeyCustomid] = custId
}
// Trace ID.
if tid, ok := trace.GetTraceId(outgoing); ok {
values[mdKeyTraceid] = tid
} else {
values[mdKeyTraceid] = trace.GenerateTraceId()
}
// Hash Key.
if hkey, ok := hashring.GetHashKey(outgoing); ok {
values[mdKeyHashKey] = hkey
}
md := metadata.New(values)
// If outgoing parent has already metadata, pass it down.
existingMd, ok := metadata.FromOutgoingContext(outgoing)
if ok {
md = metadata.Join(existingMd, md)
}
glog.V(4).Infof("ToMetadataOutgoing: %#v", md)
return metadata.NewOutgoingContext(outgoing, md)
}
// ClientToMetadataInterceptor is a gRPC client-side interceptor that put
// context value of interest into metadata for Unary RPCs.
func ClientToMetadataInterceptor(svcName string, ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
ctx = ToMetadataOutgoing(ctx, svcName)
return invoker(ctx, method, req, reply, cc, opts...)
}
// ServerFromMetadataInterceptor is a gRPC server-side interceptor that put
// metadata as context values for Unary RPCs.
func ServerFromMetadataInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
ctx, _ = FromMetadataIncoming(ctx)
if glog.V(2) {
if tid, ok := trace.GetTraceId(ctx); ok {
glog.Infof("traceId: %s", tid)
}
}
return handler(ctx, req)
}
// ToIncomingCtx creates a new incoming context with the metadata from an
// outgoing context.
func ToIncomingCtx(outgoing context.Context) context.Context {
if md, ok := metadata.FromOutgoingContext(outgoing); ok {
return metadata.NewIncomingContext(context.Background(), md)
}
return metadata.NewIncomingContext(context.Background(), nil)
}
func getLastEle(slice []string) string {
count := len(slice)
if count == 0 {
return ""
}
return slice[count-1]
}