From bc434fde5cb401f131fc58971d4ac93fffd9a877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20K=C3=B6hn?= Date: Fri, 16 Jul 2021 09:54:38 +0200 Subject: [PATCH] Add option to skip exchange declaration --- consume.go | 27 +++++++++++++++------------ consume_options.go | 9 +++++++++ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/consume.go b/consume.go index ee393ff..37e828a 100644 --- a/consume.go +++ b/consume.go @@ -2,8 +2,9 @@ package rabbitmq import ( "fmt" - "github.com/streadway/amqp" "time" + + "github.com/streadway/amqp" ) // Consumer allows you to create and connect to queues for data consumption. @@ -188,17 +189,19 @@ func (consumer Consumer) startGoroutines( if exchange.Name == "" { return fmt.Errorf("binding to exchange but name not specified") } - err = consumer.chManager.channel.ExchangeDeclare( - exchange.Name, - exchange.Kind, - exchange.Durable, - exchange.AutoDelete, - exchange.Internal, - exchange.NoWait, - tableToAMQPTable(exchange.ExchangeArgs), - ) - if err != nil { - return err + if exchange.Declare { + err = consumer.chManager.channel.ExchangeDeclare( + exchange.Name, + exchange.Kind, + exchange.Durable, + exchange.AutoDelete, + exchange.Internal, + exchange.NoWait, + tableToAMQPTable(exchange.ExchangeArgs), + ) + if err != nil { + return err + } } for _, routingKey := range routingKeys { err = consumer.chManager.channel.QueueBind( diff --git a/consume_options.go b/consume_options.go index e8eb1bb..e0f8a3b 100644 --- a/consume_options.go +++ b/consume_options.go @@ -55,6 +55,7 @@ func getBindingExchangeOptionsOrSetDefault(options *ConsumeOptions) *BindingExch Internal: false, NoWait: false, ExchangeArgs: nil, + Declare: true, } } return options.BindingExchange @@ -70,6 +71,7 @@ type BindingExchangeOptions struct { Internal bool NoWait bool ExchangeArgs Table + Declare bool } // WithConsumeOptionsQueueDurable sets the queue to durable, which means it won't @@ -151,6 +153,13 @@ func WithConsumeOptionsBindingExchangeArgs(args Table) func(*ConsumeOptions) { } } +// WithConsumeOptionsBindingExchangeSkipDeclare returns a function that skips the declaration of the +// binding exchange. Use this setting if the exchange already exists and you don't need to declare +// it on consumer start. +func WithConsumeOptionsBindingExchangeSkipDeclare(options *ConsumeOptions) { + getBindingExchangeOptionsOrSetDefault(options).Declare = false +} + // WithConsumeOptionsBindingNoWait sets the bindings to nowait, which means if the queue can not be bound // the channel will not be closed with an error. func WithConsumeOptionsBindingNoWait(options *ConsumeOptions) {