forked from Ja7ad/amqp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
561 lines (486 loc) · 17.3 KB
/
publisher.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
package amqp
import (
"context"
"fmt"
"github.com/google/uuid"
"sync"
"time"
"github.com/mar-coding/amqp/errs"
"github.com/mar-coding/amqp/logger"
"github.com/mar-coding/amqp/types"
amqp "github.com/rabbitmq/amqp091-go"
)
const (
_defaultRetryDelay = 3 * time.Second
_defaultMaxRetries = 5
)
type publisher struct {
chanManager *channel
connManager *connection
reconnectErrCh <-chan error
closeConnectionToManagerCh chan<- struct{}
disablePublishDueToFlow bool
disablePublishDueToFlowMux *sync.RWMutex
disablePublishDueToBlocked bool
disablePublishDueToBlockedMux *sync.RWMutex
handlerMux *sync.Mutex
notifyReturnHandler func(r types.Return)
notifyPublishHandler func(p types.Confirmation)
logger logger.Logger
exchange *types.Exchange
enc types.Encoder
options *publisherOptions
}
type Publisher interface {
/*
Publish sends a Publishing from the client to an exchange on the server.
When you want a single message to be delivered to a single queue, you can
publish to the default exchange with the routingKey of the queue name. This is
because every declared queue gets an implicit route to the default exchange.
Since publishings are asynchronous, any undeliverable message will get returned
by the server. Add a listener with Channel.NotifyReturn to handle any
undeliverable message when calling publish with either the mandatory or
immediate parameters as true.
Publishings can be undeliverable when the mandatory flag is true and no queue is
bound that matches the routing key, or when the immediate flag is true and no
consumer on the matched queue is ready to accept the delivery.
This can return an error when the channel, connection or socket is closed. The
error or lack of an error does not indicate whether the server has received this
publishing.
It is possible for publishing to not reach the broker if the underlying socket
is shut down without pending publishing packets being flushed from the kernel
buffers. The easy way of making it probable that all publishings reach the
server is to always call Connection.Close before terminating your publishing
application. The way to ensure that all publishings reach the server is to add
a listener to Channel.NotifyPublish and put the channel in confirm mode with
Channel.Confirm. Publishing delivery tags and their corresponding
confirmations startConsume at 1. Exit when all publishings are confirmed.
When Publish does not return an error and the channel is in confirm mode, the
internal counter for DeliveryTags with the first confirmation starts at 1.
Note: routingKey is specific keys in queue for example (subject, topic)
*/
Publish(
mandatory bool,
immediate bool,
msg types.Publishing,
routingKeys ...string,
) error
/*
PublishWithContext sends a Publishing from the client to an exchange on the server and control by prent context.
When you want a single message to be delivered to a single queue, you can
publish to the default exchange with the routingKey of the queue name. This is
because every declared queue gets an implicit route to the default exchange.
Since publishings are asynchronous, any undeliverable message will get returned
by the server. Add a listener with Channel.NotifyReturn to handle any
undeliverable message when calling publish with either the mandatory or
immediate parameters as true.
Publishings can be undeliverable when the mandatory flag is true and no queue is
bound that matches the routing key, or when the immediate flag is true and no
consumer on the matched queue is ready to accept the delivery.
This can return an error when the channel, connection or socket is closed. The
error or lack of an error does not indicate whether the server has received this
publishing.
It is possible for publishing to not reach the broker if the underlying socket
is shut down without pending publishing packets being flushed from the kernel
buffers. The easy way of making it probable that all publishings reach the
server is to always call Connection.Close before terminating your publishing
application. The way to ensure that all publishings reach the server is to add
a listener to Channel.NotifyPublish and put the channel in confirm mode with
Channel.Confirm. Publishing delivery tags and their corresponding
confirmations startConsume at 1. Exit when all publishings are confirmed.
When Publish does not return an error and the channel is in confirm mode, the
internal counter for DeliveryTags with the first confirmation starts at 1.
Note: routingKey is specific keys in queue for example (subject, topic)
*/
PublishWithContext(
ctx context.Context,
mandatory bool,
immediate bool,
msg types.Publishing,
routingKeys ...string,
) error
// PublishWithDeferredConfirmWithContext publishes the provided data to the given routing keys over the connection.
// if the publisher is in confirm mode (which can be either done by calling `NotifyPublish` with a custom handler
// or by using `WithPublisherOptionsConfirm`) a publisher confirmation is returned.
// This confirmation can be used to check if the message was actually published or wait for this to happen.
// If the publisher is not in confirm mode, the returned confirmation will always be nil.
PublishWithDeferredConfirmWithContext(
ctx context.Context,
mandatory bool,
immediate bool,
msg types.Publishing,
routingKeys ...string,
) (types.PublisherConfirmation, error)
// PublishWithRetry sends a Publishing from the client to an exchange on the server,
// controlled by the provided context. It incorporates a retry mechanism, attempting
// to publish the message multiple times with a configurable delay and maximum number
// of retries.
//
// When you want a single message to be delivered to a specific queue, you can publish
// to the default exchange with the routingKey set to the queue name. This is because
// every declared queue gets an implicit route to the default exchange.
//
// Since publishings are asynchronous, any undeliverable message will be returned by
// the server. Add a listener with Channel.NotifyReturn to handle undeliverable
// messages when calling publish with either the mandatory or immediate parameters as true.
//
// Publishings can be undeliverable when the mandatory flag is true and no queue is
// bound that matches the routing key, or when the immediate flag is true and no
// consumer on the matched queue is ready to accept the delivery.
//
// This function may return an error when the channel, connection, or socket is closed.
// The error, or lack of an error, does not indicate whether the server has received this
// publishing.
//
// It is possible for publishing to not reach the broker if the underlying socket
// is shut down without pending publishing packets being flushed from the kernel
// buffers. To increase the likelihood that all publishings reach the server, it is
// recommended to always call Connection.Close before terminating your publishing
// application. Alternatively, add a listener to Channel.NotifyPublish and put the channel
// in confirm mode with Channel.Confirm. Publishing delivery tags and their corresponding
// confirmations start at 1. Exit when all publishings are confirmed.
//
// When PublishWithRetry does not return an error and the channel is in confirm mode,
// the internal counter for DeliveryTags with the first confirmation starts at 1.
//
// Note: routingKey represents specific keys in the queue, such as subject or topic.
PublishWithRetry(
ctx context.Context,
mandatory bool,
immediate bool,
msg types.Publishing,
config types.PublisherConfig,
routingKeys ...string,
) error
// NotifyReturn registers a listener for basic.return methods.
// These can be sent from the server when a publish is undeliverable either from the mandatory or immediate flags.
// These notifications are shared across an entire connection, so if you're creating multiple
// publishers on the same connection keep that in mind
NotifyReturn(handler func(r types.Return))
// NotifyPublish registers a listener for publish confirmations, must set ConfirmPublishings option
// These notifications are shared across an entire connection, so if you're creating multiple
// publishers on the same connection keep that in mind
NotifyPublish(handler func(p types.Confirmation))
Close()
}
// Publisher create publisher interface for publishing message
func (r *AMQP) Publisher(exchange *types.Exchange, confirmMode bool, options ...PublisherOption) (Publisher, error) {
if exchange == nil {
return nil, errs.ErrExchangeIsNil
}
defaultOpts := defaultPublisherOption()
for _, opt := range options {
opt(defaultOpts)
}
chanManager, err := newChannelMgr(r.connMgr, r.logger, r.reconnectInterval)
if err != nil {
return nil, err
}
reconnectErrCh, closeCh := chanManager.notifyReconnect()
pub := &publisher{
chanManager: chanManager,
connManager: r.connMgr,
reconnectErrCh: reconnectErrCh,
closeConnectionToManagerCh: closeCh,
disablePublishDueToFlow: false,
disablePublishDueToFlowMux: &sync.RWMutex{},
disablePublishDueToBlocked: false,
disablePublishDueToBlockedMux: &sync.RWMutex{},
handlerMux: &sync.Mutex{},
notifyReturnHandler: nil,
notifyPublishHandler: nil,
exchange: exchange,
logger: r.logger,
enc: r.enc,
options: defaultOpts,
}
err = pub.startup()
if err != nil {
return nil, err
}
if confirmMode {
pub.NotifyPublish(func(_ types.Confirmation) {
// set a blank handler to set the channel in confirm mode
})
}
go func() {
for err := range pub.reconnectErrCh {
pub.logger.Info(fmt.Sprintf("successful publisher recovery from: %v", err))
err := pub.startup()
if err != nil {
pub.logger.Error(fmt.Sprintf("error on startup for publisher after cancel or close: %v", err))
pub.logger.Error("publisher closing, unable to recover")
return
}
pub.startReturnHandler()
pub.startPublishHandler()
}
}()
return pub, nil
}
func (r *publisher) Publish(
mandatory bool,
immediate bool,
msg types.Publishing,
routingKeys ...string,
) error {
return r.PublishWithContext(context.Background(), mandatory, immediate, msg, routingKeys...)
}
func (r *publisher) PublishWithRetry(
ctx context.Context,
mandatory bool,
immediate bool,
msg types.Publishing,
config types.PublisherConfig,
routingKeys ...string,
) error {
if config.RetryDelay == 0 {
config.RetryDelay = _defaultRetryDelay
}
if config.MaxRetries == 0 {
config.MaxRetries = _defaultMaxRetries
}
var lastErr error
ticker := time.NewTicker(config.RetryDelay)
for i := 0; i <= config.MaxRetries; i++ {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
err := r.PublishWithContext(ctx, mandatory, immediate, msg, routingKeys...)
if err == nil {
return nil
}
lastErr = err
}
}
return lastErr
}
func (r *publisher) PublishWithContext(
ctx context.Context,
mandatory bool,
immediate bool,
msg types.Publishing,
routingKeys ...string,
) error {
r.disablePublishDueToFlowMux.RLock()
defer r.disablePublishDueToFlowMux.RUnlock()
if r.disablePublishDueToFlow {
return fmt.Errorf("publishing blocked due to high flow on the server")
}
r.disablePublishDueToBlockedMux.RLock()
defer r.disablePublishDueToBlockedMux.RUnlock()
if r.disablePublishDueToBlocked {
return fmt.Errorf("publishing blocked due to TCP block on the server")
}
if routingKeys == nil {
return errs.ErrRoutingKeyIsNil
}
body, err := r.enc.Encode(msg.Body)
if err != nil {
return errs.ErrFailedEncode
}
for _, routingKey := range routingKeys {
pub := amqp.Publishing{
Headers: msg.Headers,
ContentType: msg.ContentType,
ContentEncoding: msg.ContentEncoding,
DeliveryMode: uint8(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,
Body: body,
}
if r.options.AutoMessageID {
pub.MessageId = uuid.New().String()
}
if r.options.AutoTimeStamp {
pub.Timestamp = time.Now()
}
// Actual publish.
err := r.chanManager.publishWithContextSafe(
ctx,
r.exchange.Name,
routingKey,
mandatory,
immediate,
pub,
)
if err != nil {
return err
}
}
return nil
}
func (r *publisher) PublishWithDeferredConfirmWithContext(
ctx context.Context,
mandatory bool,
immediate bool,
msg types.Publishing,
routingKeys ...string,
) (types.PublisherConfirmation, error) {
r.disablePublishDueToFlowMux.RLock()
defer r.disablePublishDueToFlowMux.RUnlock()
if r.disablePublishDueToFlow {
return nil, fmt.Errorf("publishing blocked due to high flow on the server")
}
r.disablePublishDueToBlockedMux.RLock()
defer r.disablePublishDueToBlockedMux.RUnlock()
if r.disablePublishDueToBlocked {
return nil, fmt.Errorf("publishing blocked due to TCP block on the server")
}
var deferredConfirmations []*amqp.DeferredConfirmation
body, err := r.enc.Encode(msg.Body)
if err != nil {
return types.PublisherConfirmation{}, errs.ErrFailedEncode
}
for _, routingKey := range routingKeys {
pub := amqp.Publishing{
Headers: msg.Headers,
ContentType: msg.ContentType,
ContentEncoding: msg.ContentEncoding,
DeliveryMode: uint8(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,
Body: body,
}
if r.options.AutoMessageID {
pub.MessageId = uuid.New().String()
}
if r.options.AutoTimeStamp {
pub.Timestamp = time.Now()
}
// Actual publish.
conf, err := r.chanManager.publishWithDeferredConfirmWithContextSafe(
ctx,
r.exchange.Name,
routingKey,
mandatory,
immediate,
pub,
)
if err != nil {
return nil, err
}
deferredConfirmations = append(deferredConfirmations, conf)
}
return deferredConfirmations, nil
}
func (r *publisher) NotifyReturn(handler func(r types.Return)) {
r.handlerMux.Lock()
start := r.notifyReturnHandler == nil
r.notifyReturnHandler = handler
r.handlerMux.Unlock()
if start {
r.startReturnHandler()
}
}
func (r *publisher) NotifyPublish(handler func(p types.Confirmation)) {
r.handlerMux.Lock()
shouldStart := r.notifyPublishHandler == nil
r.notifyPublishHandler = handler
r.handlerMux.Unlock()
if shouldStart {
r.startPublishHandler()
}
}
func (r *publisher) Close() {
// close the channel so that rabbitmq server knows that the
// publisher has been stopped.
err := r.chanManager.close()
if err != nil {
r.logger.Error(fmt.Sprintf("error while closing the channel: %v", err))
}
r.logger.Warn("closing publisher...")
go func() {
r.closeConnectionToManagerCh <- struct{}{}
}()
}
func (r *publisher) startup() error {
err := declareExchange(r.chanManager, r.exchange)
if err != nil {
return fmt.Errorf("declare exchange failed: %w", err)
}
go r.startNotifyFlowHandler()
go r.startNotifyBlockedHandler()
return nil
}
func (r *publisher) startNotifyFlowHandler() {
notifyFlowChan := r.chanManager.notifyFlowSafe(make(chan bool))
r.disablePublishDueToFlowMux.Lock()
r.disablePublishDueToFlow = false
r.disablePublishDueToFlowMux.Unlock()
for ok := range notifyFlowChan {
r.disablePublishDueToFlowMux.Lock()
if ok {
r.logger.Warn("pausing publishing due to flow request from server")
r.disablePublishDueToFlow = true
} else {
r.disablePublishDueToFlow = false
r.logger.Warn("resuming publishing due to flow request from server")
}
r.disablePublishDueToFlowMux.Unlock()
}
}
func (r *publisher) startNotifyBlockedHandler() {
blocks := r.connManager.notifyBlockedSafe(make(chan amqp.Blocking))
r.disablePublishDueToBlockedMux.Lock()
r.disablePublishDueToBlocked = false
r.disablePublishDueToBlockedMux.Unlock()
for b := range blocks {
r.disablePublishDueToBlockedMux.Lock()
if b.Active {
r.logger.Warn("pausing publishing due to TCP blocking from server")
r.disablePublishDueToBlocked = true
} else {
r.disablePublishDueToBlocked = false
r.logger.Warn("resuming publishing due to TCP blocking from server")
}
r.disablePublishDueToBlockedMux.Unlock()
}
}
func (r *publisher) startReturnHandler() {
r.handlerMux.Lock()
if r.notifyReturnHandler == nil {
r.handlerMux.Unlock()
return
}
r.handlerMux.Unlock()
go func() {
returns := r.chanManager.notifyReturnSafe(make(chan amqp.Return, 1))
for ret := range returns {
go r.notifyReturnHandler(types.Return{Return: ret})
}
}()
}
func (r *publisher) startPublishHandler() {
r.handlerMux.Lock()
if r.notifyPublishHandler == nil {
r.handlerMux.Unlock()
return
}
r.handlerMux.Unlock()
r.chanManager.confirmSafe(false)
go func() {
confirmationCh := r.chanManager.notifyPublishSafe(make(chan amqp.Confirmation, 1))
for conf := range confirmationCh {
go r.notifyPublishHandler(types.Confirmation{
Confirmation: conf,
ReconnectionCount: int(r.chanManager.getReconnectionCount()),
})
}
}()
}