Codebase list golang-github-go-kit-kit / 3018f50
reqrep Peter Bourgon 8 years ago
7 changed file(s) with 30 addition(s) and 23 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "golang.org/x/net/context"
44
5 "github.com/go-kit/kit/addsvc/reqrep"
56 "github.com/go-kit/kit/endpoint"
67 "github.com/go-kit/kit/log"
78 )
1819 // service.
1920 func proxyAdd(e endpoint.Endpoint, logger log.Logger) Add {
2021 return func(ctx context.Context, a, b int64) int64 {
21 resp, err := e(ctx, &addRequest{a, b})
22 resp, err := e(ctx, &reqrep.AddRequest{A: a, B: b})
2223 if err != nil {
2324 logger.Log("err", err)
2425 return 0
2526 }
26 addResp, ok := resp.(*addResponse)
27 addResp, ok := resp.(*reqrep.AddResponse)
2728 if !ok {
2829 logger.Log("err", endpoint.ErrBadCast)
2930 return 0
22 import (
33 "golang.org/x/net/context"
44
5 "github.com/go-kit/kit/addsvc/reqrep"
56 "github.com/go-kit/kit/endpoint"
67 )
78
1718 return nil, endpoint.ErrContextCanceled
1819 }
1920
20 addReq, ok := request.(*addRequest)
21 addReq, ok := request.(*reqrep.AddRequest)
2122 if !ok {
2223 return nil, endpoint.ErrBadCast
2324 }
2425
2526 v := a(ctx, addReq.A, addReq.B)
2627
27 return addResponse{V: v}, nil
28 return &reqrep.AddResponse{V: v}, nil
2829 }
2930 }
55 "golang.org/x/net/context"
66
77 "github.com/go-kit/kit/addsvc/pb"
8 "github.com/go-kit/kit/addsvc/reqrep"
89 "github.com/go-kit/kit/endpoint"
910 "github.com/go-kit/kit/metrics"
1011 )
1920 // way to manipulate the RPC context, like headers for HTTP. So we don't have
2021 // a way to transport e.g. Zipkin IDs with the request. TODO.
2122 func (b grpcBinding) Add(ctx context.Context, req *pb.AddRequest) (*pb.AddReply, error) {
22 addReq := addRequest{req.A, req.B}
23 addReq := reqrep.AddRequest{A: req.A, B: req.B}
2324 r, err := b.Endpoint(ctx, addReq)
2425 if err != nil {
2526 return nil, err
2627 }
2728
28 resp, ok := r.(*addResponse)
29 resp, ok := r.(*reqrep.AddResponse)
2930 if !ok {
3031 return nil, endpoint.ErrBadCast
3132 }
2323
2424 thriftadd "github.com/go-kit/kit/addsvc/_thrift/gen-go/add"
2525 "github.com/go-kit/kit/addsvc/pb"
26 "github.com/go-kit/kit/addsvc/reqrep"
2627 "github.com/go-kit/kit/endpoint"
2728 kitlog "github.com/go-kit/kit/log"
2829 "github.com/go-kit/kit/metrics"
110111 var a Add = pureAdd
111112 if *proxyHTTPAddr != "" {
112113 codec := jsoncodec.New()
113 makeResponse := func() interface{} { return &addResponse{} }
114 makeResponse := func() interface{} { return &reqrep.AddResponse{} }
114115
115116 var e endpoint.Endpoint
116117 e = httptransport.NewClient(*proxyHTTPAddr, codec, makeResponse, httptransport.ClientBefore(zipkin.ToRequest(zipkinSpanFunc)))
148149 field := metrics.Field{Key: "transport", Value: "http"}
149150 before := httptransport.BindingBefore(zipkin.ToContext(zipkinSpanFunc))
150151 after := httptransport.BindingAfter(httptransport.SetContentType("application/json"))
151 makeRequest := func() interface{} { return &addRequest{} }
152 makeRequest := func() interface{} { return &reqrep.AddRequest{} }
152153
153154 var handler http.Handler
154155 handler = httptransport.NewBinding(ctx, makeRequest, jsoncodec.New(), e, before, after)
0 package reqrep
1
2 // The request and response types should be annotated sufficiently for all
3 // transports we intend to use.
4
5 // AddRequest is a request for the add method.
6 type AddRequest struct {
7 A int64 `json:"a"`
8 B int64 `json:"b"`
9 }
10
11 // AddResponse is a response to the add method.
12 type AddResponse struct {
13 V int64 `json:"v"`
14 }
+0
-13
addsvc/request_response.go less more
0 package main
1
2 // The request and response types should be annotated sufficiently for all
3 // transports we intend to use.
4
5 type addRequest struct {
6 A int64 `json:"a"`
7 B int64 `json:"b"`
8 }
9
10 type addResponse struct {
11 V int64 `json:"v"`
12 }
55 "golang.org/x/net/context"
66
77 thriftadd "github.com/go-kit/kit/addsvc/_thrift/gen-go/add"
8 "github.com/go-kit/kit/addsvc/reqrep"
89 "github.com/go-kit/kit/endpoint"
910 "github.com/go-kit/kit/metrics"
1011 )
1819
1920 // Add implements Thrift's AddService interface.
2021 func (tb thriftBinding) Add(a, b int64) (*thriftadd.AddReply, error) {
21 r, err := tb.Endpoint(tb.Context, addRequest{a, b})
22 r, err := tb.Endpoint(tb.Context, reqrep.AddRequest{A: a, B: b})
2223 if err != nil {
2324 return nil, err
2425 }
2526
26 resp, ok := r.(*addResponse)
27 resp, ok := r.(*reqrep.AddResponse)
2728 if !ok {
2829 return nil, endpoint.ErrBadCast
2930 }