-
Notifications
You must be signed in to change notification settings - Fork 31
/
rabbitmq.go
528 lines (498 loc) · 13.7 KB
/
rabbitmq.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package mq
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/pkg/errors"
amqp "github.com/rabbitmq/amqp091-go"
"github.com/rs/zerolog/log"
"github.com/runabol/tork"
"github.com/runabol/tork/internal/syncx"
"github.com/runabol/tork/internal/uuid"
)
const (
RABBITMQ_DEFAULT_CONSUMER_TIMEOUT = time.Minute * 30
)
const (
exchangeTopic = "amq.topic"
exchangeDefault = ""
keyDefault = ""
defaultHeartbeatTTL = 60000
)
type RabbitMQBroker struct {
connPool []*amqp.Connection
nextConn int
queues *syncx.Map[string, string]
topics *syncx.Map[string, string]
subscriptions []*subscription
mu sync.RWMutex
url string
shuttingDown bool
heartbeatTTL int
consumerTimeout int
managementURL string
}
type subscription struct {
qname string
ch *amqp.Channel
name string
done chan int
}
type rabbitq struct {
Name string `json:"name"`
Messages int `json:"messages"`
Consumers int `json:"consumers"`
Unacked int `json:"messages_unacknowledged"`
Arguments map[string]any `json:"arguments"`
}
type Option = func(b *RabbitMQBroker)
// WithHeartbeatTTL sets the TTL for a heartbeat pending in the queue.
// The value of the TTL argument or policy must be a non-negative integer (0 <= n),
// describing the TTL period in milliseconds.
func WithHeartbeatTTL(ttl int) Option {
return func(b *RabbitMQBroker) {
b.heartbeatTTL = ttl
}
}
// WithConsumerTimeout sets the maximum amount of time in
// RabbitMQ will wait for a message ack before from a consumer
// before deeming the message undelivered and shuting down the
// connection. Default: 30 minutes
func WithConsumerTimeoutMS(consumerTimeout time.Duration) Option {
return func(b *RabbitMQBroker) {
b.consumerTimeout = int(consumerTimeout.Milliseconds())
}
}
func WithManagementURL(url string) Option {
return func(b *RabbitMQBroker) {
b.managementURL = url
}
}
func NewRabbitMQBroker(url string, opts ...Option) (*RabbitMQBroker, error) {
connPool := make([]*amqp.Connection, 3)
for i := 0; i < len(connPool); i++ {
conn, err := amqp.Dial(url)
if err != nil {
return nil, errors.Wrapf(err, "error dialing to RabbitMQ")
}
connPool[i] = conn
}
b := &RabbitMQBroker{
queues: new(syncx.Map[string, string]),
topics: new(syncx.Map[string, string]),
url: url,
connPool: connPool,
subscriptions: make([]*subscription, 0),
heartbeatTTL: defaultHeartbeatTTL,
consumerTimeout: int(RABBITMQ_DEFAULT_CONSUMER_TIMEOUT.Milliseconds()),
}
for _, o := range opts {
o(b)
}
return b, nil
}
func (b *RabbitMQBroker) Queues(ctx context.Context) ([]QueueInfo, error) {
rqs, err := b.rabbitQueues(ctx)
if err != nil {
return nil, err
}
qis := make([]QueueInfo, len(rqs))
for i, rq := range rqs {
qis[i] = QueueInfo{
Name: rq.Name,
Size: rq.Messages,
Subscribers: rq.Consumers,
Unacked: rq.Unacked,
}
}
return qis, nil
}
func (b *RabbitMQBroker) rabbitQueues(ctx context.Context) ([]rabbitq, error) {
u, err := url.Parse(b.url)
if err != nil {
return nil, errors.Wrapf(err, "unable to parse url: %s", b.url)
}
var endpoint string
if b.managementURL != "" {
endpoint = fmt.Sprintf("%s/api/queues/", b.managementURL)
} else {
endpoint = fmt.Sprintf("http://%s:15672/api/queues/", u.Hostname())
}
client := &http.Client{}
req, err := http.NewRequestWithContext(ctx, "GET", endpoint, nil)
if err != nil {
return nil, errors.Wrapf(err, "unable to build get queues request")
}
pw, _ := u.User.Password()
req.SetBasicAuth(u.User.Username(), pw)
resp, err := client.Do(req)
if err != nil {
return nil, errors.Wrapf(err, "error getting rabbitmq queues from the API")
}
rqs := make([]rabbitq, 0)
err = json.NewDecoder(resp.Body).Decode(&rqs)
if err != nil {
return nil, errors.Wrapf(err, "error unmarshalling API response")
}
return rqs, nil
}
func (b *RabbitMQBroker) PublishTask(ctx context.Context, qname string, t *tork.Task) error {
// it's possible that the coordinator will try to publish to a queue for which there's
// no active worker yet -- so it's possible that the queue was never created. This
// ensures that the queue is created so the message don't get dropped.
conn, err := b.getConnection()
if err != nil {
return errors.Wrapf(err, "error getting a connection")
}
ch, err := conn.Channel()
if err != nil {
return errors.Wrapf(err, "error creating channel")
}
defer ch.Close()
if err := b.declareQueue(exchangeDefault, keyDefault, qname, ch); err != nil {
return errors.Wrapf(err, "error (re)declaring queue")
}
return b.publish(ctx, exchangeDefault, qname, t)
}
func (b *RabbitMQBroker) SubscribeForTasks(qname string, handler func(t *tork.Task) error) error {
return b.subscribe(exchangeDefault, keyDefault, qname, func(msg any) error {
t, ok := msg.(*tork.Task)
if !ok {
return errors.Errorf("expecting a *tork.Task but got %T", t)
}
return handler(t)
})
}
func (b *RabbitMQBroker) subscribe(exchange, key, qname string, handler func(msg any) error) error {
conn, err := b.getConnection()
if err != nil {
return errors.Wrapf(err, "error getting a connection")
}
ch, err := conn.Channel()
if err != nil {
return errors.Wrapf(err, "error creating channel")
}
if err := b.declareQueue(exchange, key, qname, ch); err != nil {
return errors.Wrapf(err, "error (re)declaring queue")
}
if err := ch.Qos(1, 0, false); err != nil {
return errors.Wrapf(err, "error setting qos on channel")
}
cname := uuid.NewUUID()
msgs, err := ch.Consume(
qname, // queue
cname, // consumer name
false, // auto-ack
false, // exclusive
false, // no-local
false, // no-wait
nil, // args
)
if err != nil {
return errors.Wrapf(err, "unable to subscribe on q: %s", qname)
}
log.Debug().Msgf("created channel %s for queue: %s", cname, qname)
sub := &subscription{
ch: ch,
qname: qname,
name: cname,
done: make(chan int),
}
b.mu.Lock()
b.subscriptions = append(b.subscriptions, sub)
b.mu.Unlock()
go func() {
for d := range msgs {
if msg, err := deserialize(d.Type, d.Body); err != nil {
log.Error().
Err(err).
Str("queue", qname).
Str("body", (string(d.Body))).
Str("type", (string(d.Type))).
Msg("failed to deserialized message")
} else {
if err := handler(msg); err != nil {
log.Error().
Err(err).
Str("queue", qname).
Str("body", (string(d.Body))).
Msg("failed to handle message")
if err := d.Reject(false); err != nil {
log.Error().
Err(err).
Msg("failed to ack message")
}
} else {
if err := d.Ack(false); err != nil {
log.Error().
Err(err).
Msg("failed to ack message")
}
}
}
}
maxAttempts := 20
for attempt := 1; !b.shuttingDown && attempt <= maxAttempts; attempt++ {
log.Info().Msgf("%s channel closed. reconnecting", qname)
if err := b.subscribe(exchange, key, qname, handler); err != nil {
log.Error().
Err(err).
Msgf("error reconnecting to %s (attempt %d/%d)", qname, attempt, maxAttempts)
time.Sleep(time.Second * time.Duration(attempt))
} else {
return
}
}
sub.done <- 1
}()
return nil
}
func serialize(msg any) ([]byte, error) {
mtype := fmt.Sprintf("%T", msg)
if mtype != "*tork.Task" && mtype != "*tork.Job" && mtype != "*tork.Node" {
return nil, errors.Errorf("unnknown type: %T", msg)
}
body, err := json.Marshal(msg)
if err != nil {
return nil, errors.Wrapf(err, "unable to serialize the message")
}
return body, nil
}
func deserialize(tname string, body []byte) (any, error) {
switch tname {
case "*tork.Task":
t := tork.Task{}
if err := json.Unmarshal(body, &t); err != nil {
return nil, err
}
return &t, nil
case "*tork.Job":
j := tork.Job{}
if err := json.Unmarshal(body, &j); err != nil {
return nil, err
}
return &j, nil
case "*tork.Node":
n := tork.Node{}
if err := json.Unmarshal(body, &n); err != nil {
return nil, err
}
return &n, nil
}
return nil, errors.Errorf("unknown message type: %s", tname)
}
func (b *RabbitMQBroker) declareQueue(exchange, key, qname string, ch *amqp.Channel) error {
_, ok := b.queues.Get(qname)
if ok {
return nil
}
// get a list of existing queues
qs, err := b.Queues(context.Background())
if err != nil {
return err
}
exists := false
for _, q := range qs {
if q.Name == qname {
exists = true
}
}
if !exists {
log.Debug().Msgf("declaring queue: %s", qname)
args := amqp.Table{}
if qname == QUEUE_HEARTBEAT {
args["x-message-ttl"] = b.heartbeatTTL
}
args["x-consumer-timeout"] = b.consumerTimeout
_, err = ch.QueueDeclare(
qname,
false, // durable
false, // delete when unused
strings.HasPrefix(qname, QUEUE_EXCLUSIVE_PREFIX), // exclusive
false, // no-wait
args, // arguments
)
if err != nil {
return err
}
if exchange != exchangeDefault {
if err := ch.QueueBind(qname, key, exchange, false, nil); err != nil {
return err
}
}
}
b.queues.Set(qname, qname)
return nil
}
func (b *RabbitMQBroker) PublishHeartbeat(ctx context.Context, n *tork.Node) error {
return b.publish(ctx, exchangeDefault, QUEUE_HEARTBEAT, n)
}
func (b *RabbitMQBroker) publish(ctx context.Context, exchange, key string, msg any) error {
conn, err := b.getConnection()
if err != nil {
return errors.Wrapf(err, "error getting a connection")
}
ch, err := conn.Channel()
if err != nil {
return errors.Wrapf(err, "error creating channel")
}
defer ch.Close()
body, err := serialize(msg)
if err != nil {
return err
}
err = ch.PublishWithContext(ctx,
exchange, // exchange
key, // routing key
false, // mandatory
false, // immediate
amqp.Publishing{
Type: fmt.Sprintf("%T", msg),
ContentType: "application/json",
Body: []byte(body),
})
if err != nil {
return errors.Wrapf(err, "unable to publish message")
}
return nil
}
func (b *RabbitMQBroker) SubscribeForHeartbeats(handler func(n *tork.Node) error) error {
return b.subscribe(exchangeDefault, keyDefault, QUEUE_HEARTBEAT, func(msg any) error {
n, ok := msg.(*tork.Node)
if !ok {
return errors.Errorf("expecting a *tork.Node but got %T", msg)
}
return handler(n)
})
}
func (b *RabbitMQBroker) PublishJob(ctx context.Context, j *tork.Job) error {
return b.publish(ctx, exchangeDefault, QUEUE_JOBS, j)
}
func (b *RabbitMQBroker) SubscribeForJobs(handler func(j *tork.Job) error) error {
return b.subscribe(exchangeDefault, keyDefault, QUEUE_JOBS, func(msg any) error {
j, ok := msg.(*tork.Job)
if !ok {
return errors.Errorf("expecting a *tork.Job but got %T", msg)
}
return handler(j)
})
}
func (b *RabbitMQBroker) getConnection() (*amqp.Connection, error) {
b.mu.Lock()
defer b.mu.Unlock()
if len(b.connPool) == 0 {
return nil, errors.Errorf("connection pool is empty")
}
var err error
var conn *amqp.Connection
conn = b.connPool[b.nextConn]
if conn.IsClosed() {
// clearing the known queues because rabbitmq
// might have crashed and we would need to
// re-declare them
b.queues = new(syncx.Map[string, string])
log.Warn().Msg("connection is closed. reconnecting to RabbitMQ")
conn, err = amqp.Dial(b.url)
if err != nil {
return nil, errors.Wrapf(err, "error dialing to RabbitMQ")
}
b.connPool[b.nextConn] = conn
}
b.nextConn = b.nextConn + 1
if b.nextConn >= len(b.connPool) {
b.nextConn = 0
}
return conn, nil
}
func (b *RabbitMQBroker) isShuttingDown() bool {
b.mu.RLock()
defer b.mu.RUnlock()
return b.shuttingDown
}
func (b *RabbitMQBroker) Shutdown(ctx context.Context) error {
// when running in standalone mode both the coordinator
// and the worker will attempt to shutdown the broker
if b.isShuttingDown() {
return nil
}
b.mu.Lock()
b.shuttingDown = true
b.mu.Unlock()
// close channels cleanly
for _, sub := range b.subscriptions {
log.Debug().
Msgf("shutting down subscription on %s", sub.qname)
if err := sub.ch.Cancel(sub.name, false); err != nil {
log.Error().
Err(err).
Msgf("error closing channel for %s", sub.qname)
}
}
// let's give the subscribers a grace
// period to allow them to cleanly exit
for _, sub := range b.subscriptions {
// skip non-coordinator queue
if !IsCoordinatorQueue(sub.qname) {
continue
}
log.Debug().
Msgf("waiting for subscription %s to terminate", sub.qname)
select {
case <-ctx.Done():
case <-sub.done:
}
}
b.mu.Lock()
b.subscriptions = []*subscription{}
b.mu.Unlock()
// terminate connections cleanly
for _, conn := range b.connPool {
log.Debug().
Msgf("shutting down connection to %s", conn.RemoteAddr())
var done chan int = make(chan int)
go func(c *amqp.Connection) {
if err := c.Close(); err != nil {
log.Error().
Err(err).
Msg("error closing rabbitmq connection")
}
done <- 1
}(conn)
select {
case <-ctx.Done():
case <-done:
}
}
b.mu.Lock()
b.connPool = []*amqp.Connection{}
b.mu.Unlock()
return nil
}
func (b *RabbitMQBroker) SubscribeForEvents(ctx context.Context, pattern string, handler func(event any)) error {
key := strings.ReplaceAll(pattern, "*", "#")
qname := fmt.Sprintf("%s-%s", QUEUE_EXCLUSIVE_PREFIX, uuid.NewUUID())
return b.subscribe(exchangeTopic, key, qname, func(msg any) error {
handler(msg)
return nil
})
}
func (b *RabbitMQBroker) PublishEvent(ctx context.Context, topic string, event any) error {
return b.publish(ctx, exchangeTopic, topic, event)
}
func (b *RabbitMQBroker) HealthCheck(ctx context.Context) error {
conn, err := b.getConnection()
if err != nil {
return errors.Wrapf(err, "error getting a connection")
}
ch, err := conn.Channel()
if err != nil {
return errors.Wrapf(err, "error creating channel")
}
defer ch.Close()
return nil
}