/
subscriber.go
246 lines (199 loc) · 4.89 KB
/
subscriber.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package nats
import (
"context"
kitep "github.com/go-kit/kit/endpoint"
"github.com/go-kit/kit/transport"
kitn "github.com/go-kit/kit/transport/nats"
natn "github.com/nats-io/nats.go"
"github.com/pkg/errors"
"github.com/unbxd/go-base/kit/endpoint"
"github.com/unbxd/go-base/utils/log"
)
type (
// Decoder decodes the message received on NATS and converts into business entity
Decoder func(context.Context, *natn.Msg) (request interface{}, err error)
// ResponseHandler handles the endpoint response
ResponseHandler func(context.Context, string, *natn.Conn, interface{}) error
BeforeFunc func(context.Context, *natn.Msg) context.Context
AfterFunc func(context.Context, *natn.Conn) context.Context
ErrorEncoder kitn.ErrorEncoder
ErrorHandler interface{ transport.ErrorHandler }
// Subscriber is NATS subscription
subscriber struct {
*kitn.Subscriber
id string
subject string
qGroup string
end endpoint.Endpoint
dec Decoder
reshn ResponseHandler
befores []BeforeFunc
afters []AfterFunc
errorEnc ErrorEncoder
errorhn ErrorHandler
conn *natn.Conn
middlewares []endpoint.Middleware
subscription *natn.Subscription
options []kitn.SubscriberOption
}
// SubscriberOption provides set of options to modify a Subscriber
SubscriberOption func(*subscriber)
)
func (s *subscriber) Id() string {
return s.id
}
func (s *subscriber) Topic() string {
return s.subject
}
func (s *subscriber) Group() string {
return s.qGroup
}
func (s *subscriber) IsValid() bool {
return s.subscription != nil && s.subscription.IsValid()
}
func WithQGroupSubscriberOption(qGroup string) SubscriberOption {
return func(s *subscriber) {
s.qGroup = qGroup
}
}
func WithId(id string) SubscriberOption {
return func(s *subscriber) {
s.id = id
}
}
func WithSubjectSubscriberOption(sub string) SubscriberOption {
return func(s *subscriber) {
s.subject = sub
}
}
func WithEndpointSubscriberOption(end endpoint.Endpoint) SubscriberOption {
return func(s *subscriber) {
s.end = end
}
}
func WithDecoderSubscriberOption(fn Decoder) SubscriberOption {
return func(s *subscriber) {
s.dec = fn
}
}
func WithBeforeFuncsSubscriberOption(fns ...BeforeFunc) SubscriberOption {
return func(s *subscriber) {
s.befores = append(s.befores, fns...)
for _, fn := range fns {
s.options = append(
s.options,
kitn.SubscriberBefore(kitn.RequestFunc(fn)),
)
}
}
}
func WithAfterFuncsSubscriberOption(fns ...AfterFunc) SubscriberOption {
return func(s *subscriber) {
s.afters = append(s.afters, fns...)
for _, fn := range fns {
s.options = append(
s.options,
kitn.SubscriberAfter(kitn.SubscriberResponseFunc(fn)),
)
}
}
}
// HandlerWithEndpointMiddleware provides an ability to add a
// middleware of the base type
func WithEndpointMiddleware(m endpoint.Middleware) SubscriberOption {
return func(s *subscriber) {
s.middlewares = append(s.middlewares, m)
}
}
func WithErrorEncoderSubscriberOption(e ErrorEncoder) SubscriberOption {
return func(s *subscriber) {
s.errorEnc = e
s.options = append(
s.options,
kitn.SubscriberErrorEncoder(kitn.ErrorEncoder(e)),
)
}
}
func WithErrorhandlerSubscriberOption(e ErrorHandler) SubscriberOption {
return func(s *subscriber) {
s.errorhn = e
s.options = append(s.options, kitn.SubscriberErrorHandler(e))
}
}
func (s *subscriber) open() error {
var err error
if len(s.qGroup) > 0 {
s.subscription, err = s.conn.QueueSubscribe(
s.subject,
s.qGroup,
s.ServeMsg(s.conn),
)
} else {
s.subscription, err = s.conn.Subscribe(
s.subject,
s.ServeMsg(s.conn),
)
}
return err
}
func (s *subscriber) close() error {
return s.subscription.Drain()
}
func newSubscriber(
logger log.Logger,
con *natn.Conn,
options ...SubscriberOption,
) (*subscriber, error) {
s := subscriber{conn: con}
for _, o := range options {
o(&s)
}
if s.end == nil {
return nil, errors.Wrap(
ErrCreatingSubscriber, "missing endpoint",
)
}
if len(s.subject) == 0 {
return nil, errors.Wrap(
ErrCreatingSubscriber, "missing subject",
)
}
if s.dec == nil {
return nil, errors.Wrap(
ErrCreatingSubscriber, "missing decoder",
)
}
if s.reshn == nil {
s.reshn = NoOpResponseHandler
}
if s.errorEnc == nil {
WithErrorEncoderSubscriberOption(NoOpErrorEncoder)
}
if s.errorhn == nil {
WithErrorhandlerSubscriberOption(transport.NewLogErrorHandler(logger))
}
s.Subscriber = kitn.NewSubscriber(
kitep.Endpoint(
wrap(s.end, s.middlewares...),
),
kitn.DecodeRequestFunc(s.dec),
kitn.EncodeResponseFunc(s.reshn),
s.options...,
)
return &s, nil
}
func wrap(ep endpoint.Endpoint, mws ...endpoint.Middleware) endpoint.Endpoint {
newmw := endpoint.Chain(
noopMiddleware,
mws...,
)
return newmw(ep)
}
func noopMiddleware(next endpoint.Endpoint) endpoint.Endpoint {
return func(
ctx context.Context,
req interface{},
) (interface{}, error) {
return next(ctx, req)
}
}