This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(messenger): Add Symfony Messenger integration
Asynchronously dispatch messages using Symfony Messanger API and Swoole inter-process communication (Swoole task) without serialization. Notice: Symfony messenger `messenger:consume-messages` command is not supported. Dispatched messages are handled in task worker processes of Swoole server. Usage: 1. Install `symfony/messenger` package via composer 2. Enable task workers inside configuration Example: ```yaml # config/packages/swoole.yaml swoole: http_server: ... settings: ... task_worker_count: auto ``` 3. Configure swoole messenger transport Example: ```yaml # config/packages/messenger.yaml framework: messenger: transports: swoole: swoole://task routing: '*': swoole ``` 4. (optional) Follow official [symfony messenger guide](https://symfony.com/doc/current/messenger.html) to define message object and its handler Relates to #4
- Loading branch information
Showing
24 changed files
with
792 additions
and
103 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 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 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
13 changes: 13 additions & 0 deletions
13
src/Bridge/Symfony/Messenger/Exception/ReceiverNotAvailableException.php
This file contains 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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Messenger\Exception; | ||
|
||
final class ReceiverNotAvailableException extends \RuntimeException | ||
{ | ||
public static function make(): self | ||
{ | ||
throw new self('Swoole Server Task transport does not support Receiver. Messages sent via Swoole Server Task transport are dispatched inside task worker processes.'); | ||
} | ||
} |
This file contains 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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Messenger; | ||
|
||
use K911\Swoole\Bridge\Symfony\Messenger\Exception\ReceiverNotAvailableException; | ||
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; | ||
|
||
final class SwooleServerTaskReceiver implements ReceiverInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function receive(callable $handler): void | ||
{ | ||
// noop | ||
throw ReceiverNotAvailableException::make(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function stop(): void | ||
{ | ||
// noop | ||
} | ||
} |
This file contains 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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Messenger; | ||
|
||
use K911\Swoole\Server\HttpServer; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Transport\Sender\SenderInterface; | ||
|
||
final class SwooleServerTaskSender implements SenderInterface | ||
{ | ||
private $httpServer; | ||
|
||
public function __construct(HttpServer $httpServer) | ||
{ | ||
$this->httpServer = $httpServer; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function send(Envelope $envelope): Envelope | ||
{ | ||
$this->httpServer->dispatchTask($envelope); | ||
|
||
return $envelope; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Bridge/Symfony/Messenger/SwooleServerTaskTransport.php
This file contains 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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Messenger; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Transport\TransportInterface; | ||
|
||
final class SwooleServerTaskTransport implements TransportInterface | ||
{ | ||
private $receiver; | ||
private $sender; | ||
|
||
public function __construct(SwooleServerTaskReceiver $receiver, SwooleServerTaskSender $sender) | ||
{ | ||
$this->receiver = $receiver; | ||
$this->sender = $sender; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function receive(callable $handler): void | ||
{ | ||
dump($handler); | ||
$this->receiver->receive($handler); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function stop(): void | ||
{ | ||
$this->receiver->stop(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function send(Envelope $envelope): Envelope | ||
{ | ||
return $this->sender->send($envelope); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Bridge/Symfony/Messenger/SwooleServerTaskTransportFactory.php
This file contains 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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Messenger; | ||
|
||
use K911\Swoole\Server\HttpServer; | ||
use Symfony\Component\Messenger\Transport\TransportFactoryInterface; | ||
use Symfony\Component\Messenger\Transport\TransportInterface; | ||
|
||
final class SwooleServerTaskTransportFactory implements TransportFactoryInterface | ||
{ | ||
private $server; | ||
|
||
public function __construct(HttpServer $server) | ||
{ | ||
$this->server = $server; | ||
} | ||
|
||
public function createTransport(string $dsn, array $options): TransportInterface | ||
{ | ||
return new SwooleServerTaskTransport( | ||
new SwooleServerTaskReceiver(), | ||
new SwooleServerTaskSender($this->server) | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(string $dsn, array $options): bool | ||
{ | ||
return 0 === \mb_strpos($dsn, 'swoole://task'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/Bridge/Symfony/Messenger/SwooleServerTaskTransportHandler.php
This file contains 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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Bridge\Symfony\Messenger; | ||
|
||
use Assert\Assertion; | ||
use K911\Swoole\Server\TaskHandler\TaskHandlerInterface; | ||
use Swoole\Server; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\MessageBusInterface; | ||
use Symfony\Component\Messenger\Stamp\ReceivedStamp; | ||
|
||
final class SwooleServerTaskTransportHandler implements TaskHandlerInterface | ||
{ | ||
private $bus; | ||
private $decorated; | ||
|
||
public function __construct(MessageBusInterface $bus, ?TaskHandlerInterface $decorated = null) | ||
{ | ||
$this->bus = $bus; | ||
$this->decorated = $decorated; | ||
} | ||
|
||
public function handle(Server $server, int $taskId, int $fromId, $data): void | ||
{ | ||
Assertion::isInstanceOf($data, Envelope::class); | ||
/* @var $data Envelope */ | ||
$this->bus->dispatch($data->with(new ReceivedStamp())); | ||
|
||
if ($this->decorated instanceof TaskHandlerInterface) { | ||
$this->decorated->handle($server, $taskId, $fromId, $data); | ||
} | ||
} | ||
} |
This file contains 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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Server\Configurator; | ||
|
||
use K911\Swoole\Server\HttpServerConfiguration; | ||
use K911\Swoole\Server\TaskHandler\TaskFinishedHandlerInterface; | ||
use Swoole\Http\Server; | ||
|
||
final class WithTaskFinishedHandler implements ConfiguratorInterface | ||
{ | ||
private $handler; | ||
private $configuration; | ||
|
||
public function __construct(TaskFinishedHandlerInterface $handler, HttpServerConfiguration $configuration) | ||
{ | ||
$this->handler = $handler; | ||
$this->configuration = $configuration; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(Server $server): void | ||
{ | ||
if ($this->configuration->getTaskWorkerCount() > 0) { | ||
$server->on('finish', [$this->handler, 'handle']); | ||
} | ||
} | ||
} |
This file contains 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,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace K911\Swoole\Server\Configurator; | ||
|
||
use K911\Swoole\Server\HttpServerConfiguration; | ||
use K911\Swoole\Server\TaskHandler\TaskHandlerInterface; | ||
use Swoole\Http\Server; | ||
|
||
final class WithTaskHandler implements ConfiguratorInterface | ||
{ | ||
private $handler; | ||
private $configuration; | ||
|
||
public function __construct(TaskHandlerInterface $handler, HttpServerConfiguration $configuration) | ||
{ | ||
$this->handler = $handler; | ||
$this->configuration = $configuration; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configure(Server $server): void | ||
{ | ||
if ($this->configuration->getTaskWorkerCount() > 0) { | ||
$server->on('task', [$this->handler, 'handle']); | ||
} | ||
} | ||
} |
This file contains 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.