gorabbit icon indicating copy to clipboard operation
gorabbit copied to clipboard

consumer already exists

Open Mixnosha opened this issue 8 months ago • 2 comments

This method is called when creating a new consumer

Why don't we check the name of the new consumer with the old one? With this approach, you can't create multiple consumers for one queue

func (a *amqpConnection) registerConsumer(consumer MessageConsumer) error {
	for _, channel := range a.channels {
                // maybe check channel.consumer.Name != consumer.Name
		if channel.consumer != nil && channel.consumer.Queue == consumer.Queue {
			err := errConsumerAlreadyExists

			a.logger.Error(err, "Could not register consumer", logField{Key: "consumer", Value: consumer.Name})

			return err
		}
	}
 ...

Mixnosha avatar May 19 '25 12:05 Mixnosha

That's a good point, we could even check for both in that case

func (a *amqpConnection) registerConsumer(consumer MessageConsumer) error {
	for _, channel := range a.channels {
                if channel.consumer != nil && (channel.consumer.Name == consumer.Name || channel.consumer.Queue == consumer.Queue) {
			err := errConsumerAlreadyExists

			a.logger.Error(err, "Could not register consumer", logField{Key: "consumer", Value: consumer.Name})

			return err
		}
	}
 ...

m3talux avatar May 29 '25 19:05 m3talux

if channel.consumer != nil && channel.consumer.Name == consumer.Name && channel.consumer.Queue ==consumer.Queue -> return errConsumerAlreadyExists

It seems that such an expression will be correct only if the name and the queue match, return an error

Mixnosha avatar May 29 '25 20:05 Mixnosha