Package list golang-github-go-kit-kit / c095d92
Fixed grpc.Client to be go-routine safe. Bas van Beek 6 years ago
1 changed file(s) with 15 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 grpcReply: reflect.TypeOf(
41 reflect.Indirect(
42 reflect.ValueOf(grpcReply),
43 ).Interface(),
44 ),
45 before: []RequestFunc{},
4146 }
4247 for _, option := range options {
4348 option(c)
7277 }
7378 ctx = metadata.NewContext(ctx, *md)
7479
75 if err = grpc.Invoke(ctx, c.method, req, c.grpcReply, c.client); err != nil {
80 grpcReply := reflect.New(c.grpcReply).Interface()
81 if err = grpc.Invoke(ctx, c.method, req, grpcReply, c.client); err != nil {
7682 return nil, fmt.Errorf("Invoke: %v", err)
7783 }
7884
79 response, err := c.dec(ctx, c.grpcReply)
85 response, err := c.dec(ctx, grpcReply)
8086 if err != nil {
8187 return nil, fmt.Errorf("Decode: %v", err)
8288 }