Browse Source

Decrease MQTT publish timeout

Force reconnect quicker if our connection is broken, this will then
re-send any pending messages.
Nick Barrett 2 years ago
parent
commit
7d905e4828
1 changed files with 7 additions and 4 deletions
  1. 7 4
      mauigpapi/mqtt/conn.py

+ 7 - 4
mauigpapi/mqtt/conn.py

@@ -80,7 +80,8 @@ ACTIVITY_INDICATOR_REGEX = re.compile(
 
 INBOX_THREAD_REGEX = re.compile(r"/direct_v2/inbox/threads/([\w_]+)")
 
-REQUEST_TIMEOUT = 30
+REQUEST_PUBLISH_TIMEOUT = 5
+REQUEST_RESPONSE_TIMEOUT = 30
 
 
 class AndroidMQTT:
@@ -661,7 +662,9 @@ class AndroidMQTT:
         info = self._client.publish(topic.encoded, payload, qos=1)
         self.log.trace(f"Published message ID: {info.mid}")
         fut = self._loop.create_future()
-        timeout_handle = self._loop.call_later(REQUEST_TIMEOUT, self._publish_cancel_later, fut)
+        timeout_handle = self._loop.call_later(
+            REQUEST_PUBLISH_TIMEOUT, self._publish_cancel_later, fut
+        )
         fut.add_done_callback(lambda _: timeout_handle.cancel())
         self._publish_waiters[info.mid] = fut
         return fut
@@ -690,7 +693,7 @@ class AndroidMQTT:
                 f"Request published to {topic.value}, waiting for response {response.name}"
             )
             timeout_handle = self._loop.call_later(
-                timeout or REQUEST_TIMEOUT, self._request_cancel_later, fut
+                timeout or REQUEST_RESPONSE_TIMEOUT, self._request_cancel_later, fut
             )
             fut.add_done_callback(lambda _: timeout_handle.cancel())
             return await fut
@@ -789,7 +792,7 @@ class AndroidMQTT:
                 f"waiting for response {RealtimeTopic.SEND_MESSAGE_RESPONSE}"
             )
             try:
-                resp = await asyncio.wait_for(fut, timeout=REQUEST_TIMEOUT)
+                resp = await asyncio.wait_for(fut, timeout=REQUEST_RESPONSE_TIMEOUT)
             except asyncio.TimeoutError:
                 self.log.error(f"Request with ID {client_context} timed out!")
                 raise