@@ -16,26 +16,30 @@ func getDefaultConsumerOptions(queueName string) ConsumerOptions {
16
16
NoLocal : false ,
17
17
Args : Table {},
18
18
},
19
- QueueOptions : QueueOptions {
20
- Name : queueName ,
21
- Durable : false ,
22
- AutoDelete : false ,
23
- Exclusive : false ,
24
- NoWait : false ,
25
- Passive : false ,
26
- Args : Table {},
27
- Declare : true ,
19
+ Queues : []QueueOptions {
20
+ {
21
+ Name : queueName ,
22
+ Durable : false ,
23
+ AutoDelete : false ,
24
+ Exclusive : false ,
25
+ NoWait : false ,
26
+ Passive : false ,
27
+ Args : Table {},
28
+ Declare : true ,
29
+ },
28
30
},
29
- ExchangeOptions : ExchangeOptions {
30
- Name : "" ,
31
- Kind : amqp .ExchangeDirect ,
32
- Durable : false ,
33
- AutoDelete : false ,
34
- Internal : false ,
35
- NoWait : false ,
36
- Passive : false ,
37
- Args : Table {},
38
- Declare : false ,
31
+ Exchanges : []ExchangeOptions {
32
+ {
33
+ Name : "" ,
34
+ Kind : amqp .ExchangeDirect ,
35
+ Durable : false ,
36
+ AutoDelete : false ,
37
+ Internal : false ,
38
+ NoWait : false ,
39
+ Passive : false ,
40
+ Args : Table {},
41
+ Declare : false ,
42
+ },
39
43
},
40
44
Bindings : []Binding {},
41
45
Concurrency : 1 ,
@@ -59,8 +63,9 @@ func getDefaultBindingOptions() BindingOptions {
59
63
// If there are Bindings, the queue will be bound to them
60
64
type ConsumerOptions struct {
61
65
RabbitConsumerOptions RabbitConsumerOptions
62
- QueueOptions QueueOptions
63
- ExchangeOptions ExchangeOptions
66
+ QueueName string
67
+ Queues []QueueOptions
68
+ Exchanges []ExchangeOptions
64
69
Bindings []Binding
65
70
Concurrency int
66
71
Logger logger.Logger
@@ -93,105 +98,126 @@ type QueueOptions struct {
93
98
Declare bool
94
99
}
95
100
96
- // Binding describes the bhinding of a queue to a routing key on an exchange
97
- type Binding struct {
98
- RoutingKey string
99
- BindingOptions
100
- }
101
-
102
- // BindingOptions describes the options a binding can have
103
- type BindingOptions struct {
104
- NoWait bool
105
- Args Table
106
- Declare bool
107
- }
108
-
109
101
// WithConsumerOptionsQueueDurable ensures the queue is a durable queue
110
102
func WithConsumerOptionsQueueDurable (options * ConsumerOptions ) {
111
- options .QueueOptions .Durable = true
103
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
104
+ queueOptions .Durable = true
105
+ })
112
106
}
113
107
114
108
// WithConsumerOptionsQueueAutoDelete ensures the queue is an auto-delete queue
115
109
func WithConsumerOptionsQueueAutoDelete (options * ConsumerOptions ) {
116
- options .QueueOptions .AutoDelete = true
110
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
111
+ queueOptions .AutoDelete = true
112
+ })
117
113
}
118
114
119
115
// WithConsumerOptionsQueueExclusive ensures the queue is an exclusive queue
120
116
func WithConsumerOptionsQueueExclusive (options * ConsumerOptions ) {
121
- options .QueueOptions .Exclusive = true
117
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
118
+ queueOptions .Exclusive = true
119
+ })
122
120
}
123
121
124
122
// WithConsumerOptionsQueueNoWait ensures the queue is a no-wait queue
125
123
func WithConsumerOptionsQueueNoWait (options * ConsumerOptions ) {
126
- options .QueueOptions .NoWait = true
124
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
125
+ queueOptions .NoWait = true
126
+ })
127
127
}
128
128
129
129
// WithConsumerOptionsQueuePassive ensures the queue is a passive queue
130
130
func WithConsumerOptionsQueuePassive (options * ConsumerOptions ) {
131
- options .QueueOptions .Passive = true
131
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
132
+ queueOptions .Passive = true
133
+ })
132
134
}
133
135
134
136
// WithConsumerOptionsQueueNoDeclare will turn off the declaration of the queue's
135
137
// existance upon startup
136
138
func WithConsumerOptionsQueueNoDeclare (options * ConsumerOptions ) {
137
- options .QueueOptions .Declare = false
139
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
140
+ queueOptions .Declare = false
141
+ })
138
142
}
139
143
140
144
// WithConsumerOptionsQueueArgs adds optional args to the queue
141
145
func WithConsumerOptionsQueueArgs (args Table ) func (* ConsumerOptions ) {
142
146
return func (options * ConsumerOptions ) {
143
- options .QueueOptions .Args = args
147
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
148
+ queueOptions .Args = args
149
+ })
144
150
}
145
151
}
146
152
147
153
// WithConsumerOptionsExchangeName sets the exchange name
148
154
func WithConsumerOptionsExchangeName (name string ) func (* ConsumerOptions ) {
155
+
149
156
return func (options * ConsumerOptions ) {
150
- options .ExchangeOptions .Name = name
157
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
158
+ exchangeOptions .Name = name
159
+ })
151
160
}
152
161
}
153
162
154
163
// WithConsumerOptionsExchangeKind ensures the queue is a durable queue
155
164
func WithConsumerOptionsExchangeKind (kind string ) func (* ConsumerOptions ) {
165
+
156
166
return func (options * ConsumerOptions ) {
157
- options .ExchangeOptions .Kind = kind
167
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
168
+ exchangeOptions .Kind = kind
169
+ })
158
170
}
159
171
}
160
172
161
173
// WithConsumerOptionsExchangeDurable ensures the exchange is a durable exchange
162
174
func WithConsumerOptionsExchangeDurable (options * ConsumerOptions ) {
163
- options .ExchangeOptions .Durable = true
175
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
176
+ exchangeOptions .Durable = true
177
+ })
164
178
}
165
179
166
180
// WithConsumerOptionsExchangeAutoDelete ensures the exchange is an auto-delete exchange
167
181
func WithConsumerOptionsExchangeAutoDelete (options * ConsumerOptions ) {
168
- options .ExchangeOptions .AutoDelete = true
182
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
183
+ exchangeOptions .AutoDelete = true
184
+ })
169
185
}
170
186
171
187
// WithConsumerOptionsExchangeInternal ensures the exchange is an internal exchange
172
188
func WithConsumerOptionsExchangeInternal (options * ConsumerOptions ) {
173
- options .ExchangeOptions .Internal = true
189
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
190
+ exchangeOptions .Internal = true
191
+ })
174
192
}
175
193
176
194
// WithConsumerOptionsExchangeNoWait ensures the exchange is a no-wait exchange
177
195
func WithConsumerOptionsExchangeNoWait (options * ConsumerOptions ) {
178
- options .ExchangeOptions .NoWait = true
196
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
197
+ exchangeOptions .NoWait = true
198
+ })
179
199
}
180
200
181
201
// WithConsumerOptionsExchangeDeclare stops this library from declaring the exchanges existance
182
202
func WithConsumerOptionsExchangeDeclare (options * ConsumerOptions ) {
183
- options .ExchangeOptions .Declare = true
203
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
204
+ exchangeOptions .Declare = true
205
+ })
184
206
}
185
207
186
208
// WithConsumerOptionsExchangePassive ensures the exchange is a passive exchange
187
209
func WithConsumerOptionsExchangePassive (options * ConsumerOptions ) {
188
- options .ExchangeOptions .Passive = true
210
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
211
+ exchangeOptions .Passive = true
212
+ })
189
213
}
190
214
191
215
// WithConsumerOptionsExchangeArgs adds optional args to the exchange
192
216
func WithConsumerOptionsExchangeArgs (args Table ) func (* ConsumerOptions ) {
193
217
return func (options * ConsumerOptions ) {
194
- options .ExchangeOptions .Args = args
218
+ WithSimpleExchangeOptions (options , func (exchangeOptions * ExchangeOptions ) {
219
+ exchangeOptions .Args = args
220
+ })
195
221
}
196
222
}
197
223
@@ -287,9 +313,55 @@ func WithConsumerOptionsQOSGlobal(options *ConsumerOptions) {
287
313
// multiple nodes in the cluster will have the messages distributed amongst them
288
314
// for higher reliability
289
315
func WithConsumerOptionsQueueQuorum (options * ConsumerOptions ) {
290
- if options .QueueOptions .Args == nil {
291
- options .QueueOptions .Args = Table {}
316
+ WithSimpleQueueOptions (options , func (queueOptions * QueueOptions ) {
317
+ if queueOptions .Args == nil {
318
+ queueOptions .Args = Table {}
319
+ }
320
+
321
+ queueOptions .Args ["x-queue-type" ] = "quorum"
322
+ })
323
+ }
324
+
325
+ // WithSimpleQueueOptions used for backwards compatibility
326
+ // Will set options on the first queue and ensure that queue exists
327
+ func WithSimpleQueueOptions (options * ConsumerOptions , handler func (queueOptions * QueueOptions )) {
328
+ if len (options .Queues ) == 0 {
329
+ options .Queues = append (options .Queues , QueueOptions {})
330
+ }
331
+
332
+ handler (& options .Queues [0 ])
333
+ }
334
+
335
+ // WithSimpleExchangeOptions used for backwards compatibility
336
+ // Will set options on the first exchange and ensure that exchange exists
337
+ func WithSimpleExchangeOptions (options * ConsumerOptions , handler func (exchangeOptions * ExchangeOptions )) {
338
+ if len (options .Exchanges ) == 0 {
339
+ options .Exchanges = append (options .Exchanges , ExchangeOptions {})
292
340
}
293
341
294
- options .QueueOptions .Args ["x-queue-type" ] = "quorum"
342
+ handler (& options .Exchanges [0 ])
343
+ }
344
+
345
+ func WithConsumerQueue (queue QueueOptions ) func (options * ConsumerOptions ) {
346
+ return func (options * ConsumerOptions ) {
347
+ options .Queues = []QueueOptions {queue }
348
+ }
349
+ }
350
+
351
+ func WithConsumerQueues (queues []QueueOptions ) func (options * ConsumerOptions ) {
352
+ return func (options * ConsumerOptions ) {
353
+ options .Queues = queues
354
+ }
355
+ }
356
+
357
+ func WithConsumerBindings (bindings []Binding ) func (options * ConsumerOptions ) {
358
+ return func (options * ConsumerOptions ) {
359
+ options .Bindings = bindings
360
+ }
361
+ }
362
+
363
+ func WithConsumerExchanges (exchanges []ExchangeOptions ) func (options * ConsumerOptions ) {
364
+ return func (options * ConsumerOptions ) {
365
+ options .Exchanges = exchanges
366
+ }
295
367
}
0 commit comments