-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Messenger] Move RejectRedeliveredMessageMiddleware
to AMQP Package
#41749
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
Closed
Warxcell
wants to merge
2
commits into
symfony:6.4
from
Warxcell:fix_reject_redelivered_message_middleware
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...Symfony/Component/Messenger/Bridge/Amqp/Middleware/RejectRedeliveredMessageMiddleware.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Bridge\Amqp\Middleware; | ||
|
||
use Symfony\Component\Messenger\Bridge\Amqp\Transport\AmqpReceivedStamp; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\RejectRedeliveredMessageException; | ||
use Symfony\Component\Messenger\Middleware\MiddlewareInterface; | ||
use Symfony\Component\Messenger\Middleware\StackInterface; | ||
use Symfony\Component\Messenger\Transport\AmqpExt\AmqpReceivedStamp as LegacyAmqpReceivedStamp; | ||
|
||
/** | ||
* Middleware that throws a RejectRedeliveredMessageException when a message is detected that has been redelivered by AMQP. | ||
* | ||
* The middleware runs before the HandleMessageMiddleware and prevents redelivered messages from being handled directly. | ||
* The thrown exception is caught by the worker and will trigger the retry logic according to the retry strategy. | ||
* | ||
* AMQP redelivers messages when they do not get acknowledged or rejected. This can happen when the connection times out | ||
* or an exception is thrown before acknowledging or rejecting. When such errors happen again while handling the | ||
* redelivered message, the message would get redelivered again and again. The purpose of this middleware is to prevent | ||
* infinite redelivery loops and to unblock the queue by republishing the redelivered messages as retries with a retry | ||
* limit and potential delay. | ||
* | ||
* @author Tobias Schultze <http://tobion.de> | ||
*/ | ||
class RejectRedeliveredMessageMiddleware implements MiddlewareInterface | ||
{ | ||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
$amqpReceivedStamp = $envelope->last(AmqpReceivedStamp::class); | ||
if ($amqpReceivedStamp instanceof AmqpReceivedStamp && $amqpReceivedStamp->getAmqpEnvelope()->isRedelivery()) { | ||
throw new RejectRedeliveredMessageException('Redelivered message from AMQP detected that will be rejected and trigger the retry logic.'); | ||
} | ||
|
||
// Legacy code to support symfony/messenger < 5.1 | ||
$amqpReceivedStamp = $envelope->last(LegacyAmqpReceivedStamp::class); | ||
if ($amqpReceivedStamp instanceof LegacyAmqpReceivedStamp && $amqpReceivedStamp->getAmqpEnvelope()->isRedelivery()) { | ||
throw new RejectRedeliveredMessageException('Redelivered message from AMQP detected that will be rejected and trigger the retry logic.'); | ||
} | ||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.