-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
46 lines (36 loc) · 1.08 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
package rpc
import "context"
type contextKey string
var (
contextID = contextKey("ID")
contextHeader = contextKey("Header")
contextServiceMethod = contextKey("ServiceMethod")
)
// ContextHeader returns the rpc.Header from the supplied context.Context
func ContextHeader(ctx context.Context) Header {
v := ctx.Value(contextHeader)
if h, ok := v.(Header); ok {
return h
}
return Header{}
}
// ContextID returns the rpc request ID from the supplied context.Context
func ContextID(ctx context.Context) string {
v := ctx.Value(contextID)
if id, ok := v.(string); ok {
return id
}
return ""
}
// ContextServiceMethod returns the service method name fromt he supplied context.Context
func ContextServiceMethod(ctx context.Context) string {
v := ctx.Value(contextServiceMethod)
if sm, ok := v.(string); ok {
return sm
}
return ""
}
// ContextWithHeaders will set the current headers for a client context. This has no effect on a server.
func ContextWithHeaders(ctx context.Context, h Header) context.Context {
return context.WithValue(ctx, contextHeader, h)
}