-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsumer.go
457 lines (422 loc) · 10.6 KB
/
consumer.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
457
package harego
import (
"context"
"errors"
"fmt"
"reflect"
"runtime/debug"
"sync"
"time"
"github.com/go-logr/logr"
amqp "github.com/rabbitmq/amqp091-go"
)
// Consumer is a concurrent safe construct for consuming messages from queues.
// It creates multiple workers for safe communication. Zero value is not
// usable, therefore you should construct a usable Consumer by calling the
// NewConsumer constructor.
// nolint:govet // most likely not an issue, but cleaner this way.
type Consumer struct {
connector Connector
workers int
consumerName string
retryDelay time.Duration
publisher *Publisher // used for requeueing messages.
logger logr.Logger
mu sync.RWMutex
conn RabbitMQ
channel Channel
queue amqp.Queue
msgs <-chan amqp.Delivery // for cleaning up
// queue properties.
queueName string
routingKey string
exclusive bool
queueArgs amqp.Table
// exchange properties.
exchName string
exchType ExchangeType
durable bool
autoDelete bool
autoAck bool
internal bool
noWait bool
// message properties.
prefetchCount int
prefetchSize int
deliveryMode DeliveryMode
chBuff int
consumeCh chan amqp.Delivery // used for consuming messages and setup Consume loop.
once sync.Once
ctx context.Context // for turning off the Consumer.
cancel func()
closed bool
panicHandler PanicHandler
}
// NewConsumer returns a Consumer capable of publishing and consuming messages.
// The default Consumer config uses the "default" exchange of the "topic" type,
// both exchange and queues will be marked as "durable", messages will be
// persistent, and the consumer gets a random name. The connector value should
// provide a live connection. The connector value is used during reconnection
// process.
func NewConsumer(connector Connector, conf ...ConfigFunc) (*Consumer, error) {
cnf := defaultConfig()
for _, fn := range conf {
fn(cnf)
}
c := cnf.consumer()
if c.chBuff == 0 {
c.chBuff = 1
}
c.connector = connector
c.ctx, c.cancel = context.WithCancel(c.ctx)
if c.prefetchCount < c.workers {
c.prefetchCount = c.workers
}
err := c.validate()
if err != nil {
return nil, fmt.Errorf("validating configuration: %w", err)
}
c.conn, err = c.connector()
if err != nil {
return nil, fmt.Errorf("getting a connection to the broker: %w", err)
}
_, err = c.setupChannel()
if err != nil {
return nil, fmt.Errorf("setting up a channel: %w", err)
}
c.publisher, err = NewPublisher(connector, conf...)
if err != nil {
return nil, fmt.Errorf("setting up requeue: %w", err)
}
err = c.setupQueue()
if err != nil {
return nil, fmt.Errorf("setting up a queue: %w", err)
}
c.registerReconnect(c.ctx)
c.logger = c.logger.
WithName("consume").
WithName(c.exchName).
WithName(c.queueName)
if c.panicHandler == nil {
c.panicHandler = func(msg *amqp.Delivery, r any) (AckType, time.Duration) {
err := fmt.Errorf("panic: %v", r)
c.logger.WithValues("message_id", msg.MessageId).Error(err, string(debug.Stack()))
return AckTypeRequeue, time.Second
}
}
return c, nil
}
// Consume is a bloking call that passes each message to the handler and stops
// handling messages when the context is done. If the handler returns false,
// the message is returned back to the queue. If the context is cancelled, the
// Consumer remains operational but no messages will be deliverd to this
// handler. Consume returns an error if you don't specify a queue name.
func (c *Consumer) Consume(ctx context.Context, handler HandlerFunc) error {
if c.closed {
return ErrClosed
}
if handler == nil {
return ErrNilHnadler
}
c.mu.Lock()
c.ctx, c.cancel = context.WithCancel(ctx)
c.consumeCh = make(chan amqp.Delivery, c.workers*c.chBuff)
err := c.setupConsumeCh()
if err != nil {
c.mu.Unlock()
return fmt.Errorf("setting up consume process: %w", err)
}
c.mu.Unlock()
go func() {
<-c.ctx.Done()
c.mu.Lock()
defer c.mu.Unlock()
close(c.consumeCh)
}()
var wg sync.WaitGroup
wg.Add(c.workers)
for i := 0; i < c.workers; i++ {
go func() {
defer wg.Done()
c.consumeLoop(handler)
}()
}
wg.Wait()
return c.ctx.Err()
}
func (c *Consumer) logErr(err error, msg, section string) {
if err != nil {
c.logger.Error(err, msg, "type", "consumer", "section", section)
}
}
func (c *Consumer) consumeLoop(handler HandlerFunc) {
h := func(msg *amqp.Delivery) (a AckType, delay time.Duration) {
defer func() {
if r := recover(); r != nil {
a, delay = c.panicHandler(msg, r)
}
}()
return handler(msg)
}
for msg := range c.consumeCh {
msg := msg
a, delay := h(&msg)
switch a {
case AckTypeAck:
time.Sleep(delay)
c.logErr(msg.Ack(false), "Ack message", "ack")
case AckTypeNack:
time.Sleep(delay)
c.logErr(msg.Nack(false, true), "Nack message", "nack")
case AckTypeReject:
time.Sleep(delay)
c.logErr(msg.Reject(false), "Reject message", "reject")
case AckTypeRequeue:
time.Sleep(delay)
err := c.publisher.Publish(&amqp.Publishing{
Body: msg.Body,
Headers: msg.Headers,
ContentType: msg.ContentType,
ContentEncoding: msg.ContentEncoding,
DeliveryMode: msg.DeliveryMode,
Priority: msg.Priority,
CorrelationId: msg.CorrelationId,
ReplyTo: msg.ReplyTo,
Expiration: msg.Expiration,
MessageId: msg.MessageId,
Timestamp: msg.Timestamp,
Type: msg.Type,
UserId: msg.UserId,
AppId: msg.AppId,
})
if err != nil {
c.logErr(msg.Nack(false, true), "Requeue message", "publish")
continue
}
c.logErr(msg.Ack(false), "Ack message", "ack")
}
}
}
// Close closes the channel and the connection. A closed Consumer is not
// usable.
// nolint:dupl // They are quite different.
func (c *Consumer) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
if c.closed {
return ErrClosed
}
c.once.Do(func() {
c.closed = true
c.cancel()
})
var err error
if c.channel != nil && !reflect.ValueOf(c.channel).IsNil() {
er := c.channel.Close()
if er != nil {
err = fmt.Errorf("closing channel: %w", er)
}
c.channel = nil
}
if c.conn != nil {
er := c.conn.Close()
if er != nil {
er = fmt.Errorf("closing connection: %w", er)
err = errors.Join(err, er)
}
c.conn = nil
}
return err
}
func (c *Consumer) validate() error {
if c.connector == nil {
return fmt.Errorf("empty connection function (Connector): %w", ErrInput)
}
if c.workers < 1 {
return fmt.Errorf("not enough workers: %d: %w", c.workers, ErrInput)
}
if c.consumerName == "" {
return fmt.Errorf("empty consumer name: %w", ErrInput)
}
if c.queueName == "" {
return fmt.Errorf("empty queue name: %w", ErrInput)
}
if c.exchName == "" {
return fmt.Errorf("empty exchange name: %w", ErrInput)
}
if c.prefetchCount < 1 {
return fmt.Errorf("not enough prefetch count: %d: %w", c.prefetchCount, ErrInput)
}
if c.prefetchSize < 0 {
return fmt.Errorf("not enough prefetch size: %d: %w", c.prefetchSize, ErrInput)
}
if !c.deliveryMode.IsValid() {
return fmt.Errorf("delivery mode: %q: %w", c.deliveryMode.String(), ErrInput)
}
return nil
}
func (c *Consumer) registerReconnect(ctx context.Context) {
c.mu.RLock()
if c.closed {
c.mu.RUnlock()
return
}
c.mu.RUnlock()
ch := c.channel.NotifyClose(make(chan *amqp.Error, 2))
go func() {
select {
case <-ctx.Done():
return
case err := <-ch:
c.logger.Info("closed consumer", "err", err)
c.mu.Lock()
if c.closed {
c.mu.Unlock()
return
}
if c.msgs != nil {
// We should clean up the channel otherwise it will block on other
// channels reading from the same connection.
for range c.msgs { //nolint:revive // this is on purpose.
}
}
if c.channel != nil {
c.logErr(c.channel.Close(), "Closing channel", "channel")
c.channel = nil
}
if c.conn != nil {
c.logErr(c.conn.Close(), "Closing connection", "connection")
c.conn = nil
}
c.mu.Unlock()
c.keepConnecting()
go c.registerReconnect(ctx)
}
}()
}
func (c *Consumer) keepConnecting() {
// In each step we create a connection, we want to clean up if any of the
// consequent step fails.
type cleanup map[string]func() error
var cleanups []cleanup
for {
for _, cln := range cleanups {
for section, fn := range cln {
c.logErr(fn(), "Cleaning up", section)
}
}
cleanups = make([]cleanup, 0, 2)
time.Sleep(c.retryDelay)
c.mu.RLock()
if c.closed {
c.mu.RUnlock()
return
}
c.mu.RUnlock()
cl, err := c.dial()
if err != nil {
c.logger.V(1).Info("dial up", "err", err)
continue
}
cleanups = append(cleanups, cleanup{"dial": cl})
cl, err = c.setupChannel()
if err != nil {
c.logger.V(1).Info("setting up a channel", "err", err)
continue
}
cleanups = append(cleanups, cleanup{"channel": cl})
err = c.setupQueue()
if err != nil {
c.logger.V(1).Info("setting up the queue", "err", err)
continue
}
err = c.setupConsumeCh()
if err != nil {
c.logger.V(1).Info("setting up the consumer channel", "err", err)
continue
}
c.logger.Info("Reconnected consumer")
return
}
}
func (c *Consumer) dial() (func() error, error) {
// already reconnected
if c.conn != nil {
return func() error { return nil }, nil
}
conn, err := c.connector()
if err != nil {
return nil, fmt.Errorf("getting a connection to the broker: %w", err)
}
c.conn = conn
return conn.Close, nil
}
func (c *Consumer) setupChannel() (func() error, error) {
var err error
c.channel, err = c.conn.Channel()
if err != nil {
return nil, fmt.Errorf("creating channel: %w", err)
}
// to make sure rabbitmq is fair on workers.
err = c.channel.Qos(c.prefetchCount, c.prefetchSize, true)
if err != nil {
return nil, fmt.Errorf("setting Qos: %w", err)
}
return c.channel.Close, nil
}
func (c *Consumer) setupQueue() error {
var err error
c.queue, err = c.channel.QueueDeclare(
c.queueName,
c.durable,
c.autoDelete,
c.exclusive,
c.noWait,
c.queueArgs,
)
if err != nil {
return fmt.Errorf("declaring queue: %w", err)
}
err = c.channel.QueueBind(
c.queueName,
c.routingKey,
c.exchName,
c.noWait,
nil,
)
if err != nil {
return fmt.Errorf("binding queue: %w", err)
}
return nil
}
func (c *Consumer) setupConsumeCh() error {
if c.consumeCh == nil {
return nil
}
var err error
c.msgs, err = c.channel.Consume(
c.queueName,
c.consumerName,
c.autoAck,
c.exclusive,
false,
c.noWait,
nil,
)
if err != nil {
return fmt.Errorf("getting consume channel: %w", err)
}
go func() {
for msg := range c.msgs {
select {
case <-c.ctx.Done():
return
default:
}
c.mu.Lock()
c.consumeCh <- msg
c.mu.Unlock()
}
}()
return nil
}