From b126094e74268a91256543b953d4b3ea59dcc53f Mon Sep 17 00:00:00 2001 From: Konstantin Grachev Date: Thu, 4 Feb 2021 23:07:41 +0300 Subject: [PATCH] Close connection on __destruct --- src/Connection.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Connection.php b/src/Connection.php index 94c860c..ca88d71 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -11,6 +11,7 @@ use Amp\ByteStream\ZlibOutputStream; use Amp\Failure; use Amp\Promise; use Amp\Socket\Socket; +use Amp\Success; use Nsq\Config\ClientConfig; use Nsq\Config\ConnectionConfig; use Nsq\Exception\AuthenticationRequired; @@ -62,6 +63,11 @@ abstract class Connection $this->logger = $logger ?? new NullLogger(); } + public function __destruct() + { + $this->close(); + } + /** * @return Promise */ @@ -123,7 +129,7 @@ abstract class Connection public function close(): Promise { if (null === $this->socket) { - return new Failure(new NotConnected()); + return new Success(); } return call(function (): \Generator {