Package list golang-github-go-kit-kit / 1f9491d
commandline and gRPC client Peter Bourgon 6 years ago
7 changed file(s) with 87 addition(s) and 37 deletion(s). Raw diff Collapse all Expand all
00 addsvc/addsvc
1 addsvc/client/addcli/addcli
12 cover.out
23
34 # Compiled Object files, Static and Dynamic libs (Shared Objects)
1919 // service.
2020 func proxyAdd(e endpoint.Endpoint, logger log.Logger) Add {
2121 return func(ctx context.Context, a, b int64) int64 {
22 resp, err := e(ctx, &reqrep.AddRequest{A: a, B: b})
22 resp, err := e(ctx, reqrep.AddRequest{A: a, B: b})
2323 if err != nil {
2424 logger.Log("err", err)
2525 return 0
2626 }
27 addResp, ok := resp.(*reqrep.AddResponse)
27 addResp, ok := resp.(reqrep.AddResponse)
2828 if !ok {
2929 logger.Log("err", endpoint.ErrBadCast)
3030 return 0
0 package main
1
2 import (
3 "flag"
4 "log"
5 "os"
6 "time"
7
8 "github.com/go-kit/kit/addsvc/reqrep"
9 "github.com/go-kit/kit/endpoint"
10 "golang.org/x/net/context"
11
12 grpcclient "github.com/go-kit/kit/addsvc/client/grpc"
13
14 "google.golang.org/grpc"
15 )
16
17 func main() {
18 // Flag domain. Note that gRPC transitively registers flags via its import
19 // of glog. So, we define a new flag set, to keep those domains distinct.
20 fs := flag.NewFlagSet("", flag.ExitOnError)
21 var (
22 grpcAddr = fs.String("grpc.addr", "localhost:8002", "gRPC address")
23 grpcTimeout = fs.Duration("grpc.timeout", 250*time.Millisecond, "gRPC dial timeout")
24 a = fs.Int64("a", 1, "a value")
25 b = fs.Int64("b", 2, "b value")
26 )
27 flag.Usage = fs.Usage // only show our flags
28 fs.Parse(os.Args[1:])
29 log.SetFlags(0)
30 log.SetOutput(os.Stdout)
31
32 cc, err := grpc.Dial(*grpcAddr, grpc.WithTimeout(*grpcTimeout))
33 if err != nil {
34 log.Fatal(err)
35 }
36
37 var e endpoint.Endpoint = grpcclient.NewClient(cc)
38 response, err := e(context.Background(), reqrep.AddRequest{A: *a, B: *b})
39 if err != nil {
40 log.Fatalf("request: %v", err)
41 }
42
43 addResponse, ok := response.(reqrep.AddResponse)
44 if !ok {
45 log.Fatalf("response: %v", endpoint.ErrBadCast)
46 }
47
48 log.Print(addResponse.V)
49 }
0 package grpc
1
2 import (
3 "golang.org/x/net/context"
4 "google.golang.org/grpc"
5
6 "github.com/go-kit/kit/addsvc/pb"
7 "github.com/go-kit/kit/addsvc/reqrep"
8 "github.com/go-kit/kit/endpoint"
9 )
10
11 // NewClient takes a gRPC ClientConn that should point to an instance of an
12 // addsvc. It returns an endpoint that wraps and invokes that ClientConn.
13 func NewClient(cc *grpc.ClientConn) endpoint.Endpoint {
14 client := pb.NewAddClient(cc)
15 return func(ctx context.Context, request interface{}) (interface{}, error) {
16 addReq, ok := request.(reqrep.AddRequest)
17 if !ok {
18 return nil, endpoint.ErrBadCast
19 }
20 reply, err := client.Add(ctx, &pb.AddRequest{A: addReq.A, B: addReq.B})
21 if err != nil {
22 return nil, err
23 }
24 return reqrep.AddResponse{V: reply.V}, nil
25 }
26 }
+0
-27
addsvc/client/grpc/grpc_client.go less more
0 package grpc
1
2 import (
3 "golang.org/x/net/context"
4 "google.golang.org/grpc"
5
6 "github.com/go-kit/kit/addsvc/pb"
7 "github.com/go-kit/kit/addsvc/reqrep"
8 "github.com/go-kit/kit/endpoint"
9 )
10
11 // NewClient takes a gRPC ClientConn that should point to an instance of an
12 // addsvc. It returns an endpoint that wraps and invokes that ClientConn.
13 func NewClient(cc *grpc.ClientConn) endpoint.Endpoint {
14 client := pb.NewAddClient(cc)
15 return func(ctx context.Context, request interface{}) (interface{}, error) {
16 addReq, ok := request.(*reqrep.AddRequest)
17 if !ok {
18 return nil, endpoint.ErrBadCast
19 }
20 reply, err := client.Add(ctx, &pb.AddRequest{A: addReq.A, B: addReq.B})
21 if err != nil {
22 return nil, err
23 }
24 return &reqrep.AddResponse{V: reply.V}, nil
25 }
26 }
1818 return nil, endpoint.ErrContextCanceled
1919 }
2020
21 addReq, ok := request.(*reqrep.AddRequest)
21 addReq, ok := request.(reqrep.AddRequest)
2222 if !ok {
23 println("### 1")
2324 return nil, endpoint.ErrBadCast
2425 }
2526
2627 v := a(ctx, addReq.A, addReq.B)
2728
28 return &reqrep.AddResponse{V: v}, nil
29 return reqrep.AddResponse{V: v}, nil
2930 }
3031 }
2020 // way to manipulate the RPC context, like headers for HTTP. So we don't have
2121 // a way to transport e.g. Zipkin IDs with the request. TODO.
2222 func (b grpcBinding) Add(ctx context.Context, req *pb.AddRequest) (*pb.AddReply, error) {
23 addReq := &reqrep.AddRequest{A: req.A, B: req.B}
24 r, err := b.Endpoint(ctx, addReq)
23 r, err := b.Endpoint(ctx, reqrep.AddRequest{A: req.A, B: req.B})
2524 if err != nil {
2625 return nil, err
2726 }
2827
29 resp, ok := r.(*reqrep.AddResponse)
28 resp, ok := r.(reqrep.AddResponse)
3029 if !ok {
30 println("### 2")
3131 return nil, endpoint.ErrBadCast
3232 }
3333
34 return &pb.AddReply{
35 V: resp.V,
36 }, nil
34 return &pb.AddReply{V: resp.V}, nil
3735 }
3836
3937 func grpcInstrument(requests metrics.Counter, duration metrics.Histogram) func(pb.AddServer) pb.AddServer {