-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsume_options.go
369 lines (321 loc) · 12.1 KB
/
consume_options.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
package clarimq
import (
"fmt"
"time"
)
const (
defaultQOSPrefetch int = 10
defaultConcurrency int = 1
undefinedConsumer string = "undefined_consumer"
quorum string = "quorum"
maxPriorityKey string = "x-max-priority"
queueTypeKey string = "x-queue-type"
defaultDLXDelay1s time.Duration = time.Second
defaultDLXDelay10s time.Duration = 10 * time.Second
defaultDLXDelay1m time.Duration = time.Minute
defaultDLXDelay10m time.Duration = 10 * time.Minute
defaultDLXDelay1h time.Duration = time.Hour
defaultMaxRetries int64 = 10
)
type (
// ConsumeOption is an option for a Consumer.
ConsumeOption func(*ConsumeOptions)
// ConsumeOptions are used to describe how a new consumer will be configured.
ConsumeOptions struct {
ConsumerOptions *ConsumerOptions
QueueOptions *QueueOptions
ExchangeOptions *ExchangeOptions
RetryOptions *RetryOptions
Bindings []Binding
// The number of message handlers, that will run concurrently.
HandlerQuantity int
// If true, the consumer will start consuming messages instantly after successful creation.
// Default: false.
ConsumeAfterCreation bool
}
// RetryOptions are used to describe how the retry will be configured.
RetryOptions struct {
// Is used to handle the retries on a separate connection.
// If not specified, a connection will be created.
RetryConn *Connection
// The delays which a message will be exponentially redelivered with.
Delays []time.Duration
// The maximum number of times a message will be redelivered.
MaxRetries int64
// When enabled all retry related queues and exchanges associated when the consumer gets closed.
//
// Warning: Existing messages on the retry queues will be purged.
Cleanup bool
MaxRetriesExceededHandler MaxRetriesExceededHandler
publisher *Publisher
isInternalConn bool
dlxName string
dlqNameBase string
}
// ConsumerOptions are used to configure the consumer.
ConsumerOptions struct {
// Application or exchange specific fields,
// the headers exchange will inspect this field.
Args Table
// The name of the consumer / consumer-tag.
Name string
// Auto client acknowledgment for each message.
AutoAck bool
// Ensures that this is the sole consumer from the queue.
Exclusive bool
// If true, the client does not wait for a reply method. If the broker could not complete the method it will raise a channel or connection exception.
NoWait bool
}
MaxRetriesExceededHandler func(delivery *Delivery) error
)
// defaultConsumerOptions describes the options that will be used when a value isn't provided.
func defaultConsumerOptions() *ConsumeOptions {
return &ConsumeOptions{
ConsumerOptions: &ConsumerOptions{
Name: newDefaultConsumerName(),
AutoAck: false,
Exclusive: false,
NoWait: false,
Args: make(Table),
},
QueueOptions: defaultQueueOptions(),
ExchangeOptions: defaultExchangeOptions(),
Bindings: []Binding{},
HandlerQuantity: defaultConcurrency,
}
}
func newDefaultConsumerName() string {
return fmt.Sprintf("%s_%s", undefinedConsumer, newRandomString())
}
// WithCustomConsumeOptions sets the consumer options.
//
// It can be used to set all consumer options at once.
func WithCustomConsumeOptions(options *ConsumeOptions) ConsumeOption {
return func(opt *ConsumeOptions) {
if options != nil {
opt.HandlerQuantity = options.HandlerQuantity
if options.QueueOptions != nil {
opt.QueueOptions = &QueueOptions{
Args: options.QueueOptions.Args,
Durable: options.QueueOptions.Durable,
AutoDelete: options.QueueOptions.AutoDelete,
Exclusive: options.QueueOptions.Exclusive,
NoWait: options.QueueOptions.NoWait,
Passive: options.QueueOptions.Passive,
Declare: options.QueueOptions.Declare,
}
}
if options.Bindings != nil {
opt.Bindings = options.Bindings
}
if options.ExchangeOptions != nil {
opt.ExchangeOptions = options.ExchangeOptions
}
if options.ConsumerOptions != nil {
opt.ConsumerOptions = options.ConsumerOptions
}
}
}
}
// WithQueueOptionDurable sets whether the queue is a durable queue.
//
// Default: false.
func WithQueueOptionDurable(durable bool) ConsumeOption {
return func(options *ConsumeOptions) { options.QueueOptions.Durable = durable }
}
// WithQueueOptionAutoDelete sets whether the queue is an auto-delete queue.
//
// Default: false.
func WithQueueOptionAutoDelete(autoDelete bool) ConsumeOption {
return func(options *ConsumeOptions) { options.QueueOptions.AutoDelete = autoDelete }
}
// WithQueueOptionExclusive sets whether the queue is an exclusive queue.
//
// Default: false.
func WithQueueOptionExclusive(exclusive bool) ConsumeOption {
return func(options *ConsumeOptions) { options.QueueOptions.Exclusive = exclusive }
}
// WithQueueOptionNoWait sets whether the queue is a no-wait queue.
//
// Default: false.
func WithQueueOptionNoWait(noWait bool) ConsumeOption {
return func(options *ConsumeOptions) { options.QueueOptions.NoWait = noWait }
}
// WithQueueOptionPassive sets whether the queue is a passive queue.
//
// Default: false.
func WithQueueOptionPassive(passive bool) ConsumeOption {
return func(options *ConsumeOptions) { options.QueueOptions.Passive = passive }
}
// WithQueueOptionDeclare sets whether the queue should be declared upon startup
// if it doesn't already exist.
//
// Default: true.
func WithQueueOptionDeclare(declare bool) ConsumeOption {
return func(options *ConsumeOptions) { options.QueueOptions.Declare = declare }
}
// WithQueueOptionPriority if set a priority queue will be declared with the
// given maximum priority.
func WithQueueOptionPriority(maxPriority Priority) ConsumeOption {
return func(options *ConsumeOptions) {
if options.QueueOptions.Args != nil {
options.QueueOptions.Args[maxPriorityKey] = uint8(maxPriority)
}
}
}
// WithQueueOptionArgs adds optional args to the queue.
func WithQueueOptionArgs(args Table) ConsumeOption {
return func(options *ConsumeOptions) {
if options.QueueOptions.Args != nil {
options.QueueOptions.Args = args
}
}
}
// WithQueueOptionQuorum sets the queue quorum type, which means
// multiple nodes in the cluster will have the messages distributed amongst them
// for higher reliability.
func WithQueueOptionQuorum() ConsumeOption {
return func(options *ConsumeOptions) {
if options.QueueOptions.Args == nil {
options.QueueOptions.Args = make(Table)
}
options.QueueOptions.Args[queueTypeKey] = quorum
}
}
// WithExchangeOptionName sets the exchange name.
func WithExchangeOptionName(name string) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.Name = name }
}
// WithExchangeOptionKind ensures the queue is a durable queue.
func WithExchangeOptionKind(kind string) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.Kind = kind }
}
// WithExchangeOptionDurable sets whether the exchange is a durable exchange.
//
// Default: false.
func WithExchangeOptionDurable(durable bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.Durable = durable }
}
// WithExchangeOptionAutoDelete sets whether the exchange is an auto-delete exchange.
//
// Default: false.
func WithExchangeOptionAutoDelete(autoDelete bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.AutoDelete = autoDelete }
}
// WithExchangeOptionInternal sets whether the exchange is an internal exchange.
//
// Default: false.
func WithExchangeOptionInternal(internal bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.Internal = internal }
}
// WithExchangeOptionNoWait sets whether the exchange is a no-wait exchange.
//
// Default: false.
func WithExchangeOptionNoWait(noWait bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.NoWait = noWait }
}
// WithExchangeOptionDeclare sets whether the exchange should be declared on startup
// if it doesn't already exist.
//
// Default: false.
func WithExchangeOptionDeclare(declare bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.Declare = declare }
}
// WithExchangeOptionPassive sets whether the exchange is a passive exchange.
//
// Default: false.
func WithExchangeOptionPassive(passive bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ExchangeOptions.Passive = passive }
}
// WithExchangeOptionArgs adds optional args to the exchange.
func WithExchangeOptionArgs(args Table) ConsumeOption {
return func(options *ConsumeOptions) {
if options.ExchangeOptions.Args != nil {
options.ExchangeOptions.Args = args
}
}
}
// WithBindingOptionCustomBinding adds a new binding to the queue which allows you to set the binding options
// on a per-binding basis. Keep in mind that everything in the BindingOptions struct will default to
// the zero value. If you want to declare your bindings for example, be sure to set Declare=true.
func WithBindingOptionCustomBinding(binding Binding) ConsumeOption {
return func(options *ConsumeOptions) {
options.Bindings = append(options.Bindings, binding)
}
}
// WithConsumerOptionRoutingKey binds the queue to a routing key with the default binding options.
func WithConsumerOptionRoutingKey(routingKey string) ConsumeOption {
return func(options *ConsumeOptions) {
options.Bindings = append(options.Bindings, Binding{
RoutingKey: routingKey,
BindingOptions: defaultBindingOptions(),
})
}
}
// WithConsumerOptionHandlerQuantity sets the number of message handlers, that will run concurrently.
func WithConsumerOptionHandlerQuantity(concurrency int) ConsumeOption {
return func(options *ConsumeOptions) {
options.HandlerQuantity = concurrency
}
}
// WithConsumerOptionConsumerName sets the name of the consumer.
//
// If unset a random name will be given.
func WithConsumerOptionConsumerName(consumerName string) ConsumeOption {
return func(options *ConsumeOptions) {
options.ConsumerOptions.Name = consumerName
}
}
// WithConsumerOptionDeadLetterRetry enables the dead letter retry.
//
// For each `delay` a dead letter queue will be declared.
//
// After exceeding `maxRetries` the delivery will be dropped.
func WithConsumerOptionDeadLetterRetry(options *RetryOptions) ConsumeOption {
return func(opt *ConsumeOptions) {
if options != nil {
if len(options.Delays) == 0 {
options.Delays = []time.Duration{
defaultDLXDelay1s,
defaultDLXDelay10s,
defaultDLXDelay1m,
defaultDLXDelay10m,
defaultDLXDelay1h,
}
}
if options.MaxRetries <= 0 {
options.MaxRetries = defaultMaxRetries
}
opt.RetryOptions = options
}
}
}
// WithConsumerOptionConsumerAutoAck sets the auto acknowledge property of the consumer.
//
// Default: false.
func WithConsumerOptionConsumerAutoAck(autoAck bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ConsumerOptions.AutoAck = autoAck }
}
// WithConsumerOptionConsumerExclusive sets the exclusive property of this consumer, which means
// the broker will ensure that this is the only consumer
// from this queue. When exclusive is false, the broker will fairly distribute
// deliveries across multiple consumers.
//
// Default: false.
func WithConsumerOptionConsumerExclusive(exclusive bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ConsumerOptions.Exclusive = exclusive }
}
// WithConsumerOptionNoWait sets the exclusive no-wait property of this consumer, which means
// it does not wait for the broker to confirm the request and
// immediately begin deliveries. If it is not possible to consume, a channel
// exception will be raised and the channel will be closed.
//
// Default: false.
func WithConsumerOptionNoWait(noWait bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ConsumerOptions.NoWait = noWait }
}
// WithConsumerOptionConsumeAfterCreation sets the consume after creation property of the consumer.
// If true the consumer will immediately start consuming messages from the queue after creation.
func WithConsumerOptionConsumeAfterCreation(consumeAfterCreation bool) ConsumeOption {
return func(options *ConsumeOptions) { options.ConsumeAfterCreation = consumeAfterCreation }
}