Skip to content

Commit f78f932

Browse files
committed
feature #53892 [Messenger][AMQP] Automatically reconnect on connection loss (ostrolucky)
This PR was merged into the 7.1 branch. Discussion ---------- [Messenger][AMQP] Automatically reconnect on connection loss | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? |no | New feature? | no | Deprecations? | no | Issues | | License | MIT When using Rabbitmq in cluster, there is a common need of having to upgrade the nodes, while keeping the existing connections. The way this is normally done is by putting nodes in cluster to `maintenance mode`, ensuring cluster is healthy at all times. However, symfony/messenger nor php-amqp handle this use case at the moment. What happens instead is that exception when getting the message is thrown, worker crashes, error is logged and process manager has to respin it. This all happens without having a way in user space to handle this case better. Messenger's retry mechanism does not work here, because that one kicks in only when exception is thrown in handlers. Concrete exception is following: > [AMQPConnectionException (320)] > Server connection error: 320, message: CONNECTION_FORCED - Node was put into maintenance mode What I'm proposing in this PR is that if connection error is detected _try to reconnect once_ before throwing exception. That should handle the outlined case. This goes line in line with recommendation from AWS's support we got: > kindly ensure that the client connecting to the broker attempts a retry in case the above error message is observed during a maintenance window. In RabbitMQ cluster deployments, the nodes are restarted one-by-one, meaning at least two nodes will be up and running at all times. Even if a connection is severed, a connection retry will result in the other nodes accepting the connection, and the clients can keep using the broker. I've also reported issue at php-amqplib/php-amqplib#1161 with hope that this could be fixed at some point upstream, but I don't give it a big chance. Commits ------- 056b4a5 [Messenger] AMQP:Automatically reconnect on connection loss
2 parents 17c5508 + 056b4a5 commit f78f932

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/Symfony/Component/Messenger/Bridge/Amqp/Transport/AmqpReceiver.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ private function getEnvelope(string $queueName): iterable
5252
{
5353
try {
5454
$amqpEnvelope = $this->connection->get($queueName);
55+
} catch (\AMQPConnectionException) {
56+
// Try to reconnect once to accommodate need for one of the nodes in cluster needing to stop serving the
57+
// traffic. This may happen for example when one of the nodes in cluster is going into maintenance node.
58+
// see https://github.com/php-amqplib/php-amqplib/issues/1161
59+
try {
60+
$this->connection->queue($queueName)->getConnection()->reconnect();
61+
$amqpEnvelope = $this->connection->get($queueName);
62+
} catch (\AMQPException $exception) {
63+
throw new TransportException($exception->getMessage(), 0, $exception);
64+
}
5565
} catch (\AMQPException $exception) {
5666
throw new TransportException($exception->getMessage(), 0, $exception);
5767
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy