Skip to content

Commit 855bcc9

Browse files
committed
[0.6.x] Always clear heartbeat timer
To ensure we don't end up with multiple timers keeping the event loop busy.
1 parent 9784298 commit 855bcc9

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

spec/generate.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,10 @@ function amqpTypeToLength(string $type, string $e): array
10571057

10581058
$connectionContent .= " public function startHeartbeatTimer(): void\n";
10591059
$connectionContent .= " {\n";
1060+
$connectionContent .= " if (\$this->heartbeatTimer instanceof TimerInterface) {\n";
1061+
$connectionContent .= " Loop::cancelTimer(\$this->heartbeatTimer);\n";
1062+
$connectionContent .= " }\n";
1063+
$connectionContent .= "\n";
10601064
$connectionContent .= " \$this->heartbeatTimer = Loop::addTimer(\$this->configuration->heartbeat, \$this->onHeartbeat(...));\n";
10611065
$connectionContent .= " \$this->connection->on('drain', \$this->onHeartbeat(...));\n";
10621066
$connectionContent .= " }\n";
@@ -1066,6 +1070,10 @@ function amqpTypeToLength(string $type, string $e): array
10661070
$connectionContent .= " */\n";
10671071
$connectionContent .= " private function onHeartbeat(): void\n";
10681072
$connectionContent .= " {\n";
1073+
$connectionContent .= " if (\$this->heartbeatTimer instanceof TimerInterface) {\n";
1074+
$connectionContent .= " Loop::cancelTimer(\$this->heartbeatTimer);\n";
1075+
$connectionContent .= " }\n";
1076+
$connectionContent .= "\n";
10691077
$connectionContent .= " \$now = microtime(true);\n";
10701078
$connectionContent .= " \$nextHeartbeat = (\$this->lastWrite ?: \$now) + \$this->configuration->heartbeat;\n";
10711079
$connectionContent .= "\n";

src/Connection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,10 @@ public function awaitConfirmSelectOk(int $channel): Protocol\MethodConfirmSelect
21952195

21962196
public function startHeartbeatTimer(): void
21972197
{
2198+
if ($this->heartbeatTimer instanceof TimerInterface) {
2199+
Loop::cancelTimer($this->heartbeatTimer);
2200+
}
2201+
21982202
$this->heartbeatTimer = Loop::addTimer($this->configuration->heartbeat, $this->onHeartbeat(...));
21992203
$this->connection->on('drain', $this->onHeartbeat(...));
22002204
}
@@ -2204,6 +2208,10 @@ public function startHeartbeatTimer(): void
22042208
*/
22052209
private function onHeartbeat(): void
22062210
{
2211+
if ($this->heartbeatTimer instanceof TimerInterface) {
2212+
Loop::cancelTimer($this->heartbeatTimer);
2213+
}
2214+
22072215
$now = microtime(true);
22082216
$nextHeartbeat = ($this->lastWrite ?: $now) + $this->configuration->heartbeat;
22092217

0 commit comments

Comments
 (0)