transport/grpc, examples/addsvc: adapt context
Peter Bourgon
6 years ago
6 | 6 | "context" |
7 | 7 | |
8 | 8 | stdopentracing "github.com/opentracing/opentracing-go" |
9 | oldcontext "golang.org/x/net/context" | |
9 | 10 | |
10 | 11 | "github.com/go-kit/kit/examples/addsvc/pb" |
11 | 12 | "github.com/go-kit/kit/log" |
41 | 42 | concat grpctransport.Handler |
42 | 43 | } |
43 | 44 | |
44 | func (s *grpcServer) Sum(ctx context.Context, req *pb.SumRequest) (*pb.SumReply, error) { | |
45 | func (s *grpcServer) Sum(ctx oldcontext.Context, req *pb.SumRequest) (*pb.SumReply, error) { | |
45 | 46 | _, rep, err := s.sum.ServeGRPC(ctx, req) |
46 | 47 | if err != nil { |
47 | 48 | return nil, err |
49 | 50 | return rep.(*pb.SumReply), nil |
50 | 51 | } |
51 | 52 | |
52 | func (s *grpcServer) Concat(ctx context.Context, req *pb.ConcatRequest) (*pb.ConcatReply, error) { | |
53 | func (s *grpcServer) Concat(ctx oldcontext.Context, req *pb.ConcatRequest) (*pb.ConcatReply, error) { | |
53 | 54 | _, rep, err := s.concat.ServeGRPC(ctx, req) |
54 | 55 | if err != nil { |
55 | 56 | return nil, err |
0 | 0 | package grpc |
1 | 1 | |
2 | 2 | import ( |
3 | "context" | |
3 | 4 | "fmt" |
4 | 5 | "reflect" |
5 | 6 | "strings" |
6 | 7 | |
7 | "golang.org/x/net/context" | |
8 | 8 | "google.golang.org/grpc" |
9 | 9 | "google.golang.org/grpc/metadata" |
10 | 10 |
0 | 0 | package grpc |
1 | 1 | |
2 | 2 | import ( |
3 | "golang.org/x/net/context" | |
3 | "context" | |
4 | 4 | ) |
5 | 5 | |
6 | 6 | // DecodeRequestFunc extracts a user-domain request object from a gRPC request. |
7 | 7 | // It's designed to be used in gRPC servers, for server-side endpoints. One |
8 | // straightforward DecodeRequestFunc could be something that | |
9 | // decodes from the gRPC request message to the concrete request type. | |
8 | // straightforward DecodeRequestFunc could be something that decodes from the | |
9 | // gRPC request message to the concrete request type. | |
10 | 10 | type DecodeRequestFunc func(context.Context, interface{}) (request interface{}, err error) |
11 | 11 | |
12 | 12 | // EncodeRequestFunc encodes the passed request object into the gRPC request |
13 | // object. It's designed to be used in gRPC clients, for client-side | |
14 | // endpoints. One straightforward EncodeRequestFunc could something that | |
15 | // encodes the object directly to the gRPC request message. | |
13 | // object. It's designed to be used in gRPC clients, for client-side endpoints. | |
14 | // One straightforward EncodeRequestFunc could something that encodes the object | |
15 | // directly to the gRPC request message. | |
16 | 16 | type EncodeRequestFunc func(context.Context, interface{}) (request interface{}, err error) |
17 | 17 | |
18 | 18 | // EncodeResponseFunc encodes the passed response object to the gRPC response |
19 | // message. It's designed to be used in gRPC servers, for server-side | |
20 | // endpoints. One straightforward EncodeResponseFunc could be something that | |
21 | // encodes the object directly to the gRPC response message. | |
19 | // message. It's designed to be used in gRPC servers, for server-side endpoints. | |
20 | // One straightforward EncodeResponseFunc could be something that encodes the | |
21 | // object directly to the gRPC response message. | |
22 | 22 | type EncodeResponseFunc func(context.Context, interface{}) (response interface{}, err error) |
23 | 23 | |
24 | 24 | // DecodeResponseFunc extracts a user-domain response object from a gRPC |
0 | 0 | package grpc |
1 | 1 | |
2 | 2 | import ( |
3 | "context" | |
3 | 4 | "encoding/base64" |
4 | 5 | "strings" |
5 | 6 | |
6 | "golang.org/x/net/context" | |
7 | 7 | "google.golang.org/grpc/metadata" |
8 | 8 | ) |
9 | 9 |
0 | 0 | package grpc |
1 | 1 | |
2 | 2 | import ( |
3 | "golang.org/x/net/context" | |
3 | "context" | |
4 | ||
5 | oldcontext "golang.org/x/net/context" | |
4 | 6 | "google.golang.org/grpc/metadata" |
5 | 7 | |
6 | 8 | "github.com/go-kit/kit/endpoint" |
7 | 9 | "github.com/go-kit/kit/log" |
8 | 10 | ) |
9 | 11 | |
10 | // Handler which should be called from the grpc binding of the service | |
12 | // Handler which should be called from the gRPC binding of the service | |
11 | 13 | // implementation. The incoming request parameter, and returned response |
12 | 14 | // parameter, are both gRPC types, not user-domain. |
13 | 15 | type Handler interface { |
14 | ServeGRPC(ctx context.Context, request interface{}) (context.Context, interface{}, error) | |
16 | ServeGRPC(ctx oldcontext.Context, request interface{}) (oldcontext.Context, interface{}, error) | |
15 | 17 | } |
16 | 18 | |
17 | 19 | // Server wraps an endpoint and implements grpc.Handler. |
72 | 74 | } |
73 | 75 | |
74 | 76 | // ServeGRPC implements the Handler interface. |
75 | func (s Server) ServeGRPC(grpcCtx context.Context, req interface{}) (context.Context, interface{}, error) { | |
77 | func (s Server) ServeGRPC(grpcCtx oldcontext.Context, req interface{}) (oldcontext.Context, interface{}, error) { | |
76 | 78 | ctx := s.ctx |
77 | 79 | |
78 | 80 | // Retrieve gRPC metadata. |