From fbd37990810e60eeb83ffeaddc9f3f250ade1772 Mon Sep 17 00:00:00 2001 From: xl Date: Thu, 12 Sep 2024 09:06:02 +0800 Subject: [PATCH] eee --- kiri-actor/Actor.php | 238 ---------------------------------- kiri-actor/ActorInterface.php | 15 --- kiri-actor/ActorManager.php | 110 ---------------- kiri-actor/ActorMessage.php | 78 ----------- kiri-actor/ActorProcess.php | 47 ------- kiri-actor/ActorState.php | 18 --- 6 files changed, 506 deletions(-) delete mode 100644 kiri-actor/Actor.php delete mode 100644 kiri-actor/ActorInterface.php delete mode 100644 kiri-actor/ActorManager.php delete mode 100644 kiri-actor/ActorMessage.php delete mode 100644 kiri-actor/ActorProcess.php delete mode 100644 kiri-actor/ActorState.php diff --git a/kiri-actor/Actor.php b/kiri-actor/Actor.php deleted file mode 100644 index 9111e63d..00000000 --- a/kiri-actor/Actor.php +++ /dev/null @@ -1,238 +0,0 @@ -state; - } - - - /** - * @param ActorState $state - */ - public function setState(ActorState $state): void - { - $this->state = $state; - } - - - /** - * @return float - */ - public function getRunTime(): float - { - return microtime(true) - $this->startTime; - } - - - /** - * @param string $uniqueId - */ - private function __construct(readonly public string $uniqueId) - { - $this->channel = new Channel(99); - $this->startTime = microtime(true); - } - - - /** - * @return void - */ - public function init(): void - { - - } - - - /** - * @return bool - */ - public function isShutdown(): bool - { - return $this->isShutdown; - } - - - /** - * @param $id - * @return static - */ - public static function newActor($id): static - { - $actor = new static($id); - $actor->listen(); - return $actor; - } - - - /** - * @return void - */ - private function listen(): void - { - Coroutine::create(function (Actor $actor) { - $actor->coroutineId = Coroutine::getCid(); - $this->run(); - }, $this); - } - - - /** - * @return string - */ - public function getName(): string - { - return $this->uniqueId; - } - - - /** - * @param mixed $response - * @return bool - */ - public function write(mixed $response): bool - { - return $this->channel->push($response); - } - - - /** - * @return void - */ - public function shutdown(): void - { - $this->isShutdown = true; - Coroutine::cancel($this->coroutineId); - if ($this->messageId > -1) { - Coroutine::cancel($this->messageId); - } - $this->channel->close(); - } - - - /** - * @return void - */ - public function onUpdate(): void - { - } - - - /** - * @return void - * @throws Exception - */ - public function run(): void - { - if ($this->refreshInterval < 1) { - throw new Exception('Refresh interval must be greater than 1'); - } - $this->setState(ActorState::BUSY); - $this->init(); - $this->messageId = Coroutine::create(fn() => $this->loop()); - $this->interval(); - $this->setState(ActorState::IDLE); - } - - - /** - * @return void - */ - private function interval(): void - { - if ($this->isShutdown()) { - return; - } - - try { - $this->onUpdate(); - } catch (\Throwable $exception) { - error($exception); - } - - Coroutine::sleep($this->refreshInterval / 1000); - - $this->interval(); - } - - - /** - * @return bool - */ - private function loop(): bool - { - if ($this->messageId == -1) { - $this->messageId = Coroutine::getCid(); - } - if ($this->channel->errCode == SWOOLE_CHANNEL_CLOSED) { - $this->channel = new Channel(99); - } - $message = $this->channel->pop(); - $this->process($message); - if ($this->isShutdown()) { - return true; - } - return $this->loop(); - } - -} diff --git a/kiri-actor/ActorInterface.php b/kiri-actor/ActorInterface.php deleted file mode 100644 index 0bc5be61..00000000 --- a/kiri-actor/ActorInterface.php +++ /dev/null @@ -1,15 +0,0 @@ - */ - private array $nodes = []; - - - /** - * @param Actor $actor - * @return void - */ - public function addActor(ActorInterface $actor): void - { - $this->nodes[$actor->uniqueId] = $actor; - Coroutine::create(function (Actor $actor) { - $actor->run(); - }, $actor); - } - - - /** - * @param $name - * @return void - */ - public function closeActor($name): void - { - $node = $this->nodes[$name] ?? null; - if (is_null($node)) { - return; - } - foreach ($node as $actor) { - $actor->shutdown(); - } - } - - - /** - * @param $name - * @param $message - * @return bool - */ - public function write($name, $message): bool - { - $actor = $this->nodes[$name] ?? null; - if (is_null($actor)) { - return false; - } - return $actor->write($message); - } - - - /** - * @param $name - * @return array - */ - public function lists($name): array - { - $array = []; - foreach ($this->nodes[$name] as $actor) { - $array[] = [ - 'id' => $actor->getName(), - 'state' => $actor->getState()->name, - 'runTime' => $actor->getRunTime() - ]; - } - return $array; - } - - - /** - * @param string $uniqueId - * @return bool - */ - public function hasActor(string $uniqueId): bool - { - return isset($this->nodes[$uniqueId]) && $this->nodes[$uniqueId] instanceof ActorInterface; - } - - - /** - * @param array|null $data - * @return void - */ - public static function exec(?array $data): void - { - if (is_null($data)) { - return; - } - } - - - /** - * @return void - */ - public function clean(): void - { - foreach ($this->nodes as $actor) { - $actor->shutdown(); - } - $this->nodes = []; - } - -} diff --git a/kiri-actor/ActorMessage.php b/kiri-actor/ActorMessage.php deleted file mode 100644 index 91bcaa2d..00000000 --- a/kiri-actor/ActorMessage.php +++ /dev/null @@ -1,78 +0,0 @@ -userId = $userId; - $this->event = $event; - $this->body = $body; - } - - /** - * @return int - */ - public function getUserId(): int - { - return $this->userId; - } - - /** - * @return string - */ - public function getEvent(): string - { - return $this->event; - } - - /** - * @return array - */ - public function getBody(): array - { - return $this->body; - } - - - /** - * @return array - */ - #[ArrayShape(['userId' => "int", 'event' => "string", 'body' => "array"])] - public function jsonSerialize(): array - { - return [ - 'userId' => $this->userId, - 'event' => $this->event, - 'body' => $this->body - ]; - } - -} diff --git a/kiri-actor/ActorProcess.php b/kiri-actor/ActorProcess.php deleted file mode 100644 index 67527df3..00000000 --- a/kiri-actor/ActorProcess.php +++ /dev/null @@ -1,47 +0,0 @@ -isStop() === false) { - $read = $process->read(); - - ActorManager::exec(json_decode($read, true)); - - Coroutine::sleep(1000 / 120); - } - } - - - /** - * @return void - */ - public function onSigterm(): void - { - } -} \ No newline at end of file diff --git a/kiri-actor/ActorState.php b/kiri-actor/ActorState.php deleted file mode 100644 index bdb6c017..00000000 --- a/kiri-actor/ActorState.php +++ /dev/null @@ -1,18 +0,0 @@ -