From 0dfadcbb4a7b5b6854154357bed563b023d343ea Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 14 Jul 2019 10:46:31 +0200 Subject: [PATCH] Added package to delegate the execution of event listeners to a swoole task worker --- composer.json | 1 + config/autoload/swoole.global.php | 5 +++++ config/config.php | 2 ++ module/Core/config/event_dispatcher.config.php | 6 ++++++ module/Core/src/EventDispatcher/ShortUrlVisited.php | 9 ++++++++- module/Core/src/Visit/Model/UnknownVisitLocation.php | 9 +-------- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index ba72716d..a93106c5 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,7 @@ "mikehaertl/phpwkhtmltopdf": "^2.2", "monolog/monolog": "^1.21", "phly/phly-event-dispatcher": "^1.0", + "phly/phly-swoole-taskworker": "^1.1", "shlinkio/shlink-installer": "^1.1", "symfony/console": "^4.2", "symfony/filesystem": "^4.2", diff --git a/config/autoload/swoole.global.php b/config/autoload/swoole.global.php index 5f82f0bc..11a0304f 100644 --- a/config/autoload/swoole.global.php +++ b/config/autoload/swoole.global.php @@ -9,6 +9,11 @@ return [ 'swoole-http-server' => [ 'host' => '0.0.0.0', 'process-name' => 'shlink', + + 'options' => [ + 'worker_num' => 16, + 'task_worker_num' => 16, + ], ], ], diff --git a/config/config.php b/config/config.php index 4ad8e8bb..48f638af 100644 --- a/config/config.php +++ b/config/config.php @@ -5,6 +5,7 @@ namespace Shlinkio\Shlink; use Acelaya\ExpressiveErrorHandler; use Phly\EventDispatcher; +use Phly\Swoole\TaskWorker; use Zend\ConfigAggregator; use Zend\Expressive; @@ -18,6 +19,7 @@ return (new ConfigAggregator\ConfigAggregator([ Expressive\Swoole\ConfigProvider::class, ExpressiveErrorHandler\ConfigProvider::class, EventDispatcher\ConfigProvider::class, + TaskWorker\ConfigProvider::class, Common\ConfigProvider::class, Core\ConfigProvider::class, CLI\ConfigProvider::class, diff --git a/module/Core/config/event_dispatcher.config.php b/module/Core/config/event_dispatcher.config.php index 34ef9e78..406e574f 100644 --- a/module/Core/config/event_dispatcher.config.php +++ b/module/Core/config/event_dispatcher.config.php @@ -3,6 +3,7 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core; +use Phly\Swoole\TaskWorker\DeferredListenerDelegator; use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; @@ -18,6 +19,11 @@ return [ 'factories' => [ EventDispatcher\LocateShortUrlVisit::class => ConfigAbstractFactory::class, ], + 'delegators' => [ + EventDispatcher\LocateShortUrlVisit::class => [ + DeferredListenerDelegator::class, + ], + ], ], ConfigAbstractFactory::class => [ diff --git a/module/Core/src/EventDispatcher/ShortUrlVisited.php b/module/Core/src/EventDispatcher/ShortUrlVisited.php index 7874bbc6..0984f3e9 100644 --- a/module/Core/src/EventDispatcher/ShortUrlVisited.php +++ b/module/Core/src/EventDispatcher/ShortUrlVisited.php @@ -3,7 +3,9 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\EventDispatcher; -final class ShortUrlVisited +use JsonSerializable; + +final class ShortUrlVisited implements JsonSerializable { /** @var string */ private $visitId; @@ -17,4 +19,9 @@ final class ShortUrlVisited { return $this->visitId; } + + public function jsonSerialize(): array + { + return ['visitId' => $this->visitId]; + } } diff --git a/module/Core/src/Visit/Model/UnknownVisitLocation.php b/module/Core/src/Visit/Model/UnknownVisitLocation.php index 118e38bd..1cfbe4b1 100644 --- a/module/Core/src/Visit/Model/UnknownVisitLocation.php +++ b/module/Core/src/Visit/Model/UnknownVisitLocation.php @@ -25,14 +25,7 @@ final class UnknownVisitLocation implements VisitLocationInterface return 'Unknown'; } - /** - * Specify data which should be serialized to JSON - * @link https://php.net/manual/en/jsonserializable.jsonserialize.php - * @return mixed data which can be serialized by json_encode, - * which is a value of any type other than a resource. - * @since 5.4.0 - */ - public function jsonSerialize() + public function jsonSerialize(): array { return [ 'countryCode' => 'Unknown',