Skip to content
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

[Messenger] [Amqp] Handle AMQPConnectionException when publishing a message. #54167

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,73 @@ public function testItCanBeConstructedWithTLSOptionsAndNonTLSDsn()
);
}

public function testItCanRetryPublishWhenAMQPConnectionExceptionIsThrown()
{
$factory = new TestAmqpFactory(
$amqpConnection = $this->createMock(\AMQPConnection::class),
$amqpChannel = $this->createMock(\AMQPChannel::class),
$amqpQueue = $this->createMock(\AMQPQueue::class),
$amqpExchange = $this->createMock(\AMQPExchange::class)
);

$amqpExchange->expects($this->exactly(2))
->method('publish')
->willReturnOnConsecutiveCalls(
$this->throwException(new \AMQPConnectionException('a socket error occurred')),
null
);

$connection = Connection::fromDsn('amqp://localhost', [], $factory);
$connection->publish('body');
}

public function testItCanRetryPublishWithDelayWhenAMQPConnectionExceptionIsThrown()
{
$factory = new TestAmqpFactory(
$amqpConnection = $this->createMock(\AMQPConnection::class),
$amqpChannel = $this->createMock(\AMQPChannel::class),
$amqpQueue = $this->createMock(\AMQPQueue::class),
$amqpExchange = $this->createMock(\AMQPExchange::class)
);

$amqpExchange->expects($this->exactly(2))
->method('publish')
->willReturnOnConsecutiveCalls(
$this->throwException(new \AMQPConnectionException('a socket error occurred')),
null
);

$connection = Connection::fromDsn('amqp://localhost', [], $factory);
$connection->publish('body', [], 5000);
}

public function testItWillRetryMaxThreeTimesWhenAMQPConnectionExceptionIsThrown()
{
$factory = new TestAmqpFactory(
$amqpConnection = $this->createMock(\AMQPConnection::class),
$amqpChannel = $this->createMock(\AMQPChannel::class),
$amqpQueue = $this->createMock(\AMQPQueue::class),
$amqpExchange = $this->createMock(\AMQPExchange::class)
);

$exception = new \AMQPConnectionException('a socket error occurred');

$amqpExchange->expects($this->exactly(4))
->method('publish')
->willReturnOnConsecutiveCalls(
$this->throwException($exception),
$this->throwException($exception),
$this->throwException($exception),
$this->throwException($exception),
);

self::expectException($exception::class);
self::expectExceptionMessage($exception->getMessage());

$connection = Connection::fromDsn('amqp://localhost', [], $factory);
$connection->publish('body');
}

private function createDelayOrRetryConnection(\AMQPExchange $delayExchange, string $deadLetterExchangeName, string $delayQueueName): Connection
{
$amqpConnection = $this->createMock(\AMQPConnection::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,21 @@ public function publish(string $body, array $headers = [], int $delayInMs = 0, ?
$this->setupExchangeAndQueues(); // also setup normal exchange for delayed messages so delay queue can DLX messages to it
}

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

return;
}
return;
}

$this->publishOnExchange(
$this->exchange(),
$body,
$this->getRoutingKeyForMessage($amqpStamp),
$headers,
$amqpStamp
);
$this->publishOnExchange(
$this->exchange(),
$body,
$this->getRoutingKeyForMessage($amqpStamp),
$headers,
$amqpStamp
);
});
}

/**
Expand Down Expand Up @@ -545,11 +547,16 @@ public function exchange(): \AMQPExchange
private function clearWhenDisconnected(): void
{
if (!$this->channel()->isConnected()) {
unset($this->amqpChannel, $this->amqpExchange, $this->amqpDelayExchange);
$this->amqpQueues = [];
$this->clear();
}
}

private function clear(): void
{
unset($this->amqpChannel, $this->amqpExchange, $this->amqpDelayExchange);
$this->amqpQueues = [];
}

private function getDefaultPublishRoutingKey(): ?string
{
return $this->exchangeOptions['default_publish_routing_key'] ?? null;
Expand All @@ -566,4 +573,23 @@ private function getRoutingKeyForMessage(?AmqpStamp $amqpStamp): ?string
{
return $amqpStamp?->getRoutingKey() ?? $this->getDefaultPublishRoutingKey();
}

private function withConnectionExceptionRetry(callable $callable): void
{
$maxRetries = 3;
$retries = 0;

retry:
try {
$callable();
} catch (\AMQPConnectionException $e) {
if (++$retries <= $maxRetries) {
$this->clear();

goto retry;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cost years of public bashing, but I guess we can afford it 🥲

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💘 goto, no shame :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hehehe I like it too. I feel like imo it reads better than a loop specifically for retry logic.

}

throw $e;
}
}
}