Package list golang-github-go-kit-kit / d61f875
package {client,server} => package endpoint - The same definition applies to both "sides" - Update addsvc to reflect the type changes - Update zipkin tests to reflect the type changes Peter Bourgon 6 years ago
12 changed file(s) with 87 addition(s) and 111 deletion(s). Raw diff Collapse all Expand all
22 import (
33 "golang.org/x/net/context"
44
5 "github.com/go-kit/kit/client"
5 "github.com/go-kit/kit/endpoint"
66 "github.com/go-kit/kit/log"
77 )
88
1515 func pureAdd(_ context.Context, a, b int64) int64 { return a + b }
1616
1717 // proxyAdd implements Add by invoking a remote Add service.
18 func proxyAdd(e client.Endpoint) Add {
18 func proxyAdd(e endpoint.Endpoint) Add {
1919 return func(ctx context.Context, a, b int64) int64 {
2020 resp, err := e(ctx, &addRequest{a, b})
2121 if err != nil {
2424 }
2525 addResp, ok := resp.(*addResponse)
2626 if !ok {
27 log.DefaultLogger.Log("err", client.ErrBadCast)
27 log.DefaultLogger.Log("err", endpoint.ErrBadCast)
2828 return 0
2929 }
3030 return addResp.V
22 import (
33 "golang.org/x/net/context"
44
5 "github.com/go-kit/kit/server"
5 "github.com/go-kit/kit/endpoint"
66 )
77
8 // makeEndpoint returns a server.Endpoint wrapping the passed Add. If Add were
9 // an interface with multiple methods, we'd need individual endpoints for
10 // each.
8 // makeEndpoint returns an endpoint wrapping the passed Add. If Add were an
9 // interface with multiple methods, we'd need individual endpoints for each.
1110 //
1211 // This function is just boiler-plate; in theory, it could be generated.
13 func makeEndpoint(a Add) server.Endpoint {
12 func makeEndpoint(a Add) endpoint.Endpoint {
1413 return func(ctx context.Context, request interface{}) (interface{}, error) {
1514 select {
15 default:
1616 case <-ctx.Done():
17 return nil, server.ErrContextCanceled
18 default:
17 return nil, endpoint.ErrContextCanceled
1918 }
2019
2120 addReq, ok := request.(*addRequest)
2221 if !ok {
23 return nil, server.ErrBadCast
22 return nil, endpoint.ErrBadCast
2423 }
2524
2625 v := a(ctx, addReq.A, addReq.B)
55 "golang.org/x/net/context"
66
77 "github.com/go-kit/kit/addsvc/pb"
8 "github.com/go-kit/kit/endpoint"
89 "github.com/go-kit/kit/metrics"
9 "github.com/go-kit/kit/server"
1010 )
1111
1212 // A binding wraps an Endpoint so that it's usable by a transport. grpcBinding
1313 // makes an Endpoint usable over gRPC.
14 type grpcBinding struct{ server.Endpoint }
14 type grpcBinding struct{ endpoint.Endpoint }
1515
1616 // Add implements the proto3 AddServer by forwarding to the wrapped Endpoint.
1717 //
2727
2828 resp, ok := r.(*addResponse)
2929 if !ok {
30 return nil, server.ErrBadCast
30 return nil, endpoint.ErrBadCast
3131 }
3232
3333 return &pb.AddReply{
2424
2525 thriftadd "github.com/go-kit/kit/addsvc/_thrift/gen-go/add"
2626 "github.com/go-kit/kit/addsvc/pb"
27 "github.com/go-kit/kit/client"
27 "github.com/go-kit/kit/endpoint"
2828 kitlog "github.com/go-kit/kit/log"
2929 "github.com/go-kit/kit/metrics"
3030 "github.com/go-kit/kit/metrics/expvar"
3131 "github.com/go-kit/kit/metrics/prometheus"
3232 "github.com/go-kit/kit/metrics/statsd"
33 "github.com/go-kit/kit/server"
3433 "github.com/go-kit/kit/tracing/zipkin"
3534 jsoncodec "github.com/go-kit/kit/transport/codec/json"
3635 httptransport "github.com/go-kit/kit/transport/http"
9695 codec := jsoncodec.New()
9796 makeResponse := func() interface{} { return &addResponse{} }
9897
99 var e client.Endpoint
98 var e endpoint.Endpoint
10099 e = httptransport.NewClient(*proxyHTTPAddr, codec, makeResponse, httptransport.ClientBefore(zipkin.ToRequest(zipkinSpanFunc)))
101100 e = zipkin.AnnotateClient(zipkinSpanFunc, zipkinCollector)(e)
102101
104103 }
105104 a = logging(logger)(a)
106105
107 // `package server` domain
108 var e server.Endpoint
106 // Server domain
107 var e endpoint.Endpoint
109108 e = makeEndpoint(a)
110109 e = zipkin.AnnotateServer(zipkinSpanFunc, zipkinCollector)(e)
111110
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/endpoint"
89 "github.com/go-kit/kit/metrics"
9 "github.com/go-kit/kit/server"
1010 )
1111
1212 // A binding wraps an Endpoint so that it's usable by a transport.
1313 // thriftBinding makes an Endpoint usable over Thrift.
1414 type thriftBinding struct {
1515 context.Context
16 server.Endpoint
16 endpoint.Endpoint
1717 }
1818
1919 // Add implements Thrift's AddService interface.
2525
2626 resp, ok := r.(*addResponse)
2727 if !ok {
28 return nil, server.ErrBadCast
28 return nil, endpoint.ErrBadCast
2929 }
3030
3131 return &thriftadd.AddReply{Value: resp.V}, nil
+0
-17
client/client.go less more
0 package client
1
2 import (
3 "errors"
4
5 "golang.org/x/net/context"
6 )
7
8 // Endpoint is the fundamental building block of package client.
9 // It represents a single RPC method on a remote service.
10 type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
11
12 // Middleware is a chainable behavior modifier.
13 type Middleware func(Endpoint) Endpoint
14
15 // ErrBadCast indicates a type error during decoding or encoding.
16 var ErrBadCast = errors.New("bad cast")
0 package endpoint
1
2 import (
3 "errors"
4
5 "golang.org/x/net/context"
6 )
7
8 // Endpoint is the fundamental building block of packages server and client.
9 // It represents a single RPC method.
10 type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
11
12 // Middleware is a chainable behavior modifier.
13 type Middleware func(Endpoint) Endpoint
14
15 // ErrBadCast indicates an unexpected concrete request or response struct was
16 // received from an endpoint.
17 var ErrBadCast = errors.New("bad cast")
18
19 // ContextCanceled indicates the request context was canceled.
20 var ErrContextCanceled = errors.New("context canceled")
+0
-21
server/server.go less more
0 package server
1
2 import (
3 "errors"
4
5 "golang.org/x/net/context"
6 )
7
8 // Endpoint is the fundamental building block of package server.
9 // It represents a single RPC method.
10 type Endpoint func(ctx context.Context, request interface{}) (response interface{}, err error)
11
12 // Middleware is a chainable behavior modifier.
13 type Middleware func(Endpoint) Endpoint
14
15 // ErrBadCast indicates a type error during decoding or encoding.
16 var ErrBadCast = errors.New("bad cast")
17
18 // ErrContextCanceled indicates a controlling context was canceled before the
19 // request could be served.
20 var ErrContextCanceled = errors.New("context was canceled")
44 "net/http"
55 "strconv"
66
7 "github.com/go-kit/kit/client"
7 "github.com/go-kit/kit/endpoint"
88 "github.com/go-kit/kit/log"
9 "github.com/go-kit/kit/server"
109
1110 "golang.org/x/net/context"
1211 )
4948 // context, adds server-receive and server-send annotations at the boundaries,
5049 // and submits the span to the collector. If no span is found in the context,
5150 // a new span is generated and inserted.
52 func AnnotateServer(newSpan NewSpanFunc, c Collector) server.Middleware {
53 return func(e server.Endpoint) server.Endpoint {
51 func AnnotateServer(newSpan NewSpanFunc, c Collector) endpoint.Middleware {
52 return func(e endpoint.Endpoint) endpoint.Endpoint {
5453 return func(ctx context.Context, request interface{}) (interface{}, error) {
5554 span, ok := fromContext(ctx)
5655 if !ok {
6463 }
6564 }
6665
67 // AnnotateClient returns a client.Middleware that extracts a parent span from
68 // the context, produces a client (child) span from it, adds client-send and
66 // AnnotateClient returns a middleware that extracts a parent span from the
67 // context, produces a client (child) span from it, adds client-send and
6968 // client-receive annotations at the boundaries, and submits the span to the
7069 // collector. If no span is found in the context, a new span is generated and
7170 // inserted.
72 func AnnotateClient(newSpan NewSpanFunc, c Collector) client.Middleware {
73 return func(e client.Endpoint) client.Endpoint {
71 func AnnotateClient(newSpan NewSpanFunc, c Collector) endpoint.Middleware {
72 return func(e endpoint.Endpoint) endpoint.Endpoint {
7473 return func(ctx context.Context, request interface{}) (interface{}, error) {
7574 var clientSpan *Span
7675 parentSpan, ok := fromContext(ctx)
00 package zipkin_test
11
22 import (
3 "fmt"
34 "net/http"
45 "reflect"
56 "runtime"
67 "strconv"
78 "strings"
8 "sync/atomic"
99 "testing"
1010
1111 "golang.org/x/net/context"
1212
13 "github.com/go-kit/kit/client"
14 "github.com/go-kit/kit/server"
13 "github.com/go-kit/kit/endpoint"
1514 "github.com/go-kit/kit/tracing/zipkin"
1615 )
1716
8685 }
8786
8887 func TestAnnotateServer(t *testing.T) {
88 if err := testAnnotate(zipkin.AnnotateServer, zipkin.ServerReceive, zipkin.ServerSend); err != nil {
89 t.Fatal(err)
90 }
91 }
92
93 func TestAnnotateClient(t *testing.T) {
94 if err := testAnnotate(zipkin.AnnotateClient, zipkin.ClientSend, zipkin.ClientReceive); err != nil {
95 t.Fatal(err)
96 }
97 }
98
99 func testAnnotate(
100 annotate func(newSpan zipkin.NewSpanFunc, c zipkin.Collector) endpoint.Middleware,
101 wantAnnotations ...string,
102 ) error {
89103 const (
90104 hostport = "1.2.3.4:1234"
91105 serviceName = "some-service"
92106 methodName = "some-method"
93107 )
94108
95 f := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName)
96 c := &countingCollector{}
109 newSpan := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName)
110 collector := &countingCollector{}
97111
98 var e server.Endpoint
112 var e endpoint.Endpoint
99113 e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
100 e = zipkin.AnnotateServer(f, c)(e)
114 e = annotate(newSpan, collector)(e)
101115
102 if want, have := int32(0), atomic.LoadInt32(&(c.int32)); want != have {
103 t.Errorf("want %d, have %d", want, have)
116 if want, have := 0, len(collector.annotations); want != have {
117 return fmt.Errorf("pre-invocation: want %d, have %d", want, have)
104118 }
105119 if _, err := e(context.Background(), struct{}{}); err != nil {
106 t.Fatal(err)
120 return fmt.Errorf("during invocation: %v", err)
107121 }
108 if want, have := int32(1), atomic.LoadInt32(&(c.int32)); want != have {
109 t.Errorf("want %d, have %d", want, have)
122 if want, have := wantAnnotations, collector.annotations; !reflect.DeepEqual(want, have) {
123 return fmt.Errorf("after invocation: want %v, have %v", want, have)
110124 }
125
126 return nil
111127 }
112128
113 func TestAnnotateClient(t *testing.T) {
114 const (
115 hostport = "192.168.1.100:53"
116 serviceName = "client-service"
117 methodName = "client-method"
118 )
129 type countingCollector struct{ annotations []string }
119130
120 f := zipkin.MakeNewSpanFunc(hostport, serviceName, methodName)
121 c := &countingCollector{}
122
123 var e client.Endpoint
124 e = func(context.Context, interface{}) (interface{}, error) { return struct{}{}, nil }
125 e = zipkin.AnnotateClient(f, c)(e)
126
127 if want, have := int32(0), atomic.LoadInt32(&(c.int32)); want != have {
128 t.Errorf("want %d, have %d", want, have)
131 func (c *countingCollector) Collect(s *zipkin.Span) error {
132 for _, annotation := range s.Encode().GetAnnotations() {
133 c.annotations = append(c.annotations, annotation.GetValue())
129134 }
130 if _, err := e(context.Background(), struct{}{}); err != nil {
131 t.Fatal(err)
132 }
133 if want, have := int32(1), atomic.LoadInt32(&(c.int32)); want != have {
134 t.Errorf("want %d, have %d", want, have)
135 }
135 return nil
136136 }
137
138 type countingCollector struct{ int32 }
139
140 func (c *countingCollector) Collect(*zipkin.Span) error { atomic.AddInt32(&(c.int32), 1); return nil }
44
55 "golang.org/x/net/context"
66
7 "github.com/go-kit/kit/server"
7 "github.com/go-kit/kit/endpoint"
88 "github.com/go-kit/kit/transport/codec"
99 )
1010
1212 context.Context
1313 makeRequest func() interface{}
1414 codec.Codec
15 server.Endpoint
15 endpoint.Endpoint
1616 before []BeforeFunc
1717 after []AfterFunc
1818 }
1919
2020 // NewBinding returns an HTTP handler that wraps the given endpoint.
21 func NewBinding(ctx context.Context, makeRequest func() interface{}, cdc codec.Codec, endpoint server.Endpoint, options ...BindingOption) http.Handler {
21 func NewBinding(ctx context.Context, makeRequest func() interface{}, cdc codec.Codec, e endpoint.Endpoint, options ...BindingOption) http.Handler {
2222 b := &binding{
2323 Context: ctx,
2424 makeRequest: makeRequest,
2525 Codec: cdc,
26 Endpoint: endpoint,
26 Endpoint: e,
2727 }
2828 for _, option := range options {
2929 option(b)
66
77 "golang.org/x/net/context"
88
9 "github.com/go-kit/kit/client"
9 "github.com/go-kit/kit/endpoint"
1010 "github.com/go-kit/kit/transport/codec"
1111 )
1212
2121
2222 // NewClient returns a client endpoint for a remote service. addr must be a
2323 // valid, parseable URL, including the scheme and path.
24 func NewClient(addr string, cdc codec.Codec, makeResponse func() interface{}, options ...ClientOption) client.Endpoint {
24 func NewClient(addr string, cdc codec.Codec, makeResponse func() interface{}, options ...ClientOption) endpoint.Endpoint {
2525 u, err := url.Parse(addr)
2626 if err != nil {
2727 panic(err)