Skip to content

Commit 3561e05

Browse files
authored
Merge pull request #28 from Volonda/issue-26
Heartbeat doesn't work properly
2 parents 97d2750 + aca0804 commit 3561e05

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

src/Client.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,9 @@ function () {
310310
$this->properties->tune($maxChannel, $maxFrame);
311311

312312
if ($heartbeatInterval > 0) {
313-
$this->connection->heartbeat($heartbeatInterval);
313+
$this->connection->heartbeat($heartbeatInterval, function (){
314+
$this->state = self::STATE_NOT_CONNECTED;
315+
});
314316
}
315317
}
316318
);

src/Connection.php

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ final class Connection
4949
private $lastWrite = 0;
5050

5151
/**
52-
* @var string|null
52+
* @var int
53+
*/
54+
private $lastRead = 0;
55+
56+
/**
57+
* @var string
5358
*/
5459
private $heartbeatWatcherId;
5560

@@ -126,6 +131,7 @@ function () use ($timeout, $maxAttempts, $noDelay) {
126131
}
127132

128133
$this->socket = yield connect($this->uri, $context);
134+
$this->lastRead = Loop::now();
129135

130136
asyncCall(
131137
function () {
@@ -138,6 +144,7 @@ function () {
138144

139145
while ($frame = $this->parser->parse()) {
140146
$class = \get_class($frame);
147+
$this->lastRead = Loop::now();
141148

142149
/**
143150
* @psalm-var callable(AbstractFrame):Promise<bool> $callback
@@ -157,18 +164,14 @@ function () {
157164
);
158165
}
159166

160-
public function heartbeat(int $interval): void
167+
public function heartbeat(int $interval, ?callable $connectionLost = null): void
161168
{
162169
$this->heartbeatWatcherId = Loop::repeat(
163170
$interval,
164-
function (string $watcherId) use ($interval) {
165-
if ($this->socket === null) {
166-
Loop::cancel($watcherId);
167-
168-
return;
169-
}
171+
function ($watcherId) use ($interval, $connectionLost){
172+
$currentTime = Loop::now();
170173

171-
$currentTime = Loop::now();
174+
if (null !== $this->socket) {
172175
$lastWrite = $this->lastWrite ?: $currentTime;
173176

174177
$nextHeartbeat = $lastWrite + $interval;
@@ -182,9 +185,18 @@ function (string $watcherId) use ($interval) {
182185
);
183186
}
184187

185-
unset($currentTime, $lastWrite, $nextHeartbeat);
188+
unset($lastWrite, $nextHeartbeat);
186189
}
187-
);
190+
191+
if (null !== $connectionLost && 0 !== $this->lastRead) {
192+
if ($currentTime > ($this->lastRead + $interval + 1000)) {
193+
$connectionLost();
194+
Loop::cancel($watcherId);
195+
}
196+
}
197+
198+
unset($currentTime);
199+
});
188200
}
189201

190202
public function close(): void

0 commit comments

Comments
 (0)