-
Notifications
You must be signed in to change notification settings - Fork 19
/
mq.go
456 lines (361 loc) · 11.2 KB
/
mq.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Package mq provides an ability to integrate with message broker via AMQP in a declarative way.
package mq
import (
"fmt"
"net"
"sync"
"sync/atomic"
"time"
"github.com/NeowayLabs/wabbit"
"github.com/NeowayLabs/wabbit/amqp"
"github.com/NeowayLabs/wabbit/amqptest"
"github.com/NeowayLabs/wabbit/utils"
amqpDriver "github.com/rabbitmq/amqp091-go"
)
const (
// Describes states during reconnect.
statusReadyForReconnect int32 = 0
statusReconnecting int32 = 1
ConnectionStateDisconnected ConnectionState = 1
ConnectionStateConnected ConnectionState = 2
ConnectionStateConnecting ConnectionState = 3
)
// Used for creating connection to the fake AMQP server for tests.
var brokerIsMocked bool
type conn interface {
Channel() (wabbit.Channel, error)
Close() error
NotifyClose(chan wabbit.Error) chan wabbit.Error
}
type ConnectionState uint8
type MessageQueue struct {
channel wabbit.Channel
config Config
connection conn
errorChannel chan error
internalErrorChannel chan error
consumers *consumersRegistry
producers *producersRegistry
reconnectStatus int32 // Defines whether client is trying to reconnect or not.
cluster struct {
sync.Once
currentNode int32
}
state *int32
}
// New initializes AMQP connection to the message broker
// and returns adapter that provides an ability
// to get configured consumers and producers, read occurred errors and shutdown all workers.
func New(config Config) (*MessageQueue, error) {
config.normalize()
mq := &MessageQueue{
config: config,
errorChannel: make(chan error),
internalErrorChannel: make(chan error),
consumers: newConsumersRegistry(len(config.Consumers)),
producers: newProducersRegistry(len(config.Producers)),
state: new(int32),
}
atomic.StoreInt32(mq.state, int32(ConnectionStateDisconnected))
if err := mq.connect(); err != nil {
return nil, err
}
go mq.errorHandler()
return mq, mq.initialSetup()
}
// SetConsumerHandler allows you to set handler callback without getting consumer.
// Returns false if consumer wasn't found.
// Can be called once for each consumer.
func (mq *MessageQueue) SetConsumerHandler(name string, handler ConsumerHandler) error {
consumer, err := mq.Consumer(name)
if err != nil {
return err
}
consumer.Consume(handler)
return nil
}
// Consumer returns a consumer by its name or error if consumer wasn't found.
func (mq *MessageQueue) Consumer(name string) (Consumer, error) {
consumer, ok := mq.consumers.Get(name)
if !ok {
err := fmt.Errorf("consumer '%s' is not registered. Check your configuration", name)
return nil, err
}
return consumer, nil
}
// AsyncProducer returns an async producer by its name or error if producer wasn't found.
// Should be used in most cases.
func (mq *MessageQueue) AsyncProducer(name string) (AsyncProducer, error) {
item, exists := mq.producers.Get(name)
producer, asserted := item.(*asyncProducer)
if !exists || !asserted {
return nil, fmt.Errorf("producer '%s' is not registered. Check your configuration", name)
}
return producer, nil
}
// SyncProducer returns a sync producer by its name or error if producer wasn't found.
func (mq *MessageQueue) SyncProducer(name string) (SyncProducer, error) {
item, exists := mq.producers.Get(name)
producer, asserted := item.(*syncProducer)
if !exists || !asserted {
return nil, fmt.Errorf("producer '%s' is not registered. Check your configuration", name)
}
return producer, nil
}
// Error returns channel with all occurred errors.
// Errors from sync producer won't be accessible. Get them directly from producer.
func (mq *MessageQueue) Error() <-chan error {
return mq.errorChannel
}
// Close stops all consumers and producers and closes connections to the broker.
func (mq *MessageQueue) Close() {
mq.stopProducersAndConsumers()
if mq.channel != nil {
_ = mq.channel.Close()
}
if mq.connection != nil {
_ = mq.connection.Close()
}
}
// ConnectionState shows connection state.
func (mq *MessageQueue) ConnectionState() ConnectionState {
return ConnectionState(atomic.LoadInt32(mq.state))
}
func (mq *MessageQueue) connect() error {
atomic.StoreInt32(mq.state, int32(ConnectionStateConnecting))
connection, err := mq.createConnection()
if err != nil {
atomic.StoreInt32(mq.state, int32(ConnectionStateDisconnected))
return err
}
channel, err := connection.Channel()
if err != nil {
_ = connection.Close()
atomic.StoreInt32(mq.state, int32(ConnectionStateDisconnected))
return err
}
mq.connection = connection
mq.channel = channel
go mq.handleCloseEvent()
atomic.StoreInt32(mq.state, int32(ConnectionStateConnected))
return nil
}
func (mq *MessageQueue) createConnection() (conn, error) {
mq.cluster.Do(func() { atomic.StoreInt32(&mq.cluster.currentNode, -1) })
atomic.AddInt32(&mq.cluster.currentNode, 1)
if int(mq.cluster.currentNode) >= len(mq.config.dsnList) {
atomic.StoreInt32(&mq.cluster.currentNode, 0)
}
dsn := mq.config.dsnList[mq.cluster.currentNode]
if brokerIsMocked || mq.config.TestMode {
return amqptest.Dial(dsn)
}
return amqp.Dial(dsn)
}
// Register close handler.
// To get more details visit https://godoc.org/github.com/streadway/amqp#Connection.NotifyClose.
func (mq *MessageQueue) handleCloseEvent() {
err := <-mq.connection.NotifyClose(make(chan wabbit.Error))
if err != nil {
mq.internalErrorChannel <- err
}
atomic.StoreInt32(mq.state, int32(ConnectionStateDisconnected))
}
func (mq *MessageQueue) errorHandler() {
for err := range mq.internalErrorChannel {
select {
case mq.errorChannel <- err: // Proxies errors to the user.
default: // For those clients who don't read errors.
}
mq.processError(err)
}
}
func (mq *MessageQueue) processError(err error) {
switch err.(type) {
case *net.OpError:
go mq.reconnect()
case *utils.Error: // Broken connection. Used in tests.
go mq.reconnect()
case *amqpDriver.Error:
rmqErr, _ := err.(*amqpDriver.Error)
if rmqErr.Server == false { // For example channel was closed.
go mq.reconnect()
}
default:
// Wabbit error. Means that server is down.
// Used in tests.
if err.Error() == "Network unreachable" {
go mq.reconnect()
}
}
}
func (mq *MessageQueue) initialSetup() error {
if err := mq.setupExchanges(); err != nil {
return err
}
if err := mq.setupQueues(); err != nil {
return err
}
if err := mq.setupProducers(); err != nil {
return err
}
return mq.setupConsumers()
}
// Called after each reconnect to recreate non-durable queues and exchanges.
func (mq *MessageQueue) setupAfterReconnect() error {
if err := mq.setupExchanges(); err != nil {
return err
}
if err := mq.setupQueues(); err != nil {
return err
}
mq.producers.GoEach(func(producer internalProducer) {
if err := mq.reconnectProducer(producer); err != nil {
mq.internalErrorChannel <- err
}
})
mq.consumers.GoEach(func(consumer *consumer) {
if err := mq.reconnectConsumer(consumer); err != nil {
mq.internalErrorChannel <- err
}
})
return nil
}
func (mq *MessageQueue) setupExchanges() error {
for _, config := range mq.config.Exchanges {
if err := mq.declareExchange(config); err != nil {
return err
}
}
return nil
}
func (mq *MessageQueue) declareExchange(config ExchangeConfig) error {
return mq.channel.ExchangeDeclare(config.Name, config.Type, wabbit.Option(config.Options))
}
func (mq *MessageQueue) setupQueues() error {
for _, config := range mq.config.Queues {
if err := mq.declareQueue(config); err != nil {
return err
}
}
return nil
}
func (mq *MessageQueue) declareQueue(config QueueConfig) error {
if _, err := mq.channel.QueueDeclare(config.Name, wabbit.Option(config.Options)); err != nil {
return err
}
return mq.channel.QueueBind(config.Name, config.RoutingKey, config.Exchange, wabbit.Option(config.BindingOptions))
}
func (mq *MessageQueue) setupProducers() error {
for _, config := range mq.config.Producers {
if err := mq.registerProducer(config); err != nil {
return err
}
}
return nil
}
func (mq *MessageQueue) registerProducer(config ProducerConfig) error {
if _, ok := mq.producers.Get(config.Name); ok {
return fmt.Errorf(`producer with name "%s" is already registered`, config.Name)
}
channel, err := mq.connection.Channel()
if err != nil {
return err
}
producer := newInternalProducer(channel, mq.internalErrorChannel, config)
producer.init()
mq.producers.Set(config.Name, producer)
return nil
}
func (mq *MessageQueue) reconnectProducer(producer internalProducer) error {
channel, err := mq.connection.Channel()
if err != nil {
return err
}
producer.setChannel(channel)
producer.init()
return nil
}
func (mq *MessageQueue) setupConsumers() error {
for _, config := range mq.config.Consumers {
if err := mq.registerConsumer(config); err != nil {
return err
}
}
return nil
}
func (mq *MessageQueue) registerConsumer(config ConsumerConfig) error {
if _, ok := mq.consumers.Get(config.Name); ok {
return fmt.Errorf(`consumer with name "%s" is already registered`, config.Name)
}
// Consumer must have at least one worker.
if config.Workers < 1 {
config.Workers = 1
}
consumer := newConsumer(config) // We need to save a whole config for reconnect.
consumer.prefetchCount = config.PrefetchCount
consumer.prefetchSize = config.PrefetchSize
for i := 0; i < config.Workers; i++ {
worker := newWorker(mq.internalErrorChannel)
if err := mq.initializeConsumersWorker(consumer, worker); err != nil {
return err
}
consumer.workers[i] = worker
}
mq.consumers.Set(config.Name, consumer) // Workers will start after consumer.Consume method call.
return nil
}
func (mq *MessageQueue) reconnectConsumer(consumer *consumer) error {
for _, worker := range consumer.workers {
if err := mq.initializeConsumersWorker(consumer, worker); err != nil {
return err
}
}
consumer.consume(consumer.handler)
return nil
}
func (mq *MessageQueue) initializeConsumersWorker(consumer *consumer, worker *worker) error {
channel, err := mq.connection.Channel()
if err != nil {
return err
}
if err := channel.Qos(consumer.prefetchCount, consumer.prefetchSize, false); err != nil {
return err
}
deliveries, err := channel.Consume(consumer.queue, "", consumer.options)
if err != nil {
return err
}
worker.setChannel(channel)
worker.deliveries = deliveries
return nil
}
// Reconnect stops current producers and consumers,
// recreates connection to the rabbit and than runs producers and consumers.
func (mq *MessageQueue) reconnect() {
startedReconnect := atomic.CompareAndSwapInt32(&mq.reconnectStatus, statusReadyForReconnect, statusReconnecting)
// There is no need to start a new reconnect if the previous one is not finished yet.
if !startedReconnect {
return
}
defer func() {
atomic.StoreInt32(&mq.reconnectStatus, statusReadyForReconnect)
}()
time.Sleep(mq.config.ReconnectDelay) // TODO Add incremental sleep.
mq.stopProducersAndConsumers()
if err := mq.connect(); err != nil {
mq.internalErrorChannel <- err
return
}
if err := mq.setupAfterReconnect(); err != nil {
mq.internalErrorChannel <- err
}
}
func (mq *MessageQueue) stopProducersAndConsumers() {
mq.producers.GoEach(func(producer internalProducer) {
producer.Stop()
})
mq.consumers.GoEach(func(consumer *consumer) {
consumer.Stop()
})
}