/
consumer_amqp.go
287 lines (232 loc) · 6.2 KB
/
consumer_amqp.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
package bridge
import (
"context"
"fmt"
"github.com/streadway/amqp"
"golang.org/x/sync/errgroup"
"strings"
"sync"
"time"
)
type Processor func(ctx context.Context, headers map[string]string, body []byte) error
type Queue struct {
Name string
Prefetch int
Parallelism int
FailureTimeout time.Duration
Processor Processor
}
type AMQPConsumer struct {
url string
queues []Queue
log logger
wg sync.WaitGroup
ctx context.Context
cancel func()
}
// NewAMQPConsumer constructs AMQP consumer and starts message processing routine
func NewAMQPConsumer(ctx context.Context, url string, queues []Queue, log logger) *AMQPConsumer {
ctx, cancel := context.WithCancel(ctx)
c := &AMQPConsumer{
url: url,
queues: queues,
log: log,
ctx: ctx,
cancel: cancel,
}
c.run()
return c
}
// Run message processing routine
func (c *AMQPConsumer) run() {
c.wg.Add(1)
b := &backOff{}
go func() {
defer c.wg.Done()
// re-connect loop: re-initialize connection to AMQP server in case an error occurs
for {
if err := c.serve(); err != nil {
c.log.Errorf("An error occurred while serving AMQP connection: %v", err)
}
if isStopping(c.ctx) {
return
}
t := b.Timeout()
c.log.Infof("Waiting %v before re-connect", t)
if isStoppingWithTimeout(c.ctx, t) {
return
}
}
}()
}
// Stop AMQP consumer and wait for all routines to gracefully finish
func (c *AMQPConsumer) Stop() {
c.cancel()
c.wg.Wait()
}
// Serve handles AMQP connection attempt. When this method returns all resources used for serving connection should be
// released: go routines stopped, connections closed etc.
func (c *AMQPConsumer) serve() error {
c.log.Infof("Connecting to AMQP server")
conn, err := amqp.Dial(c.url)
if err != nil {
return err
}
defer c.log.Infof("AMQP connection was closed")
defer conn.Close()
// create wait group for all individual queue consumers
wg := sync.WaitGroup{}
// wait for all individual queue consumers to stop before returning
defer wg.Wait()
// create context for current connection attempt
ctx, cancel := context.WithCancel(c.ctx)
for _, queue := range c.queues {
wg.Add(1)
go func(queue Queue) {
defer wg.Done()
b := &backOff{}
// consumer re-start loop: restarts consumer in case an error occurs
for {
if err := c.consume(ctx, queue, conn); err != nil {
c.log.Errorf("An error occurred while consuming messages from %v: %v", queue.Name, err)
}
if isStopping(ctx) {
return
}
t := b.Timeout()
c.log.Infof("Waiting %v before re-starting consumer for %v", t, queue.Name)
if isStoppingWithTimeout(ctx, t) {
return
}
}
}(queue)
}
// handle connection closing notification
closing := make(chan *amqp.Error)
conn.NotifyClose(closing)
select {
case err := <-closing:
cancel()
return err
case <-ctx.Done():
return nil
}
}
// Consume messages from individual queue. When this method returns all resources used by individual queue consumer
// should be released: go routines stopped, connections closed etc.
func (c *AMQPConsumer) consume(ctx context.Context, queue Queue, conn *amqp.Connection) error {
c.log.Infof("Starting consumer for queue %v", queue.Name)
ch, err := conn.Channel()
if err != nil {
return err
}
defer c.log.Infof("Consumer for queue %v has stopped", queue.Name)
defer ch.Close()
if err := ch.Qos(queue.Prefetch, 0, false); err != nil {
return err
}
dv, err := ch.Consume(queue.Name, "", false, false, false, false, amqp.Table{})
if err != nil {
return err
}
eg, ctx := errgroup.WithContext(ctx)
sem := make(chan struct{}, queue.Parallelism)
loop:
for {
select {
case <-ctx.Done():
break loop
case d, ok := <-dv:
if !ok {
break loop
}
sem <- struct{}{}
eg.Go(func() error {
defer func() {
<-sem
}()
logctx := map[string]interface{}{
"message_id": d.MessageId,
"delivery_tag": d.DeliveryTag,
}
c.log.Debug("Processing message", logctx)
err := queue.Processor(ctx, headers(d), d.Body)
switch err {
case nil: // 2xx
c.log.Debug("Message successfully processed", logctx)
if err := d.Ack(false); err != nil {
return err
}
case ErrProcessingError: // 4xx error
c.log.Debug(fmt.Sprintf("Message processed with error: %v", err), logctx)
if err := d.Reject(false); err != nil {
return err
}
case ErrProcessingFailed: // 5xx error
fallthrough
case ErrUnknownStatus: // status code is missing (could be 2xx, could be fatal error)
fallthrough
case ErrProcessorInternal: // could not perform request
fallthrough
default:
t := queue.FailureTimeout
c.log.Error(fmt.Sprintf("Message processing failed: %v. Waiting %v before putting message back to the queue.", err, t), logctx)
// wait a bit before putting message back to the queue
wait(ctx, t)
if err := d.Reject(true); err != nil {
return err
}
}
return nil
})
}
}
return eg.Wait()
}
func wait(ctx context.Context, duration time.Duration) {
select {
case <-time.After(duration):
case <-ctx.Done():
}
}
func isStopping(ctx context.Context) bool {
select {
case <-ctx.Done():
return true
default:
return false
}
}
func isStoppingWithTimeout(ctx context.Context, duration time.Duration) bool {
select {
case <-ctx.Done():
return true
case <-time.After(duration):
return false
}
}
func headers(d amqp.Delivery) map[string]string {
h := map[string]string{
"CONTENT_TYPE": d.ContentType,
"CONTENT_ENCODING": d.ContentEncoding,
"DELIVERY_MODE": fmt.Sprint(d.DeliveryMode),
"PRIORITY": fmt.Sprint(d.Priority),
"CORRELATION_ID": d.CorrelationId,
"REPLY_TO": d.ReplyTo,
"EXPIRATION": d.Expiration,
"MESSAGE_ID": d.MessageId,
"TIMESTAMP": fmt.Sprint(d.Timestamp),
"TYPE": d.Type,
"USER_ID": d.UserId,
"APP_ID": d.AppId,
"CONSUMER_TAG": d.ConsumerTag,
"DELIVERY_TAG": fmt.Sprint(d.DeliveryTag),
"REDELIVERED": fmt.Sprint(d.Redelivered),
"EXCHANGE": d.Exchange,
"ROUTING_KEY": d.RoutingKey,
}
for k, v := range d.Headers {
h["AMQP_"+strings.ToUpper(k)] = fmt.Sprint(v)
}
return h
}