Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18912b6

Browse files
committed
[Messenger] backport #54167: retry AMQPConnectionException on publish
Ports the upstream Symfony 5.4/6.4 fix for symfony/symfony#48241 into this 4.4 fork. Wraps Connection::publish() in withConnectionExceptionRetry() which catches \AMQPConnectionException, drops cached channel/queues/exchanges via clear(), and retries up to 3 times with no backoff. Fixes intermittent AMQPS publish failures against AWS AmazonMQ caused by AWS NLB's 350s idle TCP timeout silently killing connections — ext-amqp's isConnected() doesn't detect the dead socket so the next publish surfaces as "Library error: a SSL error occurred". Upstream PR: symfony/symfony#54167 Upstream issue: symfony/symfony#48241
1 parent f620068 commit 18912b6

1 file changed

Lines changed: 44 additions & 18 deletions

File tree

Transport/AmqpExt/Connection.php

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -187,23 +187,25 @@ public function publish(string $body, array $headers = [], int $delayInMs = 0, A
187187
{
188188
$this->clearWhenDisconnected();
189189

190-
if (0 !== $delayInMs) {
191-
$this->publishWithDelay($body, $headers, $delayInMs, $amqpStamp);
190+
$this->withConnectionExceptionRetry(function () use ($body, $headers, $delayInMs, $amqpStamp) {
191+
if (0 !== $delayInMs) {
192+
$this->publishWithDelay($body, $headers, $delayInMs, $amqpStamp);
192193

193-
return;
194-
}
194+
return;
195+
}
195196

196-
if ($this->shouldSetup()) {
197-
$this->setupExchangeAndQueues();
198-
}
197+
if ($this->shouldSetup()) {
198+
$this->setupExchangeAndQueues();
199+
}
199200

200-
$this->publishOnExchange(
201-
$this->exchange(),
202-
$body,
203-
$this->getRoutingKeyForMessage($amqpStamp),
204-
$headers,
205-
$amqpStamp
206-
);
201+
$this->publishOnExchange(
202+
$this->exchange(),
203+
$body,
204+
$this->getRoutingKeyForMessage($amqpStamp),
205+
$headers,
206+
$amqpStamp
207+
);
208+
});
207209
}
208210

209211
/**
@@ -442,10 +444,34 @@ public function exchange(): \AMQPExchange
442444
private function clearWhenDisconnected(): void
443445
{
444446
if (!$this->channel()->isConnected()) {
445-
$this->amqpChannel = null;
446-
$this->amqpQueues = [];
447-
$this->amqpExchange = null;
448-
$this->amqpDelayExchange = null;
447+
$this->clear();
448+
}
449+
}
450+
451+
private function clear(): void
452+
{
453+
$this->amqpChannel = null;
454+
$this->amqpQueues = [];
455+
$this->amqpExchange = null;
456+
$this->amqpDelayExchange = null;
457+
}
458+
459+
private function withConnectionExceptionRetry(callable $callable): void
460+
{
461+
$maxRetries = 3;
462+
$retries = 0;
463+
464+
retry:
465+
try {
466+
$callable();
467+
} catch (\AMQPConnectionException $e) {
468+
if (++$retries <= $maxRetries) {
469+
$this->clear();
470+
471+
goto retry;
472+
}
473+
474+
throw $e;
449475
}
450476
}
451477

0 commit comments

Comments
 (0)