-
Notifications
You must be signed in to change notification settings - Fork 69
/
amqp.go
296 lines (250 loc) · 6.68 KB
/
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
288
289
290
291
292
293
294
295
296
// Package amqp provides consumer implementation with included tracing capabilities.
package amqp
import (
"context"
"errors"
"fmt"
"net"
"time"
"github.com/beatlabs/patron/component/async"
"github.com/beatlabs/patron/correlation"
"github.com/beatlabs/patron/encoding"
patronErrors "github.com/beatlabs/patron/errors"
"github.com/beatlabs/patron/log"
"github.com/beatlabs/patron/trace"
"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/streadway/amqp"
)
const (
consumerComponent = "amqp-consumer"
)
var defaultCfg = amqp.Config{
Dial: func(network, addr string) (net.Conn, error) {
return net.DialTimeout(network, addr, 30*time.Second)
},
}
type message struct {
span opentracing.Span
ctx context.Context
del *amqp.Delivery
dec encoding.DecodeRawFunc
requeue bool
source string
}
func (m *message) Context() context.Context {
return m.ctx
}
func (m *message) Decode(v interface{}) error {
return m.dec(m.del.Body, v)
}
func (m *message) Ack() error {
err := m.del.Ack(false)
trace.SpanSuccess(m.span)
return err
}
func (m *message) Nack() error {
err := m.del.Nack(false, m.requeue)
trace.SpanError(m.span)
return err
}
// Source returns the queue's name where the message arrived.
func (m *message) Source() string {
return m.source
}
// Payload returns the message payload.
func (m *message) Payload() []byte {
return m.del.Body
}
// Exchange represents an AMQP exchange.
type Exchange struct {
name string
kind string
}
// NewExchange validates its input and creates a new Exchange.
// name should be a non empty string, kind should be one of: [direct, fanout, topic, headers].
func NewExchange(name, kind string) (*Exchange, error) {
if name == "" {
return nil, errors.New("AMQP Exchange name is required")
}
if kind == "" {
return nil, errors.New("AMQP Exchange type is required")
}
if kind != amqp.ExchangeDirect &&
kind != amqp.ExchangeFanout &&
kind != amqp.ExchangeTopic &&
kind != amqp.ExchangeHeaders {
return nil, fmt.Errorf("AMQP Exchange type is invalid, one of [%s, %s, %s, %s] is required",
amqp.ExchangeDirect,
amqp.ExchangeFanout,
amqp.ExchangeTopic,
amqp.ExchangeHeaders)
}
return &Exchange{name: name, kind: kind}, nil
}
// Factory of an AMQP consumer.
type Factory struct {
url string
queue string
exchange Exchange
bindings []string
oo []OptionFunc
}
// New constructor.
func New(url, queue string, exchange Exchange, oo ...OptionFunc) (*Factory, error) {
if url == "" {
return nil, errors.New("AMQP url is required")
}
if queue == "" {
return nil, errors.New("AMQP queue name is required")
}
return &Factory{url: url, queue: queue, exchange: exchange, bindings: []string{""}, oo: oo}, nil
}
// Create a new consumer.
func (f *Factory) Create() (async.Consumer, error) {
c := &consumer{
url: f.url,
queue: f.queue,
exchange: f.exchange,
requeue: true,
cfg: defaultCfg,
buffer: 1000,
traceTag: opentracing.Tag{Key: "queue", Value: f.queue},
}
for _, o := range f.oo {
err := o(c)
if err != nil {
return nil, err
}
}
return c, nil
}
type consumer struct {
url string
queue string
exchange Exchange
bindings []string
requeue bool
tag string
buffer int
traceTag opentracing.Tag
cfg amqp.Config
ch *amqp.Channel
conn *amqp.Connection
}
func (c *consumer) OutOfOrder() bool {
return true
}
// Consume starts of consuming a AMQP queue.
func (c *consumer) Consume(ctx context.Context) (<-chan async.Message, <-chan error, error) {
deliveries, err := c.consume()
if err != nil {
return nil, nil, fmt.Errorf("failed initialize consumer: %w", err)
}
chMsg := make(chan async.Message, c.buffer)
chErr := make(chan error, c.buffer)
go func() {
for {
select {
case <-ctx.Done():
log.Info("canceling consuming messages requested")
return
case d := <-deliveries:
log.Debugf("processing message %d", d.DeliveryTag)
corID := getCorrelationID(d.Headers)
sp, ctxCh := trace.ConsumerSpan(ctx, trace.ComponentOpName(consumerComponent, c.queue),
consumerComponent, corID, mapHeader(d.Headers), c.traceTag)
dec, err := async.DetermineDecoder(d.ContentType)
if err != nil {
errNack := d.Nack(false, c.requeue)
if errNack != nil {
err = patronErrors.Aggregate(err, fmt.Errorf("failed to NACK message: %w", errNack))
}
trace.SpanError(sp)
chErr <- err
return
}
ctxCh = correlation.ContextWithID(ctxCh, corID)
ctxCh = log.WithContext(ctxCh, log.Sub(map[string]interface{}{correlation.ID: corID}))
chMsg <- &message{
ctx: ctxCh,
dec: dec,
del: &d,
span: sp,
requeue: c.requeue,
source: c.queue,
}
}
}
}()
return chMsg, chErr, nil
}
// Close handles closing channel and connection of AMQP.
func (c *consumer) Close() error {
var errChan error
var errConn error
if c.ch != nil {
err := c.ch.Cancel(c.tag, true)
if err != nil {
errChan = fmt.Errorf("failed to cancel channel of consumer %s: %w", c.tag, err)
}
}
if c.conn != nil {
err := c.conn.Close()
if err != nil {
errConn = fmt.Errorf("failed to close connection: %w", err)
}
}
return patronErrors.Aggregate(errChan, errConn)
}
func (c *consumer) consume() (<-chan amqp.Delivery, error) {
conn, err := amqp.DialConfig(c.url, c.cfg)
if err != nil {
return nil, fmt.Errorf("failed to dial @ %s: %w", c.url, err)
}
c.conn = conn
ch, err := c.conn.Channel()
if err != nil {
return nil, fmt.Errorf("failed get channel: %w", err)
}
c.ch = ch
c.tag = uuid.New().String()
log.Infof("consuming messages for tag %s", c.tag)
err = ch.ExchangeDeclare(c.exchange.name, c.exchange.kind, true, false, false, false, nil)
if err != nil {
return nil, fmt.Errorf("failed to declare exchange: %w", err)
}
q, err := ch.QueueDeclare(c.queue, true, false, false, false, nil)
if err != nil {
return nil, fmt.Errorf("failed to declare queue: %w", err)
}
for _, binding := range c.bindings {
if err := ch.QueueBind(q.Name, binding, c.exchange.name, false, nil); err != nil {
return nil, fmt.Errorf("failed to bind queue to exchange queue: %w", err)
}
}
deliveries, err := ch.Consume(c.queue, c.tag, false, false, false, false, nil)
if err != nil {
return nil, fmt.Errorf("failed initialize consumer: %w", err)
}
return deliveries, nil
}
func mapHeader(hh amqp.Table) map[string]string {
mp := make(map[string]string)
for k, v := range hh {
mp[k] = fmt.Sprint(v)
}
return mp
}
func getCorrelationID(hh amqp.Table) string {
for key, value := range hh {
if key == correlation.HeaderID {
val, ok := value.(string)
if ok && val != "" {
return val
}
break
}
}
return uuid.New().String()
}