From f03913ab0498e157a0eac49f1e5cff7584d074cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=94=81?= <52o@qq52o.cn> Date: Wed, 20 Apr 2022 22:24:51 +0800 Subject: [PATCH] Fix max_execution_time config doesn't work (#510) * Fix max_execution_time config doesn't work * Add extension_loaded swoole verify * Update EnsureRequestsDontExceedMaxExecutionTime.php * Update EnsureRequestsDontExceedMaxExecutionTime.php --- bin/createSwooleTimerTable.php | 1 + bin/swoole-server | 1 + .../EnsureRequestsDontExceedMaxExecutionTime.php | 11 ++++++++++- src/Swoole/Handlers/OnServerStart.php | 4 ++-- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/bin/createSwooleTimerTable.php b/bin/createSwooleTimerTable.php index 756f4518e..3cf369081 100644 --- a/bin/createSwooleTimerTable.php +++ b/bin/createSwooleTimerTable.php @@ -10,6 +10,7 @@ $timerTable->column('worker_pid', Table::TYPE_INT); $timerTable->column('time', Table::TYPE_INT); + $timerTable->column('fd', Table::TYPE_INT); $timerTable->create(); diff --git a/bin/swoole-server b/bin/swoole-server index bcd7a28e4..f2b0c8784 100755 --- a/bin/swoole-server +++ b/bin/swoole-server @@ -107,6 +107,7 @@ $server->on('request', function ($request, $response) use ($server, $workerState $workerState->timerTable->set($workerState->workerId, [ 'worker_pid' => $workerState->workerPid, 'time' => time(), + 'fd' => $request->fd, ]); } diff --git a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php index 1f9e1ad49..0902d44f7 100644 --- a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php +++ b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php @@ -3,13 +3,16 @@ namespace Laravel\Octane\Swoole\Actions; use Laravel\Octane\Swoole\SwooleExtension; +use Swoole\Http\Response; +use Swoole\Http\Server; class EnsureRequestsDontExceedMaxExecutionTime { public function __construct( protected SwooleExtension $extension, protected $timerTable, - protected $maxExecutionTime + protected $maxExecutionTime, + protected ?Server $server = null ) { } @@ -25,6 +28,12 @@ public function __invoke() $this->timerTable->del($workerId); $this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL); + + if ($this->server instanceof Server) { + $response = Response::create($this->server, $row['fd']); + $response->status(408); + $response->end(); + } } } } diff --git a/src/Swoole/Handlers/OnServerStart.php b/src/Swoole/Handlers/OnServerStart.php index bb4614ec2..5cd1f6dfe 100644 --- a/src/Swoole/Handlers/OnServerStart.php +++ b/src/Swoole/Handlers/OnServerStart.php @@ -43,9 +43,9 @@ public function __invoke($server) } if ($this->maxExecutionTime > 0) { - $server->tick(1000, function () { + $server->tick(1000, function () use ($server) { (new EnsureRequestsDontExceedMaxExecutionTime( - $this->extension, $this->timerTable, $this->maxExecutionTime + $this->extension, $this->timerTable, $this->maxExecutionTime, $server ))(); }); }