]> git.michaelhowe.org Git - packages/p/paho-mqtt.git/commitdiff
Retry the first connection attempt in face of errors
authorHmvp <hmvp@hmvp.nl>
Tue, 16 Sep 2014 08:38:47 +0000 (10:38 +0200)
committerHmvp <hmvp@hmvp.nl>
Wed, 24 Sep 2014 09:49:11 +0000 (11:49 +0200)
When using connect async and loop_forever it is expected that connection errors lead to a reconnect
The first connection is not retried. This patch changes that

To preserve backward compatibility added behind boolean

Change-Id: I919595beb57ce8dd7da6da601760a2775634cf50
Signed-off-by: Hmvp <hmvp@hmvp.nl>
README.rst
src/paho/mqtt/client.py

index 890a613df9baa3ea1aff3b03730f1b8dafb000ab..1903090e5db0006199cb57df928019df990a4ec1 100644 (file)
@@ -434,10 +434,12 @@ loop_forever()
 
 ::
 
-    loop_forever(timeout=1.0, max_packets=1)
+    loop_forever(timeout=1.0, max_packets=1, retry_first_connection=False)
     
 This is a blocking form of the network loop and will not return until the client calls ``disconnect()``. It automatically handles reconnecting.
 
+Except for the first connection attempt when using connect_async, use ``retry_first_connection=True`` to make it retry the first connection.  Warning: This might lead to situations where the client keeps connecting to an non existing host without failing.
+
 The ``timeout`` and ``max_packets`` arguments are obsolete and should be left unset.
 
 Publishing
index b662aeb7ad41100ee24109dee86357e2a627a768..72c5505afefdeeaa2a6e81a615426ae0ab488895 100755 (executable)
@@ -1224,17 +1224,36 @@ class Client(object):
         else:
             return self._sock
 
-    def loop_forever(self, timeout=1.0, max_packets=1):
+    def loop_forever(self, timeout=1.0, max_packets=1, retry_first_connection=False):
         """This function call loop() for you in an infinite blocking loop. It
         is useful for the case where you only want to run the MQTT client loop
         in your program.
 
         loop_forever() will handle reconnecting for you. If you call
-        disconnect() in a callback it will return."""
+        disconnect() in a callback it will return.
+
+
+        timeout: The time in seconds to wait for incoming/outgoing network
+          traffic before timing out and returning.
+        max_packets: Not currently used.
+        retry_first_connection: Should the first connection attempt be retried on failure.
+
+        Raises socket.error on first connection failures unless retry_first_connection=True
+        """
 
         run = True
-        if self._state == mqtt_cs_connect_async:
-            self.reconnect()
+
+        while run:
+            if self._state == mqtt_cs_connect_async:
+                try:
+                    self.reconnect()
+                except socket.error:
+                    if not retry_first_connection:
+                        raise
+                    self._easy_log(MQTT_LOG_DEBUG, "Connection failed, retrying")
+                    time.sleep(1)
+            else:
+                break
 
         while run:
             rc = MQTT_ERR_SUCCESS