From e1c95e047c9a448e64a521125514d0c421f6a25b Mon Sep 17 00:00:00 2001 From: twomiao <995200452@qq.com> Date: Sun, 15 Oct 2023 00:06:10 +0800 Subject: [PATCH 1/2] Update worker --- src/Events/Swoole.php | 12 ++---------- src/Worker.php | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/src/Events/Swoole.php b/src/Events/Swoole.php index ff2cf24bf..1e2c9eea0 100644 --- a/src/Events/Swoole.php +++ b/src/Events/Swoole.php @@ -45,16 +45,6 @@ class Swoole implements EventInterface */ protected $errorHandler = null; - /** - * Construct - */ - public function __construct() - { - // Avoid process exit due to no listening - Timer::tick(100000000, function () { - }); - } - /** * {@inheritdoc} * @throws Throwable @@ -219,6 +209,8 @@ public function deleteAllTimer(): void */ public function run(): void { + // Avoid process exit due to no listening + Timer::tick(100000000, static fn() => null); Event::wait(); } diff --git a/src/Worker.php b/src/Worker.php index 39262277e..087e47584 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -37,6 +37,7 @@ use function stream_socket_recvfrom; use function substr; use function array_walk; +use function get_class; /** * Worker class @@ -627,6 +628,9 @@ protected static function init(): void // State. static::$status = static::STATUS_STARTING; + // Avoiding incorrect user calls. + // static::resetGlobalEvent(); + // For statistics. static::$globalStatistics['start_timestamp'] = time(); @@ -640,6 +644,20 @@ protected static function init(): void Timer::init(); } + /** + * reset globalEvent Instance. + * + * @return void + */ + protected static function resetGlobalEvent(): void + { + if (static::$status === static::STATUS_STARTING && + static::$globalEvent instanceof EventInterface) { + static::$eventLoopClass = get_class(static::$globalEvent); + static::$globalEvent = null; + } + } + /** * Lock. * @@ -1822,15 +1840,11 @@ protected static function reload(): void foreach (static::$pidMap as $workerId => $workerPidArray) { $worker = static::$workers[$workerId]; if ($worker->reloadable) { - foreach ($workerPidArray as $pid) { - $reloadablePidArray[$pid] = $pid; - } - } else { - foreach ($workerPidArray as $pid) { - // Send reload signal to a worker process which reloadable is false. - posix_kill($pid, $sig); - } + $reloadablePidArray += $workerPidArray; + continue; } + // Send reload signal to a worker process which reloadable is false. + array_walk($workerPidArray, static fn ($pid) => posix_kill($pid, $sig)); } // Get all pids that are waiting reload. static::$pidsToRestart = array_intersect(static::$pidsToRestart, $reloadablePidArray); From 74e8dfb764507bfb0216243219be7a243d22c75c Mon Sep 17 00:00:00 2001 From: twomiao <995200452@qq.com> Date: Sun, 15 Oct 2023 00:15:14 +0800 Subject: [PATCH 2/2] Update worker --- src/Worker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Worker.php b/src/Worker.php index 087e47584..120ffe86e 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -629,7 +629,7 @@ protected static function init(): void static::$status = static::STATUS_STARTING; // Avoiding incorrect user calls. - // static::resetGlobalEvent(); + static::resetGlobalEvent(); // For statistics. static::$globalStatistics['start_timestamp'] = time();