/
context.go
43 lines (34 loc) · 1.27 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
package grpc
import "context"
// ServerInfo is gRPC server infomation.
type ServerInfo struct {
// Server is the service implementation the user provides. This is read-only.
Server interface{}
// FullMethod is the full RPC method string, i.e., /package.service/method.
FullMethod string
}
type serverKey struct{}
// NewServerContext returns a new Context that carries value.
func NewServerContext(ctx context.Context, info ServerInfo) context.Context {
return context.WithValue(ctx, serverKey{}, info)
}
// FromServerContext returns the Transport value stored in ctx, if any.
func FromServerContext(ctx context.Context) (info ServerInfo, ok bool) {
info, ok = ctx.Value(serverKey{}).(ServerInfo)
return
}
// ClientInfo is gRPC server infomation.
type ClientInfo struct {
// FullMethod is the full RPC method string, i.e., /package.service/method.
FullMethod string
}
type clientKey struct{}
// NewClientContext returns a new Context that carries value.
func NewClientContext(ctx context.Context, info ClientInfo) context.Context {
return context.WithValue(ctx, serverKey{}, info)
}
// FromClientContext returns the Transport value stored in ctx, if any.
func FromClientContext(ctx context.Context) (info ClientInfo, ok bool) {
info, ok = ctx.Value(serverKey{}).(ClientInfo)
return
}