-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Introduce DeduplicateMiddleware
#54141
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
Merged
Merged
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
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
51 changes: 51 additions & 0 deletions
51
src/Symfony/Component/Messenger/Middleware/DeduplicateMiddleware.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,51 @@ | ||
<?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\Middleware; | ||
|
||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Stamp\DeduplicateStamp; | ||
use Symfony\Component\Messenger\Stamp\ReceivedStamp; | ||
|
||
final class DeduplicateMiddleware implements MiddlewareInterface | ||
{ | ||
public function __construct(private LockFactory $lockFactory) | ||
{ | ||
} | ||
|
||
public function handle(Envelope $envelope, StackInterface $stack): Envelope | ||
{ | ||
if (!$stamp = $envelope->last(DeduplicateStamp::class)) { | ||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
|
||
if (!$envelope->last(ReceivedStamp::class)) { | ||
$lock = $this->lockFactory->createLockFromKey($stamp->getKey(), $stamp->getTtl(), autoRelease: false); | ||
|
||
if (!$lock->acquire()) { | ||
return $envelope; | ||
} | ||
} elseif ($stamp->onlyDeduplicateInQueue()) { | ||
$this->lockFactory->createLockFromKey($stamp->getKey())->release(); | ||
} | ||
|
||
try { | ||
$envelope = $stack->next()->handle($envelope, $stack); | ||
} finally { | ||
if ($envelope->last(ReceivedStamp::class) && !$stamp->onlyDeduplicateInQueue()) { | ||
$this->lockFactory->createLockFromKey($stamp->getKey())->release(); | ||
} | ||
} | ||
VincentLanglet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return $envelope; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Messenger/Stamp/DeduplicateStamp.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,42 @@ | ||
<?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\Stamp; | ||
|
||
use Symfony\Component\Lock\Key; | ||
|
||
final class DeduplicateStamp implements StampInterface | ||
{ | ||
private Key $key; | ||
|
||
public function __construct( | ||
string $key, | ||
private ?float $ttl = 300.0, | ||
private bool $onlyDeduplicateInQueue = false, | ||
) { | ||
$this->key = new Key($key); | ||
} | ||
|
||
public function onlyDeduplicateInQueue(): bool | ||
{ | ||
return $this->onlyDeduplicateInQueue; | ||
} | ||
|
||
public function getKey(): Key | ||
{ | ||
return $this->key; | ||
} | ||
|
||
public function getTtl(): ?float | ||
{ | ||
return $this->ttl; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/Symfony/Component/Messenger/Tests/Middleware/DeduplicateMiddlewareTest.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,68 @@ | ||
<?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\Tests\Middleware; | ||
|
||
use Symfony\Component\Lock\LockFactory; | ||
use Symfony\Component\Lock\Store\FlockStore; | ||
use Symfony\Component\Lock\Store\SemaphoreStore; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Middleware\DeduplicateMiddleware; | ||
use Symfony\Component\Messenger\Stamp\DeduplicateStamp; | ||
use Symfony\Component\Messenger\Stamp\ReceivedStamp; | ||
use Symfony\Component\Messenger\Test\Middleware\MiddlewareTestCase; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
|
||
final class DeduplicateMiddlewareTest extends MiddlewareTestCase | ||
{ | ||
public function testDeduplicateMiddlewareIgnoreIfMessageIsNotLockable() | ||
{ | ||
$message = new DummyMessage('Hello'); | ||
$envelope = new Envelope($message); | ||
|
||
$lockFactory = $this->createMock(LockFactory::class); | ||
$lockFactory->expects($this->never())->method('createLock'); | ||
|
||
$decorator = new DeduplicateMiddleware($lockFactory); | ||
|
||
$decorator->handle($envelope, $this->getStackMock(true)); | ||
} | ||
|
||
public function testDeduplicateMiddlewareIfMessageHasKey() | ||
{ | ||
$message = new DummyMessage('Hello'); | ||
$envelope = new Envelope($message, [new DeduplicateStamp('id')]); | ||
|
||
if (SemaphoreStore::isSupported()) { | ||
$store = new SemaphoreStore(); | ||
} else { | ||
$store = new FlockStore(); | ||
} | ||
|
||
$decorator = new DeduplicateMiddleware(new LockFactory($store)); | ||
|
||
$envelope = $decorator->handle($envelope, $this->getStackMock(true)); | ||
$this->assertNotNull($envelope->last(DeduplicateStamp::class)); | ||
|
||
$message2 = new DummyMessage('Hello'); | ||
$envelope2 = new Envelope($message2, [new DeduplicateStamp('id')]); | ||
|
||
$decorator->handle($envelope2, $this->getStackMock(false)); | ||
|
||
// Simulate receiving the first message | ||
$envelope = $envelope->with(new ReceivedStamp('transport')); | ||
$decorator->handle($envelope, $this->getStackMock(true)); | ||
|
||
$message3 = new DummyMessage('Hello'); | ||
$envelope3 = new Envelope($message3, [new DeduplicateStamp('id')]); | ||
$decorator->handle($envelope3, $this->getStackMock(true)); | ||
} | ||
} |
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
Oops, something went wrong.
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.
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.
update the test deps next time to avoid these exra conditions :)
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.
it's all maintenance
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.
cc @fabpot
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.
There’s not really a need to forbid using FrameworkBundle with Messenger < 7.3. But that would be the consequence if we cannot test it.
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 dont like the if/else condition based on class presence generally
Uh oh!
There was an error while loading. Please reload this page.
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.
7.2 is tested without class X
7.3 is tested with class X
it's that simple to me.
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.
That would complicate things. Either we would require people to update FrameworkBundle and components at the same time which would make updating harder. Or we would not be able to test the bundle with the lowest deps which would not allow us to detect compatibility issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
generally yes, it that prevents if/else flows in your codebase. We should prefer branches then.
updating many sf packages from 7.2 to 7.x is not an issue.