Reconnect according to twitters recommendations.
authorTimo Ewalds <tewalds@google.com>
Fri, 23 Aug 2013 22:08:42 +0000 (18:08 -0400)
committerTimo Ewalds <tewalds@google.com>
Mon, 26 Aug 2013 15:42:12 +0000 (11:42 -0400)
Recommendations found here: https://dev.twitter.com/docs/streaming-apis/connecting#Reconnecting

A simpler and more complete version of
- https://github.com/tweepy/tweepy/pull/132
- https://github.com/tweepy/tweepy/pull/150
only handling the reconnect strategies.

tweepy/streaming.py

index 3a5651a49a5493f73b8ab555df0f7b97eba6c0b2..67abd60bb6ad3d0ea578d2c25501184476c3f2d9 100644 (file)
@@ -96,8 +96,12 @@ class Stream(object):
         self.running = False
         self.timeout = options.get("timeout", 300.0)
         self.retry_count = options.get("retry_count")
-        self.retry_time = options.get("retry_time", 10.0)
-        self.snooze_time = options.get("snooze_time",  5.0)
+        # values according to https://dev.twitter.com/docs/streaming-apis/connecting#Reconnecting
+        self.retry_time_start = options.get("retry_time", 5.0)
+        self.retry_420_start = options.get("retry_420", 60.0)
+        self.retry_time_cap = options.get("retry_time_cap", 320.0)
+        self.snooze_time_step = options.get("snooze_time", 0.25)
+        self.snooze_time_cap = options.get("snooze_time_cap", 16)
         self.buffer_size = options.get("buffer_size",  1500)
         if options.get("secure", True):
             self.scheme = "https"
@@ -108,6 +112,8 @@ class Stream(object):
         self.headers = options.get("headers") or {}
         self.parameters = None
         self.body = None
+        self.retry_time = self.retry_time_start
+        self.snooze_time = self.snooze_time_step
 
     def _run(self):
         # Authenticate
@@ -134,9 +140,14 @@ class Stream(object):
                     if self.listener.on_error(resp.status) is False:
                         break
                     error_counter += 1
+                    if resp.status == 420:
+                        self.retry_time = max(self.retry_420_start, self.retry_time)
                     sleep(self.retry_time)
+                    self.retry_time = min(self.retry_time * 2, self.retry_time_cap)
                 else:
                     error_counter = 0
+                    self.retry_time = self.retry_time_start
+                    self.snooze_time = self.snooze_time_step
                     self.listener.on_connect()
                     self._read_loop(resp)
             except timeout:
@@ -146,6 +157,8 @@ class Stream(object):
                     break
                 conn.close()
                 sleep(self.snooze_time)
+                self.snooze_time = min(self.snooze_time + self.snooze_time_step,
+                                       self.snooze_time_cap)
             except Exception, exception:
                 # any other exception is fatal, so kill loop
                 break