Skip to content

Commit

Permalink
Merge pull request #31 from php-service-bus/master
Browse files Browse the repository at this point in the history
Merge fix
  • Loading branch information
mmasiukevich authored Dec 4, 2021
2 parents 3561e05 + 0df0471 commit 0f86217
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ final class Connection
private $lastRead = 0;

/**
* @var string
* @var string|null
*/
private $heartbeatWatcherId;

Expand Down Expand Up @@ -168,35 +168,38 @@ public function heartbeat(int $interval, ?callable $connectionLost = null): void
{
$this->heartbeatWatcherId = Loop::repeat(
$interval,
function ($watcherId) use ($interval, $connectionLost){
$currentTime = Loop::now();
function (string $watcherId) use ($interval, $connectionLost){
$currentTime = Loop::now();

if (null !== $this->socket) {
$lastWrite = $this->lastWrite ?: $currentTime;
if (null !== $this->socket) {
$lastWrite = $this->lastWrite ?: $currentTime;

$nextHeartbeat = $lastWrite + $interval;
$nextHeartbeat = $lastWrite + $interval;

if ($currentTime >= $nextHeartbeat) {
yield $this->write((new Buffer)
->appendUint8(8)
->appendUint16(0)
->appendUint32(0)
->appendUint8(206)
);
}
if ($currentTime >= $nextHeartbeat) {
yield $this->write((new Buffer)
->appendUint8(8)
->appendUint16(0)
->appendUint32(0)
->appendUint8(206)
);
}

unset($lastWrite, $nextHeartbeat);
}
unset($lastWrite, $nextHeartbeat);
}

if (null !== $connectionLost && 0 !== $this->lastRead) {
if ($currentTime > ($this->lastRead + $interval + 1000)) {
if (
null !== $connectionLost &&
0 !== $this->lastRead &&
$currentTime > ($this->lastRead + $interval + 1000)
)
{
$connectionLost();
Loop::cancel($watcherId);
}
}

unset($currentTime);
});
unset($currentTime);
});
}

public function close(): void
Expand Down

0 comments on commit 0f86217

Please sign in to comment.