-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] allow processing messages in batches #43354
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
[Messenger] allow processing messages in batches
- Loading branch information
commit 81e52b2f4edb4cdf3afc3110ad7f7b3b49035242
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?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\Handler; | ||
|
||
use Symfony\Component\Messenger\Exception\LogicException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class Acknowledger | ||
{ | ||
private $handlerClass; | ||
private $ack; | ||
private $error = null; | ||
private $result = null; | ||
|
||
/** | ||
* @param null|\Closure(\Throwable|null, mixed):void $ack | ||
*/ | ||
public function __construct(string $handlerClass, \Closure $ack = null) | ||
{ | ||
$this->handlerClass = $handlerClass; | ||
$this->ack = $ack ?? static function () {}; | ||
} | ||
|
||
/** | ||
* @param mixed $result | ||
*/ | ||
public function ack($result = null): void | ||
{ | ||
$this->doAck(null, $result); | ||
} | ||
|
||
public function nack(\Throwable $error): void | ||
{ | ||
$this->doAck($error); | ||
} | ||
|
||
public function getError(): ?\Throwable | ||
{ | ||
return $this->error; | ||
} | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getResult() | ||
{ | ||
return $this->result; | ||
} | ||
|
||
public function isAcknowledged(): bool | ||
{ | ||
return null === $this->ack; | ||
} | ||
|
||
public function __destruct() | ||
{ | ||
if ($this->ack instanceof \Closure) { | ||
throw new LogicException(sprintf('The acknowledger was not called by the "%s" batch handler.', $this->handlerClass)); | ||
} | ||
} | ||
|
||
private function doAck(\Throwable $e = null, $result = null): void | ||
{ | ||
if (!$ack = $this->ack) { | ||
throw new LogicException(sprintf('The acknowledger cannot be called twice by the "%s" batch handler.', $this->handlerClass)); | ||
} | ||
$this->ack = null; | ||
$this->error = $e; | ||
$this->result = $result; | ||
$ack($e, $result); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/Symfony/Component/Messenger/Handler/BatchHandlerInterface.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,34 @@ | ||
<?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\Handler; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
interface BatchHandlerInterface | ||
{ | ||
/** | ||
* @param Acknowledger|null $ack The function to call to ack/nack the $message. | ||
* The message should be handled synchronously when null. | ||
* | ||
* @return mixed The number of pending messages in the batch if $ack is not null, | ||
* the result from handling the message otherwise | ||
*/ | ||
//public function __invoke(object $message, Acknowledger $ack = null): mixed; | ||
|
||
/** | ||
* Flushes any pending buffers. | ||
* | ||
* @param bool $force Whether flushing is required; it can be skipped if not | ||
*/ | ||
public function flush(bool $force): void; | ||
} |
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Messenger/Handler/BatchHandlerTrait.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,75 @@ | ||
<?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\Handler; | ||
|
||
use Symfony\Component\Messenger\Exception\LogicException; | ||
|
||
/** | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
trait BatchHandlerTrait | ||
{ | ||
private $jobs = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function flush(bool $force): void | ||
{ | ||
if ($jobs = $this->jobs) { | ||
$this->jobs = []; | ||
$this->process($jobs); | ||
} | ||
} | ||
lyrixx marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @param Acknowledger|null $ack The function to call to ack/nack the $message. | ||
* The message should be handled synchronously when null. | ||
* | ||
* @return mixed The number of pending messages in the batch if $ack is not null, | ||
* the result from handling the message otherwise | ||
*/ | ||
private function handle(object $message, ?Acknowledger $ack) | ||
{ | ||
if (null === $ack) { | ||
$ack = new Acknowledger(get_debug_type($this)); | ||
$this->jobs[] = [$message, $ack]; | ||
$this->flush(true); | ||
|
||
return $ack->getResult(); | ||
} | ||
|
||
$this->jobs[] = [$message, $ack]; | ||
if (!$this->shouldFlush()) { | ||
return \count($this->jobs); | ||
} | ||
|
||
$this->flush(true); | ||
|
||
return 0; | ||
} | ||
|
||
private function shouldFlush(): bool | ||
{ | ||
return 10 <= \count($this->jobs); | ||
} | ||
|
||
/** | ||
* Completes the jobs in the list. | ||
* | ||
* @list<array{0: object, 1: Acknowledger}> $jobs A list of pairs of messages and their corresponding acknowledgers | ||
*/ | ||
private function process(array $jobs): void | ||
{ | ||
throw new LogicException(sprintf('"%s" should implement abstract method "process()".', get_debug_type($this))); | ||
} | ||
} |
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
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.