From 45aa4c5d925ddc5bc3067a6b54c216f170af4036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=9E=97?= Date: Tue, 11 Oct 2022 15:19:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=98=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kiri-actor/Actor.php | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/kiri-actor/Actor.php b/kiri-actor/Actor.php index 80f40714..1ed9e9b2 100644 --- a/kiri-actor/Actor.php +++ b/kiri-actor/Actor.php @@ -2,9 +2,10 @@ namespace Kiri\Actor; +use JsonSerializable; use Swoole\Coroutine\Channel; -abstract class Actor implements ActorInterface +abstract class Actor implements ActorInterface, JsonSerializable { @@ -64,7 +65,7 @@ abstract class Actor implements ActorInterface */ private function __construct(readonly public string $uniqueId) { - $this->channel = new Channel(1000); + $this->channel = new Channel(99); $this->startTime = microtime(true); } @@ -113,24 +114,23 @@ abstract class Actor implements ActorInterface */ public function run(): void { - if ($this->channel->errCode == SWOOLE_CHANNEL_CLOSED) { - if ($this->isShutdown) { - return; - } - $this->channel = new Channel(1000); - } $this->setState(ActorState::BUSY); - while (!$this->isShutdown) { - $message = $this->channel->pop(); - $this->process($message); - unset($message); - } + $this->loop(); $this->setState(ActorState::IDLE); - if ($this->isShutdown) { - $this->channel->close(); - return; + } + + + /** + * @return mixed + */ + private function loop(): mixed + { + if ($this->channel->errCode == SWOOLE_CHANNEL_CLOSED) { + $this->channel = new Channel(99); } - $this->run(); + $message = $this->channel->pop(); + $this->process($message); + return $this->loop(); } }