Skip to content

Commit 22a207e

Browse files
authored
Add new connection setting for delay between reconnect attempts (#130)
1 parent 8d6ecd8 commit 22a207e

File tree

4 files changed

+39
-1
lines changed

4 files changed

+39
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
164164
// This setting is only relevant if setReconnectAutomatically() is set to true.
165165
->setMaxReconnectAttempts(3)
166166

167+
// Defines the delay between reconnect attempts in milliseconds.
168+
// This setting is only relevant if setReconnectAutomatically() is set to true.
169+
->setDelayBetweenReconnectAttempts(0)
170+
167171
// The keep alive interval is the number of seconds the client will wait without sending a message
168172
// until it sends a keep alive signal (ping) to the broker. The value cannot be less than 1 second
169173
// and may not be higher than 65535 seconds. A reasonable value is 10 seconds (the default).

src/Concerns/ValidatesConfiguration.php

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ protected function ensureConnectionSettingsAreValid(ConnectionSettings $settings
4747
throw new ConfigurationInvalidException('The maximum reconnect attempts cannot be fewer than 1.');
4848
}
4949

50+
if ($settings->getDelayBetweenReconnectAttempts() < 0) {
51+
throw new ConfigurationInvalidException('The delay between reconnect attempts cannot be lower than 0.');
52+
}
53+
5054
if ($settings->getUsername() !== null && trim($settings->getUsername()) === '') {
5155
throw new ConfigurationInvalidException('The username may not consist of white space only.');
5256
}

src/ConnectionSettings.php

+25
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ConnectionSettings
1919
private int $keepAliveInterval = 10;
2020
private bool $reconnectAutomatically = false;
2121
private int $maxReconnectAttempts = 3;
22+
private int $delayBetweenReconnectAttempts = 0;
2223
private ?string $lastWillTopic = null;
2324
private ?string $lastWillMessage = null;
2425
private int $lastWillQualityOfService = 0;
@@ -226,6 +227,30 @@ public function getMaxReconnectAttempts(): int
226227
return $this->maxReconnectAttempts;
227228
}
228229

230+
/**
231+
* Defines the delay between reconnect attempts in milliseconds.
232+
* This setting is only relevant if {@see setReconnectAutomatically()} is set to true.
233+
*
234+
* @param int $delayBetweenReconnectAttempts
235+
* @return ConnectionSettings
236+
*/
237+
public function setDelayBetweenReconnectAttempts(int $delayBetweenReconnectAttempts): ConnectionSettings
238+
{
239+
$copy = clone $this;
240+
241+
$copy->delayBetweenReconnectAttempts = $delayBetweenReconnectAttempts;
242+
243+
return $copy;
244+
}
245+
246+
/**
247+
* @return int
248+
*/
249+
public function getDelayBetweenReconnectAttempts(): int
250+
{
251+
return $this->delayBetweenReconnectAttempts;
252+
}
253+
229254
/**
230255
* If the broker should publish a last will message in the name of the client when the client
231256
* disconnects abruptly, this setting defines the topic on which the message will be published.

src/MqttClient.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ protected function performConnectionHandshake(bool $useCleanSession = false): vo
409409
*/
410410
protected function reconnect(): void
411411
{
412-
$maxReconnectAttempts = $this->settings->getMaxReconnectAttempts();
412+
$maxReconnectAttempts = $this->settings->getMaxReconnectAttempts();
413+
$delayBetweenReconnectAttempts = $this->settings->getDelayBetweenReconnectAttempts();
413414

414415
for ($i = 1; $i <= $maxReconnectAttempts; $i++) {
415416
try {
@@ -420,6 +421,10 @@ protected function reconnect(): void
420421
if ($i === $maxReconnectAttempts) {
421422
throw $e;
422423
}
424+
425+
if ($delayBetweenReconnectAttempts > 0) {
426+
usleep($delayBetweenReconnectAttempts * 1000);
427+
}
423428
}
424429
}
425430
}

0 commit comments

Comments
 (0)