From 7d53672f08ee846c58a8fda6bb05bee260801776 Mon Sep 17 00:00:00 2001 From: louisni Date: Mon, 22 Jun 2020 19:06:55 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Add=20=E2=80=9Cserver=5Ftimeout=E2=80=9D=20?= =?UTF-8?q?parameter=20for=20detect=20server-side=20timeout.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0server=5Ftimeout=20=E7=9A=84=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE,=20=E4=B8=89=E4=B8=AA=E5=87=BD=E6=95=B0,?= =?UTF-8?q?=E4=B8=BB=E8=A6=81=E4=B8=BA=E4=BA=86=E6=A3=80=E6=B5=8B=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E6=9C=89=E5=BC=80=E5=90=AF=E6=9C=8D=E5=8A=A1=E7=AB=AF?= =?UTF-8?q?=E8=B6=85=E6=97=B6,=20=E5=A6=82=E6=9E=9C=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E6=8A=9B=E5=87=BA=E5=BC=82=E5=B8=B8.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/laravels.php | 2 +- src/LaravelS.php | 6 ++++- src/Swoole/Process/ServerTimeoutTrait.php | 32 +++++++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/Swoole/Process/ServerTimeoutTrait.php diff --git a/config/laravels.php b/config/laravels.php index 22a98520..279c53f9 100644 --- a/config/laravels.php +++ b/config/laravels.php @@ -77,7 +77,7 @@ 'enable_reuse_port' => true, 'enable_coroutine' => false, 'http_compression' => false, - + 'server_timeout' => -1, // Slow log // 'request_slowlog_timeout' => 2, // 'request_slowlog_file' => storage_path(sprintf('logs/slow-%s.log', date('Y-m'))), diff --git a/src/LaravelS.php b/src/LaravelS.php index 77183752..96d0c745 100644 --- a/src/LaravelS.php +++ b/src/LaravelS.php @@ -37,7 +37,7 @@ class LaravelS extends Server /** * Fix conflicts of traits */ - use InotifyTrait, LaravelTrait, LogTrait, ProcessTitleTrait, TimerTrait, CustomProcessTrait; + use InotifyTrait, LaravelTrait, LogTrait, ProcessTitleTrait, TimerTrait, CustomProcessTrait, ServerTimeoutTrait; /**@var array */ protected $laravelConf; @@ -117,6 +117,8 @@ public function onWorkerStart(HttpServer $server, $workerId) // Fire WorkerStart event $this->fireEvent('WorkerStart', WorkerStartInterface::class, func_get_args()); + + $this->registerSignal(); } public function onWorkerStop(HttpServer $server, $workerId) @@ -149,6 +151,7 @@ public function onRequest(SwooleRequest $swooleRequest, SwooleResponse $swooleRe { try { parent::onRequest($swooleRequest, $swooleResponse); + $this->handleServerTimeout(); $laravelRequest = $this->convertRequest($this->laravel, $swooleRequest); $this->laravel->bindRequest($laravelRequest); $this->laravel->fireEvent('laravels.received_request', [$laravelRequest]); @@ -218,6 +221,7 @@ protected function handleDynamicResource(Laravel $laravel, IlluminateRequest $la } $response->setChunkLimit($this->conf['swoole']['buffer_output_size']); $response->send($this->conf['enable_gzip']); + $this->resetAlarm(); $laravel->clean(); return true; } diff --git a/src/Swoole/Process/ServerTimeoutTrait.php b/src/Swoole/Process/ServerTimeoutTrait.php new file mode 100644 index 00000000..a6f165d6 --- /dev/null +++ b/src/Swoole/Process/ServerTimeoutTrait.php @@ -0,0 +1,32 @@ +getServerTimeout() != -1) { + \Swoole\Process::alarm(-1); + } + } + + public function handleServerTimeout() + { + if ($this->getServerTimeout() != -1) { + \Swoole\Process::alarm($this->conf['swoole']['server_timeout'] * 1000 * 1000); + } + } + + public function registerSignal() + { + if ($this->getServerTimeout() != -1) { + pcntl_signal(SIGALRM, function () { + \Swoole\Process::alarm(-1); + throw new \Exception(); + }); + } + } +} From 521e7d12a8fbafb13779040f4d26c87ff9b6e62d Mon Sep 17 00:00:00 2001 From: louis9507 Date: Tue, 23 Jun 2020 17:13:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/LaravelS.php | 1 + src/Swoole/Process/ServerTimeoutTrait.php | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/LaravelS.php b/src/LaravelS.php index 96d0c745..a031d117 100644 --- a/src/LaravelS.php +++ b/src/LaravelS.php @@ -14,6 +14,7 @@ use Hhxsv5\LaravelS\Swoole\InotifyTrait; use Hhxsv5\LaravelS\Swoole\Process\CustomProcessTrait; use Hhxsv5\LaravelS\Swoole\Process\ProcessTitleTrait; +use Hhxsv5\LaravelS\Swoole\Process\ServerTimeoutTrait; use Hhxsv5\LaravelS\Swoole\Request; use Hhxsv5\LaravelS\Swoole\Server; use Hhxsv5\LaravelS\Swoole\StaticResponse; diff --git a/src/Swoole/Process/ServerTimeoutTrait.php b/src/Swoole/Process/ServerTimeoutTrait.php index a6f165d6..d8335078 100644 --- a/src/Swoole/Process/ServerTimeoutTrait.php +++ b/src/Swoole/Process/ServerTimeoutTrait.php @@ -29,4 +29,9 @@ public function registerSignal() }); } } + + public function getServerTimeout() + { + return $this->conf['swoole']['server_timeout']; + } }