Package list golang-github-go-kit-kit / 9b3848b
Merge pull request #218 from basvanbeek/grpc-fix Fixed grpc.Client to be go-routine safe. Peter Bourgon 6 years ago
1 changed file(s) with 19 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
11
22 import (
33 "fmt"
4 "reflect"
45
56 "golang.org/x/net/context"
67 "google.golang.org/grpc"
1718 method string
1819 enc EncodeRequestFunc
1920 dec DecodeResponseFunc
20 grpcReply interface{}
21 grpcReply reflect.Type
2122 before []RequestFunc
2223 }
2324
3233 options ...ClientOption,
3334 ) *Client {
3435 c := &Client{
35 client: cc,
36 method: fmt.Sprintf("/pb.%s/%s", serviceName, method),
37 enc: enc,
38 dec: dec,
39 grpcReply: grpcReply,
40 before: []RequestFunc{},
36 client: cc,
37 method: fmt.Sprintf("/pb.%s/%s", serviceName, method),
38 enc: enc,
39 dec: dec,
40 // We are using reflect.Indirect here to allow both reply structs and
41 // pointers to these reply structs. New consumers of the client should
42 // use structs directly, while existing consumers will not break if they
43 // remain to use pointers to structs.
44 grpcReply: reflect.TypeOf(
45 reflect.Indirect(
46 reflect.ValueOf(grpcReply),
47 ).Interface(),
48 ),
49 before: []RequestFunc{},
4150 }
4251 for _, option := range options {
4352 option(c)
7281 }
7382 ctx = metadata.NewContext(ctx, *md)
7483
75 if err = grpc.Invoke(ctx, c.method, req, c.grpcReply, c.client); err != nil {
84 grpcReply := reflect.New(c.grpcReply).Interface()
85 if err = grpc.Invoke(ctx, c.method, req, grpcReply, c.client); err != nil {
7686 return nil, fmt.Errorf("Invoke: %v", err)
7787 }
7888
79 response, err := c.dec(ctx, c.grpcReply)
89 response, err := c.dec(ctx, grpcReply)
8090 if err != nil {
8191 return nil, fmt.Errorf("Decode: %v", err)
8292 }