Skip to content

Commit 28863cc

Browse files
committed
docs and multiple exchanges
1 parent 937bab2 commit 28863cc

6 files changed

+81
-48
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# go-rabbitmq
22

3-
Wrapper of [rabbitmq/amqp091-go](https://github.com/rabbitmq/amqp091-go) that provides reconnection logic and sane defaults. Hit the project with a star if you find it useful ⭐
3+
A wrapper of [rabbitmq/amqp091-go](https://github.com/rabbitmq/amqp091-go) that provides reconnection logic and sane defaults. Hit the project with a star if you find it useful ⭐
44

55
Supported by [Boot.dev](https://boot.dev)
66

@@ -103,7 +103,7 @@ See the [examples](examples) directory for more ideas.
103103

104104
* By default, queues are declared if they didn't already exist by new consumers
105105
* By default, routing-key bindings are declared by consumers if you're using `WithConsumerOptionsRoutingKey`
106-
* By default, exchanges are *not* declared by publishers or consumers if they didn't already exist, hence `WithPublisherOptionsExchangeDeclare` and `WithConsumerOptionsExchangeDeclare`.
106+
* By default, exchanges are *not* declared by publishers or consumers if they don't already exist, hence `WithPublisherOptionsExchangeDeclare` and `WithConsumerOptionsExchangeDeclare`.
107107

108108
Read up on all the options in the GoDoc, there are quite a few of them. I try to pick sane and simple defaults.
109109

consume.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,11 @@ func (consumer *Consumer) startGoroutines(
140140
if err != nil {
141141
return fmt.Errorf("declare qos failed: %w", err)
142142
}
143-
err = declareExchange(consumer.chanManager, options.ExchangeOptions)
144-
if err != nil {
145-
return fmt.Errorf("declare exchange failed: %w", err)
143+
for _, exchangeOption := range options.ExchangeOptions {
144+
err = declareExchange(consumer.chanManager, exchangeOption)
145+
if err != nil {
146+
return fmt.Errorf("declare exchange failed: %w", err)
147+
}
146148
}
147149
err = declareQueue(consumer.chanManager, options.QueueOptions)
148150
if err != nil {

consumer_options.go

+57-29
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,26 @@ func getDefaultConsumerOptions(queueName string) ConsumerOptions {
2626
Args: Table{},
2727
Declare: true,
2828
},
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,
39-
},
40-
Bindings: []Binding{},
41-
Concurrency: 1,
42-
Logger: stdDebugLogger{},
43-
QOSPrefetch: 10,
44-
QOSGlobal: false,
29+
ExchangeOptions: []ExchangeOptions{},
30+
Concurrency: 1,
31+
Logger: stdDebugLogger{},
32+
QOSPrefetch: 10,
33+
QOSGlobal: false,
34+
}
35+
}
36+
37+
func getDefaultExchangeOptions() ExchangeOptions {
38+
return ExchangeOptions{
39+
Name: "",
40+
Kind: amqp.ExchangeDirect,
41+
Durable: false,
42+
AutoDelete: false,
43+
Internal: false,
44+
NoWait: false,
45+
Passive: false,
46+
Args: Table{},
47+
Declare: false,
48+
Bindings: []Binding{},
4549
}
4650
}
4751

@@ -60,8 +64,7 @@ func getDefaultBindingOptions() BindingOptions {
6064
type ConsumerOptions struct {
6165
RabbitConsumerOptions RabbitConsumerOptions
6266
QueueOptions QueueOptions
63-
ExchangeOptions ExchangeOptions
64-
Bindings []Binding
67+
ExchangeOptions []ExchangeOptions
6568
Concurrency int
6669
Logger logger.Logger
6770
QOSPrefetch int
@@ -144,61 +147,77 @@ func WithConsumerOptionsQueueArgs(args Table) func(*ConsumerOptions) {
144147
}
145148
}
146149

150+
func ensureExchangeOptions(options *ConsumerOptions) {
151+
if len(options.ExchangeOptions) == 0 {
152+
options.ExchangeOptions = append(options.ExchangeOptions, getDefaultExchangeOptions())
153+
}
154+
}
155+
147156
// WithConsumerOptionsExchangeName sets the exchange name
148157
func WithConsumerOptionsExchangeName(name string) func(*ConsumerOptions) {
149158
return func(options *ConsumerOptions) {
150-
options.ExchangeOptions.Name = name
159+
ensureExchangeOptions(options)
160+
options.ExchangeOptions[0].Name = name
151161
}
152162
}
153163

154164
// WithConsumerOptionsExchangeKind ensures the queue is a durable queue
155165
func WithConsumerOptionsExchangeKind(kind string) func(*ConsumerOptions) {
156166
return func(options *ConsumerOptions) {
157-
options.ExchangeOptions.Kind = kind
167+
ensureExchangeOptions(options)
168+
options.ExchangeOptions[0].Kind = kind
158169
}
159170
}
160171

161172
// WithConsumerOptionsExchangeDurable ensures the exchange is a durable exchange
162173
func WithConsumerOptionsExchangeDurable(options *ConsumerOptions) {
163-
options.ExchangeOptions.Durable = true
174+
ensureExchangeOptions(options)
175+
options.ExchangeOptions[0].Durable = true
164176
}
165177

166178
// WithConsumerOptionsExchangeAutoDelete ensures the exchange is an auto-delete exchange
167179
func WithConsumerOptionsExchangeAutoDelete(options *ConsumerOptions) {
168-
options.ExchangeOptions.AutoDelete = true
180+
ensureExchangeOptions(options)
181+
options.ExchangeOptions[0].AutoDelete = true
169182
}
170183

171184
// WithConsumerOptionsExchangeInternal ensures the exchange is an internal exchange
172185
func WithConsumerOptionsExchangeInternal(options *ConsumerOptions) {
173-
options.ExchangeOptions.Internal = true
186+
ensureExchangeOptions(options)
187+
options.ExchangeOptions[0].Internal = true
174188
}
175189

176190
// WithConsumerOptionsExchangeNoWait ensures the exchange is a no-wait exchange
177191
func WithConsumerOptionsExchangeNoWait(options *ConsumerOptions) {
178-
options.ExchangeOptions.NoWait = true
192+
ensureExchangeOptions(options)
193+
options.ExchangeOptions[0].NoWait = true
179194
}
180195

181196
// WithConsumerOptionsExchangeDeclare stops this library from declaring the exchanges existance
182197
func WithConsumerOptionsExchangeDeclare(options *ConsumerOptions) {
183-
options.ExchangeOptions.Declare = true
198+
ensureExchangeOptions(options)
199+
options.ExchangeOptions[0].Declare = true
184200
}
185201

186202
// WithConsumerOptionsExchangePassive ensures the exchange is a passive exchange
187203
func WithConsumerOptionsExchangePassive(options *ConsumerOptions) {
188-
options.ExchangeOptions.Passive = true
204+
ensureExchangeOptions(options)
205+
options.ExchangeOptions[0].Passive = true
189206
}
190207

191208
// WithConsumerOptionsExchangeArgs adds optional args to the exchange
192209
func WithConsumerOptionsExchangeArgs(args Table) func(*ConsumerOptions) {
193210
return func(options *ConsumerOptions) {
194-
options.ExchangeOptions.Args = args
211+
ensureExchangeOptions(options)
212+
options.ExchangeOptions[0].Args = args
195213
}
196214
}
197215

198216
// WithConsumerOptionsRoutingKey binds the queue to a routing key with the default binding options
199217
func WithConsumerOptionsRoutingKey(routingKey string) func(*ConsumerOptions) {
200218
return func(options *ConsumerOptions) {
201-
options.Bindings = append(options.Bindings, Binding{
219+
ensureExchangeOptions(options)
220+
options.ExchangeOptions[0].Bindings = append(options.ExchangeOptions[0].Bindings, Binding{
202221
RoutingKey: routingKey,
203222
BindingOptions: getDefaultBindingOptions(),
204223
})
@@ -210,7 +229,16 @@ func WithConsumerOptionsRoutingKey(routingKey string) func(*ConsumerOptions) {
210229
// the zero value. If you want to declare your bindings for example, be sure to set Declare=true
211230
func WithConsumerOptionsBinding(binding Binding) func(*ConsumerOptions) {
212231
return func(options *ConsumerOptions) {
213-
options.Bindings = append(options.Bindings, binding)
232+
ensureExchangeOptions(options)
233+
options.ExchangeOptions[0].Bindings = append(options.ExchangeOptions[0].Bindings, binding)
234+
}
235+
}
236+
237+
// WithConsumerOptionsExchangeOptions adds a new exchange to the consumer, this should probably only be
238+
// used if you want to to consume from multiple exchanges on the same consumer
239+
func WithConsumerOptionsExchangeOptions(exchangeOptions ExchangeOptions) func(*ConsumerOptions) {
240+
return func(options *ConsumerOptions) {
241+
options.ExchangeOptions = append(options.ExchangeOptions, exchangeOptions)
214242
}
215243
}
216244

declare.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,21 @@ func declareExchange(chanManager *channelmanager.ChannelManager, options Exchang
7171
}
7272

7373
func declareBindings(chanManager *channelmanager.ChannelManager, options ConsumerOptions) error {
74-
for _, binding := range options.Bindings {
75-
if !binding.Declare {
76-
continue
77-
}
78-
err := chanManager.QueueBindSafe(
79-
options.QueueOptions.Name,
80-
binding.RoutingKey,
81-
options.ExchangeOptions.Name,
82-
binding.NoWait,
83-
tableToAMQPTable(binding.Args),
84-
)
85-
if err != nil {
86-
return err
74+
for _, exchangeOption := range options.ExchangeOptions {
75+
for _, binding := range exchangeOption.Bindings {
76+
if !binding.Declare {
77+
continue
78+
}
79+
err := chanManager.QueueBindSafe(
80+
options.QueueOptions.Name,
81+
binding.RoutingKey,
82+
exchangeOption.Name,
83+
binding.NoWait,
84+
tableToAMQPTable(binding.Args),
85+
)
86+
if err != nil {
87+
return err
88+
}
8789
}
8890
}
8991
return nil

exchange_options.go

+1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ type ExchangeOptions struct {
1313
Passive bool // if false, a missing exchange will be created on the server
1414
Args Table
1515
Declare bool
16+
Bindings []Binding
1617
}

publisher_options.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func WithPublisherOptionsExchangeNoWait(options *PublisherOptions) {
7777
options.ExchangeOptions.NoWait = true
7878
}
7979

80-
// WithPublisherOptionsExchangeDeclare stops this library from declaring the exchanges existance
80+
// WithPublisherOptionsExchangeDeclare will create the exchange if it doesn't exist
8181
func WithPublisherOptionsExchangeDeclare(options *PublisherOptions) {
8282
options.ExchangeOptions.Declare = true
8383
}

0 commit comments

Comments
 (0)