From 6f6e8f63386e31af4d7aecbf76234df1ad3009c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= Date: Thu, 3 Jun 2021 09:50:25 +0800 Subject: [PATCH] Optimized code which be used to close connection friendly. (#3659) --- src/AMQPConnection.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/AMQPConnection.php b/src/AMQPConnection.php index c45795e..b6ddac2 100644 --- a/src/AMQPConnection.php +++ b/src/AMQPConnection.php @@ -70,6 +70,11 @@ class AMQPConnection extends AbstractConnection */ protected $channelManager; + /** + * @var bool + */ + protected $exited = false; + public function __construct( string $user, string $password, @@ -142,7 +147,7 @@ public function channel($channel_id = null) $this->channelManager->close($channel_id); $this->channelManager->get($channel_id, true); - return parent::channel($channel_id); // TODO: Change the autogenerated stub + return parent::channel($channel_id); } public function getConfirmChannel(): AMQPChannel @@ -210,7 +215,8 @@ protected function loop(): void $this->channelManager->get($channel)->push([$frame_type, $payload], 0.001); } } catch (\Throwable $exception) { - $this->logger && $this->logger->error('Recv loop broken. The reason is ' . (string) $exception); + $level = $this->exited ? 'warning' : 'error'; + $this->logger && $this->logger->log($level, 'Recv loop broken. The reason is ' . (string) $exception); } finally { $this->loop = false; $this->close(); @@ -240,6 +246,8 @@ protected function heartbeat(): void Coroutine::create(function () { while (true) { if (CoordinatorManager::until(Constants::WORKER_EXIT)->yield($this->getHeartbeat())) { + $this->exited = true; + $this->close(); break; }