-
Notifications
You must be signed in to change notification settings - Fork 1
/
endpoints.go
executable file
·107 lines (96 loc) · 4 KB
/
endpoints.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package endpoints
import (
"context"
"time"
"github.com/go-kit/kit/circuitbreaker"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
"github.com/go-kit/kit/ratelimit"
"github.com/go-kit/kit/tracing/opentracing"
"github.com/go-kit/kit/tracing/zipkin"
stdopentracing "github.com/opentracing/opentracing-go"
stdzipkin "github.com/openzipkin/zipkin-go"
"github.com/sony/gobreaker"
"golang.org/x/time/rate"
"github.com/cage1016/gokitconsul/pkg/addsvc/service"
)
// Endpoints collects all of the endpoints that compose the addsvc service. It's
// meant to be used as a helper struct, to collect all of the endpoints into a
// single parameter.
type Endpoints struct {
SumEndpoint endpoint.Endpoint `json:""`
ConcatEndpoint endpoint.Endpoint `json:""`
}
// New return a new instance of the endpoint that wraps the provided service.
func New(svc service.AddsvcService, logger log.Logger, duration metrics.Histogram, otTracer stdopentracing.Tracer, zipkinTracer *stdzipkin.Tracer) (ep Endpoints) {
var sumEndpoint endpoint.Endpoint
{
method := "sum"
sumEndpoint = MakeSumEndpoint(svc)
sumEndpoint = ratelimit.NewErroringLimiter(rate.NewLimiter(rate.Every(time.Second), 100))(sumEndpoint)
sumEndpoint = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{}))(sumEndpoint)
sumEndpoint = opentracing.TraceServer(otTracer, method)(sumEndpoint)
sumEndpoint = zipkin.TraceEndpoint(zipkinTracer, method)(sumEndpoint)
sumEndpoint = LoggingMiddleware(log.With(logger, "method", method))(sumEndpoint)
sumEndpoint = InstrumentingMiddleware(duration.With("method", method))(sumEndpoint)
ep.SumEndpoint = sumEndpoint
}
var concatEndpoint endpoint.Endpoint
{
method := "concat"
concatEndpoint = MakeConcatEndpoint(svc)
concatEndpoint = ratelimit.NewErroringLimiter(rate.NewLimiter(rate.Every(time.Second), 100))(concatEndpoint)
concatEndpoint = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{}))(concatEndpoint)
concatEndpoint = opentracing.TraceServer(otTracer, method)(concatEndpoint)
concatEndpoint = zipkin.TraceEndpoint(zipkinTracer, method)(concatEndpoint)
concatEndpoint = LoggingMiddleware(log.With(logger, "method", method))(concatEndpoint)
concatEndpoint = InstrumentingMiddleware(duration.With("method", method))(concatEndpoint)
ep.ConcatEndpoint = concatEndpoint
}
return ep
}
// MakeSumEndpoint returns an endpoint that invokes Sum on the service.
// Primarily useful in a server.
func MakeSumEndpoint(svc service.AddsvcService) (ep endpoint.Endpoint) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(SumRequest)
if err := req.validate(); err != nil {
return SumResponse{}, err
}
rs, err := svc.Sum(ctx, req.A, req.B)
return SumResponse{Rs: rs}, err
}
}
// Sum implements the service interface, so Endpoints may be used as a service.
// This is primarily useful in the context of a client library.
func (e Endpoints) Sum(ctx context.Context, a int64, b int64) (rs int64, err error) {
resp, err := e.SumEndpoint(ctx, SumRequest{A: a, B: b})
if err != nil {
return
}
response := resp.(SumResponse)
return response.Rs, nil
}
// MakeConcatEndpoint returns an endpoint that invokes Concat on the service.
// Primarily useful in a server.
func MakeConcatEndpoint(svc service.AddsvcService) (ep endpoint.Endpoint) {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(ConcatRequest)
if err := req.validate(); err != nil {
return ConcatResponse{}, err
}
rs, err := svc.Concat(ctx, req.A, req.B)
return ConcatResponse{Rs: rs}, err
}
}
// Concat implements the service interface, so Endpoints may be used as a service.
// This is primarily useful in the context of a client library.
func (e Endpoints) Concat(ctx context.Context, a string, b string) (rs string, err error) {
resp, err := e.ConcatEndpoint(ctx, ConcatRequest{A: a, B: b})
if err != nil {
return
}
response := resp.(ConcatResponse)
return response.Rs, nil
}