mqtt: retry connection if failed to resolve hostname

Signed-off-by: Pedro Lamas <pedrolamas@gmail.com>
This commit is contained in:
Pedro Lamas 2021-11-17 11:57:45 +00:00 committed by Eric Callahan
parent 93299658fb
commit d51d65ef30
1 changed files with 3 additions and 3 deletions

View File

@ -243,8 +243,8 @@ class MQTTClient(APITransport, Subscribable):
for _ in range(retries): for _ in range(retries):
try: try:
self.client.connect(self.address, self.port) self.client.connect(self.address, self.port)
except ConnectionRefusedError: except (ConnectionRefusedError, socket.gaierror) as e:
logging.info("Unable to connect to MQTT broker, " logging.info(f"MQTT connection error, {e}, "
f"retries remaining: {retries}") f"retries remaining: {retries}")
await asyncio.sleep(2.) await asyncio.sleep(2.)
else: else:
@ -368,7 +368,7 @@ class MQTTClient(APITransport, Subscribable):
break break
try: try:
self.client.reconnect() self.client.reconnect()
except ConnectionRefusedError: except (ConnectionRefusedError, socket.gaierror):
continue continue
self.client.socket().setsockopt( self.client.socket().setsockopt(
socket.SOL_SOCKET, socket.SO_SNDBUF, 2048) socket.SOL_SOCKET, socket.SO_SNDBUF, 2048)