From 504dd7f0638a4575003ddb88d8a63b0813155502 Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Fri, 9 Aug 2024 11:46:48 +0800 Subject: [PATCH 1/6] Do not perform deletion operations during traversal (all keys can be taken out for deletion after traversal) Do not perform deletion operations during traversal (all keys can be taken out for deletion after traversal) --- ...sureRequestsDontExceedMaxExecutionTime.php | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php index 787ea9fa2..7ac0a1ad9 100644 --- a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php +++ b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php @@ -23,23 +23,31 @@ public function __construct( */ public function __invoke() { + $rows = []; foreach ($this->timerTable as $workerId => $row) { if ((time() - $row['time']) > $this->maxExecutionTime) { - $this->timerTable->del($workerId); + $rows[$workerId] = $row; + } + } - if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) { - continue; - } - $this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL); + foreach ($rows as $workerId => $row) { + if ($this->timerTable->get($workerId, 'fd') !== $row['fd']) { + continue; + } + + $this->timerTable->del($workerId); + if ($this->server instanceof Server && !$this->server->exists($row['fd'])) { + continue; + } - if ($this->server instanceof Server) { - $response = Response::create($this->server, $row['fd']); + $this->extension->dispatchProcessSignal($row['worker_pid'], SIGKILL); - if ($response) { - $response->status(408); - $response->end(); - } + if ($this->server instanceof Server) { + $response = Response::create($this->server, $row['fd']); + if ($response) { + $response->status(408); + $response->end(); } } } From 8ab04dab8f4649c99e996dd3d08f78d23b3cba60 Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Fri, 9 Aug 2024 14:08:13 +0800 Subject: [PATCH 2/6] fix bug --- tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php b/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php index a63ee51c7..4e5945b21 100644 --- a/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php +++ b/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php @@ -39,4 +39,9 @@ public function del($workerId) { $this->deleted[] = $workerId; } + + public function get($workerId, $field = null) + { + return 1; + } } From c72bd2053553f9836f3abed4f935a10decdfadd2 Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Fri, 9 Aug 2024 14:09:38 +0800 Subject: [PATCH 3/6] fix bug --- src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php index 7ac0a1ad9..f3964f6c6 100644 --- a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php +++ b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php @@ -37,7 +37,7 @@ public function __invoke() } $this->timerTable->del($workerId); - if ($this->server instanceof Server && !$this->server->exists($row['fd'])) { + if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) { continue; } From 18720c1699bfddffb6b1c4115484f0a92160b9e0 Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Fri, 9 Aug 2024 14:09:52 +0800 Subject: [PATCH 4/6] fix bug --- src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php index f3964f6c6..4bbf77bb4 100644 --- a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php +++ b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php @@ -30,7 +30,6 @@ public function __invoke() } } - foreach ($rows as $workerId => $row) { if ($this->timerTable->get($workerId, 'fd') !== $row['fd']) { continue; From bd710482fedbcb983b7786a6c96d82c89dbe1bd8 Mon Sep 17 00:00:00 2001 From: "hugh.li" Date: Fri, 9 Aug 2024 14:11:30 +0800 Subject: [PATCH 5/6] fix bug --- tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php b/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php index 4e5945b21..2b525a4fb 100644 --- a/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php +++ b/tests/EnsureRequestsDontExceedMaxExecutionTimeTest.php @@ -17,6 +17,7 @@ public function test_process_is_killed_if_current_request_exceeds_max_execution_ $table['fake-worker-id'] = [ 'worker_pid' => 111, 'time' => time() - 60, + 'fd' => 1, ]; $action = new EnsureRequestsDontExceedMaxExecutionTime( From 9e965ba1014e7ce149b5ea2b992623e1cda2d056 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 9 Aug 2024 07:19:57 -0500 Subject: [PATCH 6/6] Update EnsureRequestsDontExceedMaxExecutionTime.php --- .../Actions/EnsureRequestsDontExceedMaxExecutionTime.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php index 4bbf77bb4..8437ab362 100644 --- a/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php +++ b/src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php @@ -24,6 +24,7 @@ public function __construct( public function __invoke() { $rows = []; + foreach ($this->timerTable as $workerId => $row) { if ((time() - $row['time']) > $this->maxExecutionTime) { $rows[$workerId] = $row; @@ -36,6 +37,7 @@ public function __invoke() } $this->timerTable->del($workerId); + if ($this->server instanceof Server && ! $this->server->exists($row['fd'])) { continue; } @@ -44,6 +46,7 @@ public function __invoke() if ($this->server instanceof Server) { $response = Response::create($this->server, $row['fd']); + if ($response) { $response->status(408); $response->end();