From aca194669091db25ad6bd2896c860714ef59e33b Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Tue, 17 Mar 2026 19:52:09 +0300 Subject: [PATCH 1/2] Simplify ping pong handler --- src/Internal/Connection/PingPongHandler.php | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/Internal/Connection/PingPongHandler.php b/src/Internal/Connection/PingPongHandler.php index f9fb4d2..0b85f5c 100644 --- a/src/Internal/Connection/PingPongHandler.php +++ b/src/Internal/Connection/PingPongHandler.php @@ -4,7 +4,6 @@ namespace Thesis\Nats\Internal\Connection; -use Amp; use Revolt\EventLoop; use Thesis\Nats\Internal\Protocol; @@ -18,8 +17,6 @@ final class PingPongHandler /** @var non-negative-int */ private int $pings = 0; - private ?float $lastPingPongTime = null; - public function __construct( private readonly Connection $connection, ) {} @@ -32,23 +29,21 @@ public function startup(int $interval, int $maxPings): void { $interval /= 1_000; - $this->callbackId = EventLoop::repeat($interval, function () use ($interval, $maxPings): void { + $this->callbackId = EventLoop::repeat($interval, function () use ($maxPings): void { if (++$this->pings > $maxPings) { $this->forceStop(); - } elseif ((Amp\now() - (int) $this->lastPingPongTime) > $interval) { - $this->lastPingPongTime = Amp\now(); - $this->connection->execute(Protocol\Ping::Frame); + return; } + + $this->connection->execute(Protocol\Ping::Frame); }); $this->connection->hooks()->onPing(function (): void { - $this->lastPingPongTime = Amp\now(); $this->connection->execute(Protocol\Pong::Frame); }); $this->connection->hooks()->onPong(function (): void { - $this->lastPingPongTime = Amp\now(); - $this->pings = max(0, $this->pings - 1); + $this->pings = 0; }); $this->connection->hooks()->onClose($this->stop(...)); From 8272b0036edbce3a802ca4eada8b929fe1e122a4 Mon Sep 17 00:00:00 2001 From: kafkiansky Date: Tue, 17 Mar 2026 19:55:38 +0300 Subject: [PATCH 2/2] chore: fix cs --- src/Internal/Connection/PingPongHandler.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Internal/Connection/PingPongHandler.php b/src/Internal/Connection/PingPongHandler.php index 0b85f5c..b569265 100644 --- a/src/Internal/Connection/PingPongHandler.php +++ b/src/Internal/Connection/PingPongHandler.php @@ -32,6 +32,7 @@ public function startup(int $interval, int $maxPings): void $this->callbackId = EventLoop::repeat($interval, function () use ($maxPings): void { if (++$this->pings > $maxPings) { $this->forceStop(); + return; }