Skip to content

Commit 5d8e6e4

Browse files
committed
v1.6.0 - gc collect cycles
1 parent fdb4b4d commit 5d8e6e4

7 files changed

+128
-2
lines changed

src/Facade/HttpRoadrunnerFacade.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http\Facade;
15+
16+
use Micro\Plugin\Http\HttpRoadrunnerPluginConfigurationInterface;
17+
18+
final readonly class HttpRoadrunnerFacade implements HttpRoadrunnerFacadeInterface
19+
{
20+
public function __construct(
21+
private HttpRoadrunnerPluginConfigurationInterface $httpRoadrunnerPluginConfiguration
22+
) {
23+
}
24+
25+
public function getGcCollectCyclesCount(): int
26+
{
27+
return $this->httpRoadrunnerPluginConfiguration->getGcCollectCyclesCount();
28+
}
29+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http\Facade;
15+
16+
interface HttpRoadrunnerFacadeInterface
17+
{
18+
public function getGcCollectCyclesCount(): int;
19+
}

src/HttpRoadrunnerPlugin.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@
1313

1414
namespace Micro\Plugin\Http;
1515

16+
use Micro\Component\DependencyInjection\Container;
17+
use Micro\Framework\Kernel\Plugin\ConfigurableInterface;
18+
use Micro\Framework\Kernel\Plugin\DependencyProviderInterface;
19+
use Micro\Framework\Kernel\Plugin\PluginConfigurationTrait;
1620
use Micro\Framework\Kernel\Plugin\PluginDependedInterface;
1721
use Micro\Plugin\EventEmitter\EventEmitterPlugin;
22+
use Micro\Plugin\Http\Facade\HttpRoadrunnerFacade;
23+
use Micro\Plugin\Http\Facade\HttpRoadrunnerFacadeInterface;
1824

19-
final readonly class HttpRoadrunnerPlugin implements PluginDependedInterface
25+
/**
26+
* @method HttpRoadrunnerPluginConfigurationInterface configuration()
27+
*/
28+
final class HttpRoadrunnerPlugin implements DependencyProviderInterface, PluginDependedInterface, ConfigurableInterface
2029
{
30+
use PluginConfigurationTrait;
31+
32+
public function provideDependencies(Container $container): void
33+
{
34+
$container->register(HttpRoadrunnerFacadeInterface::class, function () {
35+
return new HttpRoadrunnerFacade($this->configuration());
36+
});
37+
}
38+
2139
public function getDependedPlugins(): iterable
2240
{
2341
return [
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http;
15+
16+
use Micro\Framework\Kernel\Configuration\PluginConfiguration;
17+
18+
final class HttpRoadrunnerPluginConfiguration extends PluginConfiguration implements HttpRoadrunnerPluginConfigurationInterface
19+
{
20+
public const CFG_GC_COLLECT_CYCLES_COUNT = 'MICRO_RR_GC_COLLECT_CYCLES_COUNT';
21+
22+
public const CFG_GC_COLLECT_CYCLES_COUNT_DEFAULT = 10;
23+
24+
public function getGcCollectCyclesCount(): int
25+
{
26+
return (int) $this->configuration
27+
->get(
28+
self::CFG_GC_COLLECT_CYCLES_COUNT,
29+
self::CFG_GC_COLLECT_CYCLES_COUNT_DEFAULT,
30+
false
31+
);
32+
}
33+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Micro framework package.
7+
*
8+
* (c) Stanislau Komar <kost@micro-php.net>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Micro\Plugin\Http;
15+
16+
interface HttpRoadrunnerPluginConfigurationInterface
17+
{
18+
public function getGcCollectCyclesCount(): int;
19+
}

src/Listener/ApplicationRoadrunnerStartedListener.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Micro\Kernel\App\Business\Event\ApplicationReadyEvent;
1919
use Micro\Kernel\App\Business\Event\ApplicationReadyEventInterface;
2020
use Micro\Plugin\Http\Facade\HttpFacadeInterface;
21+
use Micro\Plugin\Http\Facade\HttpRoadrunnerFacadeInterface;
2122
use Nyholm\Psr7\Factory\Psr17Factory;
2223
use Spiral\RoadRunner;
2324
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
@@ -26,7 +27,8 @@
2627
final readonly class ApplicationRoadrunnerStartedListener implements EventListenerInterface
2728
{
2829
public function __construct(
29-
private HttpFacadeInterface $httpFacade
30+
private HttpFacadeInterface $httpFacade,
31+
private HttpRoadrunnerFacadeInterface $httpRoadrunnerFacade
3032
) {
3133
}
3234

@@ -50,13 +52,19 @@ public function on(EventInterface $event): void
5052

5153
$worker = RoadRunner\Worker::create();
5254
$worker = new RoadRunner\Http\PSR7Worker($worker, $psr17Factory, $psr17Factory, $psr17Factory);
55+
$i = 0;
56+
$gcCollectStep = $this->httpRoadrunnerFacade->getGcCollectCyclesCount();
5357
while ($request = $worker->waitRequest()) {
5458
try {
5559
$appRequest = $httpFoundationFactory->createRequest($request);
5660
$appResponse = $this->httpFacade->execute($appRequest, false);
5761
$worker->respond($httpMessageFactory->createResponse($appResponse));
5862
} catch (\Throwable $e) {
5963
$worker->getWorker()->error((string) $e);
64+
} finally {
65+
if (++$i === $gcCollectStep) {
66+
gc_collect_cycles();
67+
}
6068
}
6169
}
6270
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)