Skip to content

Commit

Permalink
Fix max_execution_time config doesn't work (#510)
Browse files Browse the repository at this point in the history
* Fix max_execution_time config doesn't work

* Add extension_loaded swoole verify

* Update EnsureRequestsDontExceedMaxExecutionTime.php

* Update EnsureRequestsDontExceedMaxExecutionTime.php
  • Loading branch information
sy-records authored Apr 20, 2022
1 parent 85dcacf commit f03913a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions bin/createSwooleTimerTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions bin/swoole-server
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);
}

Expand Down
11 changes: 10 additions & 1 deletion src/Swoole/Actions/EnsureRequestsDontExceedMaxExecutionTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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();
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Swoole/Handlers/OnServerStart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
))();
});
}
Expand Down

0 comments on commit f03913a

Please sign in to comment.