You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/transport/grpc/context.go

44 lines
1.3 KiB

4 years ago
package grpc
import "context"
// ServerInfo represent gRPC server information.
4 years ago
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 represent gRPC server information.
4 years ago
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, clientKey{}, info)
4 years ago
}
// FromClientContext returns the Transport value stored in ctx, if any.
func FromClientContext(ctx context.Context) (info ClientInfo, ok bool) {
info, ok = ctx.Value(clientKey{}).(ClientInfo)
4 years ago
return
}