/
endpoint.go
99 lines (84 loc) · 2.67 KB
/
endpoint.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
package dvd
import (
"context"
"time"
"github.com/go-kit/kit/circuitbreaker"
"github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/ratelimit"
"github.com/go-kit/kit/tracing/opentracing"
stdopentracing "github.com/opentracing/opentracing-go"
"github.com/sony/gobreaker"
"golang.org/x/time/rate"
)
type CreateDVDRequest struct {
Name string `json:"name"`
}
type CreateDVDResponse struct {
Err error `json:"error,omitempty"`
}
func (r CreateDVDResponse) error() error {
return r.Err
}
func makeCreateDVDEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(CreateDVDRequest)
err := s.CreateDVD(ctx, req.Name)
return CreateDVDResponse{Err: err}, nil
}
}
type DVDEndpoints struct {
CreateDVDEndpoint endpoint.Endpoint
RentDVDEndpoint endpoint.Endpoint
}
func (ep DVDEndpoints) CreateDVD(ctx context.Context, name string) error {
res, err := ep.CreateDVDEndpoint(ctx, CreateDVDRequest{Name: name})
if err != nil {
return err
}
response := res.(CreateDVDResponse)
return response.Err
}
type RentDVDRequest struct {
ID string `json:"id"`
}
type RentDVDResponse struct {
Err error `json:"error,omitempty"`
}
func (r RentDVDResponse) error() error {
return r.Err
}
func (ep DVDEndpoints) RentDVD(ctx context.Context, id string) error {
res, err := ep.RentDVDEndpoint(ctx, RentDVDRequest{ID: id})
if err != nil {
return err
}
response := res.(RentDVDResponse)
return response.Err
}
func makeRentDVDEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(RentDVDRequest)
err := s.RentDVD(ctx, req.ID)
return RentDVDResponse{Err: err}, nil
}
}
func NewDVDEndpoint(svc Service, ot stdopentracing.Tracer) DVDEndpoints {
var createDVDEndpoint endpoint.Endpoint
{
createDVDEndpoint = makeCreateDVDEndpoint(svc)
createDVDEndpoint = ratelimit.NewErroringLimiter(rate.NewLimiter(rate.Every(time.Second), 1))(createDVDEndpoint)
createDVDEndpoint = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{}))(createDVDEndpoint)
createDVDEndpoint = opentracing.TraceServer(ot, "create_dvd")(createDVDEndpoint)
}
var rentDVDEndpoint endpoint.Endpoint
{
rentDVDEndpoint = makeRentDVDEndpoint(svc)
rentDVDEndpoint = ratelimit.NewErroringLimiter(rate.NewLimiter(rate.Every(time.Second), 1))(rentDVDEndpoint)
rentDVDEndpoint = circuitbreaker.Gobreaker(gobreaker.NewCircuitBreaker(gobreaker.Settings{}))(rentDVDEndpoint)
rentDVDEndpoint = opentracing.TraceClient(ot, "rent_dvd")(rentDVDEndpoint)
}
return DVDEndpoints{
CreateDVDEndpoint: createDVDEndpoint,
RentDVDEndpoint: rentDVDEndpoint,
}
}