Skip to content

Commit ddff90f

Browse files
authored
Abort connection handshake when connection timeout is reached (#84)
1 parent e3cbfcb commit ddff90f

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/MqttClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ private function parseTlsErrorMessage(?array $phpError, ?string &$tlsErrorCode =
315315
protected function performConnectionHandshake(bool $useCleanSession = false): void
316316
{
317317
try {
318+
$connectionHandshakeStartedAt = microtime(true);
319+
318320
$data = $this->messageProcessor->buildConnectMessage($this->settings, $useCleanSession);
319321

320322
$this->logger->debug('Sending connection handshake to broker.');
@@ -349,6 +351,15 @@ protected function performConnectionHandshake(bool $useCleanSession = false): vo
349351

350352
break;
351353
}
354+
355+
// If no acknowledgement has been received from the broker within the configured connection timeout period,
356+
// we abort the connection attempt and assume broker unavailability.
357+
if (microtime(true) - $this->settings->getConnectTimeout() > $connectionHandshakeStartedAt) {
358+
throw new ConnectingToBrokerFailedException(
359+
ConnectingToBrokerFailedException::EXCEPTION_CONNECTION_BROKER_UNAVAILABLE,
360+
'The broker did not acknowledge the connection attempt within the configured connection timeout period.'
361+
);
362+
}
352363
}
353364

354365
// We need to set the global buffer to the remaining data we might already have read.

0 commit comments

Comments
 (0)