reqrep
Peter Bourgon
8 years ago
2 | 2 |
import (
|
3 | 3 |
"golang.org/x/net/context"
|
4 | 4 |
|
|
5 |
"github.com/go-kit/kit/addsvc/reqrep"
|
5 | 6 |
"github.com/go-kit/kit/endpoint"
|
6 | 7 |
"github.com/go-kit/kit/log"
|
7 | 8 |
)
|
|
18 | 19 |
// service.
|
19 | 20 |
func proxyAdd(e endpoint.Endpoint, logger log.Logger) Add {
|
20 | 21 |
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})
|
22 | 23 |
if err != nil {
|
23 | 24 |
logger.Log("err", err)
|
24 | 25 |
return 0
|
25 | 26 |
}
|
26 | |
addResp, ok := resp.(*addResponse)
|
|
27 |
addResp, ok := resp.(*reqrep.AddResponse)
|
27 | 28 |
if !ok {
|
28 | 29 |
logger.Log("err", endpoint.ErrBadCast)
|
29 | 30 |
return 0
|
2 | 2 |
import (
|
3 | 3 |
"golang.org/x/net/context"
|
4 | 4 |
|
|
5 |
"github.com/go-kit/kit/addsvc/reqrep"
|
5 | 6 |
"github.com/go-kit/kit/endpoint"
|
6 | 7 |
)
|
7 | 8 |
|
|
17 | 18 |
return nil, endpoint.ErrContextCanceled
|
18 | 19 |
}
|
19 | 20 |
|
20 | |
addReq, ok := request.(*addRequest)
|
|
21 |
addReq, ok := request.(*reqrep.AddRequest)
|
21 | 22 |
if !ok {
|
22 | 23 |
return nil, endpoint.ErrBadCast
|
23 | 24 |
}
|
24 | 25 |
|
25 | 26 |
v := a(ctx, addReq.A, addReq.B)
|
26 | 27 |
|
27 | |
return addResponse{V: v}, nil
|
|
28 |
return &reqrep.AddResponse{V: v}, nil
|
28 | 29 |
}
|
29 | 30 |
}
|
5 | 5 |
"golang.org/x/net/context"
|
6 | 6 |
|
7 | 7 |
"github.com/go-kit/kit/addsvc/pb"
|
|
8 |
"github.com/go-kit/kit/addsvc/reqrep"
|
8 | 9 |
"github.com/go-kit/kit/endpoint"
|
9 | 10 |
"github.com/go-kit/kit/metrics"
|
10 | 11 |
)
|
|
19 | 20 |
// way to manipulate the RPC context, like headers for HTTP. So we don't have
|
20 | 21 |
// a way to transport e.g. Zipkin IDs with the request. TODO.
|
21 | 22 |
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}
|
23 | 24 |
r, err := b.Endpoint(ctx, addReq)
|
24 | 25 |
if err != nil {
|
25 | 26 |
return nil, err
|
26 | 27 |
}
|
27 | 28 |
|
28 | |
resp, ok := r.(*addResponse)
|
|
29 |
resp, ok := r.(*reqrep.AddResponse)
|
29 | 30 |
if !ok {
|
30 | 31 |
return nil, endpoint.ErrBadCast
|
31 | 32 |
}
|
23 | 23 |
|
24 | 24 |
thriftadd "github.com/go-kit/kit/addsvc/_thrift/gen-go/add"
|
25 | 25 |
"github.com/go-kit/kit/addsvc/pb"
|
|
26 |
"github.com/go-kit/kit/addsvc/reqrep"
|
26 | 27 |
"github.com/go-kit/kit/endpoint"
|
27 | 28 |
kitlog "github.com/go-kit/kit/log"
|
28 | 29 |
"github.com/go-kit/kit/metrics"
|
|
110 | 111 |
var a Add = pureAdd
|
111 | 112 |
if *proxyHTTPAddr != "" {
|
112 | 113 |
codec := jsoncodec.New()
|
113 | |
makeResponse := func() interface{} { return &addResponse{} }
|
|
114 |
makeResponse := func() interface{} { return &reqrep.AddResponse{} }
|
114 | 115 |
|
115 | 116 |
var e endpoint.Endpoint
|
116 | 117 |
e = httptransport.NewClient(*proxyHTTPAddr, codec, makeResponse, httptransport.ClientBefore(zipkin.ToRequest(zipkinSpanFunc)))
|
|
148 | 149 |
field := metrics.Field{Key: "transport", Value: "http"}
|
149 | 150 |
before := httptransport.BindingBefore(zipkin.ToContext(zipkinSpanFunc))
|
150 | 151 |
after := httptransport.BindingAfter(httptransport.SetContentType("application/json"))
|
151 | |
makeRequest := func() interface{} { return &addRequest{} }
|
|
152 |
makeRequest := func() interface{} { return &reqrep.AddRequest{} }
|
152 | 153 |
|
153 | 154 |
var handler http.Handler
|
154 | 155 |
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 | |
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 | |
}
|
5 | 5 |
"golang.org/x/net/context"
|
6 | 6 |
|
7 | 7 |
thriftadd "github.com/go-kit/kit/addsvc/_thrift/gen-go/add"
|
|
8 |
"github.com/go-kit/kit/addsvc/reqrep"
|
8 | 9 |
"github.com/go-kit/kit/endpoint"
|
9 | 10 |
"github.com/go-kit/kit/metrics"
|
10 | 11 |
)
|
|
18 | 19 |
|
19 | 20 |
// Add implements Thrift's AddService interface.
|
20 | 21 |
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})
|
22 | 23 |
if err != nil {
|
23 | 24 |
return nil, err
|
24 | 25 |
}
|
25 | 26 |
|
26 | |
resp, ok := r.(*addResponse)
|
|
27 |
resp, ok := r.(*reqrep.AddResponse)
|
27 | 28 |
if !ok {
|
28 | 29 |
return nil, endpoint.ErrBadCast
|
29 | 30 |
}
|