Skip to content

Commit 1620873

Browse files
authored
frankenphp: add frankenphp_loop_max option (#156)
See php/frankenphp#280.
1 parent ba45740 commit 1620873

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

src/frankenphp-symfony/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Added
66

77
- Add support for Symfony 7
8+
- Add `frankenphp_loop_max` option
89

910
## 0.1.0
1011

src/frankenphp-symfony/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ return function (array $context) {
3636
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
3737
};
3838
```
39+
40+
## Options
41+
42+
* `frankenphp_loop_max`: the number of requests after which the worker must restart, to prevent weird memory leaks (default to `500`, set to `-1` to never restart)

src/frankenphp-symfony/src/Runner.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
*/
1717
class Runner implements RunnerInterface
1818
{
19-
public function __construct(private HttpKernelInterface $kernel)
20-
{
19+
public function __construct(
20+
private HttpKernelInterface $kernel,
21+
private int $loopMax,
22+
) {
2123
}
2224

2325
public function run(): int
@@ -38,6 +40,7 @@ public function run(): int
3840
$sfResponse->send();
3941
};
4042

43+
$loops = 0;
4144
do {
4245
$ret = \frankenphp_handle_request($handler);
4346

@@ -46,7 +49,7 @@ public function run(): int
4649
}
4750

4851
gc_collect_cycles();
49-
} while ($ret);
52+
} while ($ret && (-1 === $this->loopMax || ++$loops <= $this->loopMax));
5053

5154
return 0;
5255
}

src/frankenphp-symfony/src/Runtime.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@
1515
*/
1616
class Runtime extends SymfonyRuntime
1717
{
18+
/**
19+
* @param array{
20+
* frankenphp_loop_max?: int,
21+
* } $options
22+
*/
23+
public function __construct(array $options = [])
24+
{
25+
$options['frankenphp_loop_max'] = (int) ($options['frankenphp_loop_max'] ?? $_SERVER['FRANKENPHP_LOOP_MAX'] ?? $_ENV['FRANKENPHP_LOOP_MAX'] ?? 500);
26+
27+
parent::__construct($options);
28+
}
29+
1830
public function getRunner(?object $application): RunnerInterface
1931
{
2032
if ($application instanceof HttpKernelInterface && ($_SERVER['FRANKENPHP_WORKER'] ?? false)) {
21-
return new Runner($application);
33+
return new Runner($application, $this->options['frankenphp_loop_max']);
2234
}
2335

2436
return parent::getRunner($application);

src/frankenphp-symfony/tests/RunnerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function testRun(): void
3737

3838
$_SERVER['FOO'] = 'bar';
3939

40-
$runner = new Runner($application);
40+
$runner = new Runner($application, 500);
4141
$this->assertSame(0, $runner->run());
4242
}
4343
}

0 commit comments

Comments
 (0)