From a19657e6ee74387d2e2b4e557909a4d6dde552a8 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Tue, 1 Oct 2024 21:47:04 +0800 Subject: [PATCH] fix --- src/CoroutineWebServer.php | 2 +- src/Handlers/SwooleHandler.php | 2 +- src/Handlers/SwowHandler.php | 2 +- src/Utils/WaitGroup/Handlers/SwooleWaitGroup.php | 4 ++++ src/Utils/Worker/ServerMethods.php | 3 ++- 5 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/CoroutineWebServer.php b/src/CoroutineWebServer.php index adc16c2..ec82162 100644 --- a/src/CoroutineWebServer.php +++ b/src/CoroutineWebServer.php @@ -117,7 +117,7 @@ public function onMessage($connection, $request, ...$params) $channel->push(func_get_args()); $waitGroup = new WaitGroup(); // 根据request consumer数量创建协程 - $consumerCount = config('plugin.workbunny.webman-coroutine.app.consumer_count', 1); + $consumerCount = max(1, config('plugin.workbunny.webman-coroutine.app.consumer_count', 1)); foreach (range(1, $consumerCount) as $ignored) { $waitGroup->add(); // 请求消费协程 diff --git a/src/Handlers/SwooleHandler.php b/src/Handlers/SwooleHandler.php index 2540125..1289532 100644 --- a/src/Handlers/SwooleHandler.php +++ b/src/Handlers/SwooleHandler.php @@ -92,7 +92,7 @@ public static function onMessage(CoroutineServerInterface $app, mixed $connectio ]); $waitGroup = new WaitGroup(); // 根据request consumer数量创建协程 - $consumerCount = config('plugin.workbunny.webman-coroutine.app.consumer_count', 1); + $consumerCount = max(1, config('plugin.workbunny.webman-coroutine.app.consumer_count', 1)); foreach (range(1, $consumerCount) as $ignored) { // 协程监听通道,消费 $res = Coroutine::create(function () use ($app, $connectionChannels, $waitGroup) { diff --git a/src/Handlers/SwowHandler.php b/src/Handlers/SwowHandler.php index e721259..9f15847 100644 --- a/src/Handlers/SwowHandler.php +++ b/src/Handlers/SwowHandler.php @@ -80,7 +80,7 @@ public static function onMessage(CoroutineServerInterface $app, mixed $connectio ]); $waitGroup = new WaitGroup(); // 根据request consumer数量创建协程 - $consumerCount = config('plugin.workbunny.webman-coroutine.app.consumer_count', 1); + $consumerCount = max(1, config('plugin.workbunny.webman-coroutine.app.consumer_count', 1)); foreach (range(1, $consumerCount) as $ignored) { $waitGroup->add(); // 请求消费协程 diff --git a/src/Utils/WaitGroup/Handlers/SwooleWaitGroup.php b/src/Utils/WaitGroup/Handlers/SwooleWaitGroup.php index 52c163c..9f50aa3 100644 --- a/src/Utils/WaitGroup/Handlers/SwooleWaitGroup.php +++ b/src/Utils/WaitGroup/Handlers/SwooleWaitGroup.php @@ -23,6 +23,10 @@ public function __construct() /** @inheritdoc */ public function __destruct() { + $count = max(1, $this->count()); + foreach (range(1, $count) as $ignored) { + $this->done(); + } $this->_waitGroup = null; } diff --git a/src/Utils/Worker/ServerMethods.php b/src/Utils/Worker/ServerMethods.php index ef28ede..5b01d7f 100644 --- a/src/Utils/Worker/ServerMethods.php +++ b/src/Utils/Worker/ServerMethods.php @@ -204,7 +204,8 @@ protected function __runInit__serverMethods(): void ]); $waitGroup = new WaitGroup(); // 消费者消费 - foreach (range(1, $this->getConnectionConsumerCount()) as $ignored) { + $count = max(1, $this->getConnectionConsumerCount()); + foreach (range(1, $count) as $ignored) { $waitGroup->add(); // 协程创建 $this->getCoroutine()->create(function () use ($channel, $waitGroup) {