From 4d3514b79681213f7ba73bf267be48b8034e9fb4 Mon Sep 17 00:00:00 2001 From: Marc Morera Date: Sat, 23 Jul 2022 11:20:46 +0200 Subject: [PATCH] GC Collect cycles forced each N seconds --- src/Application.php | 6 ++++++ src/Console/ServerCommand.php | 1 + src/Context/ServerContext.php | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/Application.php b/src/Application.php index 4818380..859f632 100644 --- a/src/Application.php +++ b/src/Application.php @@ -186,6 +186,12 @@ function (ServerRequestInterface $request) use ($kernelAdapter, $filesystem) { await($kernelAdapter->shutdown(), $loop); }; + if ($this->serverContext->getGcCollectsInSeconds() > 0) { + $this->loop->addPeriodicTimer($this->serverContext->getGcCollectsInSeconds(), function () { + gc_collect_cycles(); + }); + } + $this->loop->addSignal(SIGTERM, $signalHandler); $this->loop->addSignal(SIGINT, $signalHandler); } diff --git a/src/Console/ServerCommand.php b/src/Console/ServerCommand.php index d74f29f..f5669af 100644 --- a/src/Console/ServerCommand.php +++ b/src/Console/ServerCommand.php @@ -74,6 +74,7 @@ protected function configure() ->addOption('request-body-buffer', null, InputOption::VALUE_OPTIONAL, 'Limit of the buffer used for the Request body. In KiB.', 1024) ->addOption('adapter', null, InputOption::VALUE_OPTIONAL, 'Server Adapter. Can be a namespace or a shortcut. Available shortcuts [drift, symfony]', 'drift') ->addOption('allowed-loop-stops', null, InputOption::VALUE_OPTIONAL, 'Number of allowed loop stops', 0) + ->addOption('gc-collect-cycles', null, InputOption::VALUE_OPTIONAL, 'Seconds between each force GC collect execution. 0 for disabled.', 0) ->addOption('workers', null, InputOption::VALUE_OPTIONAL, 'Number of workers. Use -1 to get as many workers as physical thread available for your system. Maximum of 128 workers. Option disabled for watch command.', 1 ) diff --git a/src/Context/ServerContext.php b/src/Context/ServerContext.php index 452900c..3e14bfe 100644 --- a/src/Context/ServerContext.php +++ b/src/Context/ServerContext.php @@ -44,6 +44,8 @@ final class ServerContext private int $workers; private bool $closeConnections; + private int $gcCollectsInSeconds; + /** * @param InputInterface $input * @@ -114,6 +116,7 @@ public static function buildByInput(InputInterface $input): ServerContext $serverContext->limitConcurrentRequests = intval($input->getOption('concurrent-requests')); $serverContext->requestBodyBuffer = intval($input->getOption('request-body-buffer')); + $serverContext->gcCollectsInSeconds = intval($input->getOption('gc-collect-cycles')); $serverContext->closeConnections = boolval($input->getOption('close-connections')); $serverContext->allowedLoopStops = intval($input->getOption('allowed-loop-stops')); $serverContext->workers = \intval($input->getOption('workers')); @@ -320,6 +323,14 @@ public function mustCloseConnections(): bool return $this->closeConnections; } + /** + * @return int + */ + public function getGcCollectsInSeconds(): int + { + return $this->gcCollectsInSeconds; + } + /** * Build queue architecture from array of strings. *