-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Messenger] support for multiple bindings on the same queue #37722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dd55e54
be9e781
c2b77ce
32de160
254ff41
76426b4
52a834b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,9 +60,18 @@ class Connection | |
]; | ||
|
||
private const AVAILABLE_QUEUE_OPTIONS = [ | ||
'bindings', | ||
'flags', | ||
'arguments', | ||
]; | ||
|
||
private const ORIGINAL_BINDING_KEYS = [ | ||
'binding_keys', | ||
'binding_arguments', | ||
'flags', | ||
]; | ||
|
||
private const AVAILABLE_BINDINGS_OPTIONS = [ | ||
'key', | ||
'arguments', | ||
]; | ||
|
||
|
@@ -131,8 +140,11 @@ public function __construct(array $connectionOptions, array $exchangeOptions, ar | |
* * connect_timeout: Connection timeout. Note: 0 or greater seconds. May be fractional. | ||
* * confirm_timeout: Timeout in seconds for confirmation, if none specified transport will not wait for message confirmation. Note: 0 or greater seconds. May be fractional. | ||
* * queues[name]: An array of queues, keyed by the name | ||
* * binding_keys: The binding keys (if any) to bind to this queue | ||
* * binding_arguments: Arguments to be used while binding the queue. | ||
* * binding_keys: The binding keys (if any) to bind to this queue (Usage is deprecated. See 'bindings') | ||
* * binding_arguments: Arguments to be used while binding the queue. (Usage is deprecated. See 'bindings') | ||
* * bindings[name]: An array of bindings for this queue, keyed by the name | ||
* * key: The binding key (if any) to bind to this queue | ||
* * arguments: An array of arguments to be used while binding the queue. | ||
* * flags: Queue flags (Default: AMQP_DURABLE) | ||
* * arguments: Extra arguments | ||
* * exchange: | ||
|
@@ -237,8 +249,18 @@ private static function validateOptions(array $options): void | |
continue; | ||
} | ||
|
||
if (0 < \count($invalidQueueOptions = array_diff(array_keys($queue), self::AVAILABLE_QUEUE_OPTIONS))) { | ||
if (0 < \count($invalidQueueOptions = array_diff(array_keys($queue), self::AVAILABLE_QUEUE_OPTIONS, self::ORIGINAL_BINDING_KEYS))) { | ||
trigger_deprecation('symfony/messenger', '5.1', 'Invalid queue option(s) "%s" passed to the AMQP Messenger transport. Passing invalid queue options is deprecated.', implode('", "', $invalidQueueOptions)); | ||
} elseif (0 < \count($invalidQueueOptions = array_diff(array_keys($queue), self::AVAILABLE_QUEUE_OPTIONS))) { | ||
trigger_deprecation('symfony/messenger', '5.2', 'Deprecated queue option(s) "%s" passed to the AMQP Messenger transport. The "bindings" option should be used rather than "binding_keys" and "binding_arguments".', implode('", "', $invalidQueueOptions)); | ||
} | ||
|
||
if (\is_array($queue['bindings'] ?? false)) { | ||
foreach ($queue['bindings'] as $title => $individualBinding) { | ||
if (0 < \count($invalidBindingsOptions = array_diff(array_keys($individualBinding), self::AVAILABLE_BINDINGS_OPTIONS))) { | ||
throw new \InvalidArgumentException(sprintf('Invalid bindings option(s) "%s" passed to the AMQP Messenger transport in "%s". Each "bindings" option only accepts "key" and "arguments"', implode('", "', $invalidBindingsOptions), $title)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -461,6 +483,12 @@ private function setupExchangeAndQueues(): void | |
|
||
foreach ($this->queuesOptions as $queueName => $queueConfig) { | ||
$this->queue($queueName)->declareQueue(); | ||
foreach ($queueConfig['bindings'] ?? [] as $binding) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the example in the PR description, you show using an array key under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure there is or should be a correct usage. The original intent was that these bindings could have a developer/human friendly name. I have found that using these names for the bindings is useful when we have more than a dozen bindings. In my projects, all of these bindings would ideally have a name. I do not think I would want to force another developer to do this generally though. I'm okay with any suggestions here though. |
||
$this->queue($queueName)->bind($this->exchangeOptions['name'], $binding['key'] ?? null, $binding['arguments'] ?? []); | ||
} | ||
if (isset($queueConfig['bindings']) && empty($queueConfig['binding_keys'])) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this isn't needed? Because if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The '?? [null]' in the next line will ensure that bind is called at least once with a null $bindingKey if 'binding_keys' is empty. This would have the effect of binding all messages from the exchange to the queue. |
||
continue; | ||
} | ||
foreach ($queueConfig['binding_keys'] ?? [null] as $bindingKey) { | ||
$this->queue($queueName)->bind($this->exchangeOptions['name'], $bindingKey, $queueConfig['binding_arguments'] ?? []); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is quite right. In 5.1, we ADDED validation of the options in general. In 5.2, you're deprecating
bindings
. So basically, if you passbindings
, we should say thatbindings
is deprecated in 5.2. If we pass any other invalid option, it would use the existing deprecation message.Also, could we add validation that
bindings
only containskey
andarguments
keys?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed this and added validation on each individual binding. Thanks!