From 9791c1237775f3b4f348f4cb17927e7ae106f059 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 8 Oct 2020 12:05:22 +0200 Subject: [PATCH 1/3] Allow developer to control FpmHandler timeout --- src/Event/Http/FpmHandler.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Event/Http/FpmHandler.php b/src/Event/Http/FpmHandler.php index 9925779c4..7984ae4c6 100644 --- a/src/Event/Http/FpmHandler.php +++ b/src/Event/Http/FpmHandler.php @@ -43,6 +43,8 @@ final class FpmHandler extends HttpHandler private $configFile; /** @var Process|null */ private $fpm; + /** @var int */ + private $socketReadWriteTimeout = 30000; public function __construct(string $handler, string $configFile = self::CONFIG) { @@ -98,6 +100,17 @@ public function __destruct() $this->stop(); } + /** + * Allow the developer to change the Socket Read Write Timeout to support APIs that take + * longer than 30 seconds to run behing ALB. + */ + public function setSocketReadWriteTimeout(int $timeout): self + { + $this->socketReadWriteTimeout = $timeout; + + return $this; + } + /** * Proxy the API Gateway event to PHP-FPM and return its response. */ From e1d0dc7847995aad6acc1461be4e582f1bd0bc29 Mon Sep 17 00:00:00 2001 From: Deleu Date: Thu, 8 Oct 2020 13:19:12 +0200 Subject: [PATCH 2/3] Forgot to use the variable :sweat_smile: --- src/Event/Http/FpmHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Event/Http/FpmHandler.php b/src/Event/Http/FpmHandler.php index 7984ae4c6..cc8d3e5b1 100644 --- a/src/Event/Http/FpmHandler.php +++ b/src/Event/Http/FpmHandler.php @@ -80,7 +80,7 @@ public function start(): void }); $this->client = new Client; - $this->connection = new UnixDomainSocket(self::SOCKET, 1000, 30000); + $this->connection = new UnixDomainSocket(self::SOCKET, 1000, $this->socketReadWriteTimeout); $this->waitUntilReady(); } From 5ee64d202edd767847b7cd297a8850cc91f6320c Mon Sep 17 00:00:00 2001 From: Deleu Date: Fri, 9 Oct 2020 11:28:43 +0200 Subject: [PATCH 3/3] Environmnet Variable strategy for FPM Timeout --- runtime/layers/fpm/bootstrap | 8 +++++++- src/Event/Http/FpmHandler.php | 16 +++------------- tests/Handler/FpmHandlerLoadBalancerTest.php | 2 +- tests/Handler/FpmHandlerTest.php | 6 +++--- 4 files changed, 14 insertions(+), 18 deletions(-) diff --git a/runtime/layers/fpm/bootstrap b/runtime/layers/fpm/bootstrap index e9378e44c..fa7215d07 100755 --- a/runtime/layers/fpm/bootstrap +++ b/runtime/layers/fpm/bootstrap @@ -24,7 +24,13 @@ if (! is_file($handlerFile)) { $lambdaRuntime->failInitialization("Handler `$handlerFile` doesn't exist"); } -$phpFpm = new FpmHandler($handlerFile); +if (getenv('BREF_PHP_FPM_TIMEOUT')) { + $timeout = (int) getenv('BREF_PHP_FPM_TIMEOUT') * 1000; +} else { + $timeout = 30000; +} + +$phpFpm = new FpmHandler($handlerFile, $timeout); try { $phpFpm->start(); } catch (\Throwable $e) { diff --git a/src/Event/Http/FpmHandler.php b/src/Event/Http/FpmHandler.php index cc8d3e5b1..9c6c5967b 100644 --- a/src/Event/Http/FpmHandler.php +++ b/src/Event/Http/FpmHandler.php @@ -44,11 +44,12 @@ final class FpmHandler extends HttpHandler /** @var Process|null */ private $fpm; /** @var int */ - private $socketReadWriteTimeout = 30000; + private $socketReadWriteTimeout; - public function __construct(string $handler, string $configFile = self::CONFIG) + public function __construct(string $handler, int $socketReadWriteTimeout, string $configFile = self::CONFIG) { $this->handler = $handler; + $this->socketReadWriteTimeout = $socketReadWriteTimeout; $this->configFile = $configFile; } @@ -100,17 +101,6 @@ public function __destruct() $this->stop(); } - /** - * Allow the developer to change the Socket Read Write Timeout to support APIs that take - * longer than 30 seconds to run behing ALB. - */ - public function setSocketReadWriteTimeout(int $timeout): self - { - $this->socketReadWriteTimeout = $timeout; - - return $this; - } - /** * Proxy the API Gateway event to PHP-FPM and return its response. */ diff --git a/tests/Handler/FpmHandlerLoadBalancerTest.php b/tests/Handler/FpmHandlerLoadBalancerTest.php index 404d35213..58ebff4b8 100644 --- a/tests/Handler/FpmHandlerLoadBalancerTest.php +++ b/tests/Handler/FpmHandlerLoadBalancerTest.php @@ -151,7 +151,7 @@ private function startFpm(string $handler): void if ($this->fpm) { $this->fpm->stop(); } - $this->fpm = new FpmHandler($handler, __DIR__ . '/PhpFpm/php-fpm.conf'); + $this->fpm = new FpmHandler($handler, 5000, __DIR__ . '/PhpFpm/php-fpm.conf'); $this->fpm->start(); } } diff --git a/tests/Handler/FpmHandlerTest.php b/tests/Handler/FpmHandlerTest.php index 00144fa98..96cb2a583 100644 --- a/tests/Handler/FpmHandlerTest.php +++ b/tests/Handler/FpmHandlerTest.php @@ -1085,7 +1085,7 @@ public function test response with error_log() */ public function test FPM timeouts are recovered from() { - $this->fpm = new FpmHandler(__DIR__ . '/PhpFpm/timeout.php', __DIR__ . '/PhpFpm/php-fpm.conf'); + $this->fpm = new FpmHandler(__DIR__ . '/PhpFpm/timeout.php', 5000, __DIR__ . '/PhpFpm/php-fpm.conf'); $this->fpm->start(); try { @@ -1124,7 +1124,7 @@ public function test warmer events do not invoke the application() { // Run `timeout.php` to make sure that the handler is not really executed. // If it was, then PHP-FPM would timeout (and error). - $this->fpm = new FpmHandler(__DIR__ . '/PhpFpm/timeout.php', __DIR__ . '/PhpFpm/php-fpm.conf'); + $this->fpm = new FpmHandler(__DIR__ . '/PhpFpm/timeout.php', 5000, __DIR__ . '/PhpFpm/php-fpm.conf'); $this->fpm->start(); $result = $this->fpm->handle([ @@ -1198,7 +1198,7 @@ private function startFpm(string $handler): void if ($this->fpm) { $this->fpm->stop(); } - $this->fpm = new FpmHandler($handler, __DIR__ . '/PhpFpm/php-fpm.conf'); + $this->fpm = new FpmHandler($handler, 5000, __DIR__ . '/PhpFpm/php-fpm.conf'); $this->fpm->start(); } }