watermill icon indicating copy to clipboard operation
watermill copied to clipboard

[watermill-amqp] It is not possible to generate queue bindings from topic

Open devinmarder opened this issue 3 years ago • 0 comments

It does not seem like it is possible to generate queue bindings from the topic

The amqp.Config allows providing a function to generate a queue binding

QueueBind: QueueBindConfig{
	GenerateRoutingKey: func(topic string) string {
		return topic
	},
},

This seems to imply that the topic that is being subscribed to will be bound to the queue with the generated binding key, but this doesn't actually seem possible, even with a custom implementation of the topology builder.

The default topology builder passes the generated queue to that function, not the topic. The TopologyBuilder interface does not accept the topic as an argument and the binding key is not being generated beforehand and passed in like the exchangeName and queueName:

type TopologyBuilder interface {
	BuildTopology(channel *amqp.Channel, queueName string, exchangeName string, config Config, logger watermill.LoggerAdapter) error
	ExchangeDeclare(channel *amqp.Channel, exchangeName string, config Config) error
}

I see a few options here:

  • add the topic to the buildTopology function signature (and even remove the queue and exchange parameters since they can already be generated from the topic and the Config
  • precompute the binding key and pass it in like the other parameters

This would allow for more customizable and dynamic topologies, utilizing wildcard matching on topic exchanges as well as more intuitive configurations.

An example would be to have an exchange which accepts topics as the routing key and then subscribers can subscribe to that on their own queue, and can even subscribe to a subset of those messages using wildcards. This seems like an example of how rabbitmq/amqp is typically used, but doesn't seem possible with the current watermill amqp implementation.

devinmarder avatar Oct 29 '22 09:10 devinmarder