Lower API v2 streaming timeout to 20 seconds
authorHarmon <Harmon758@gmail.com>
Sat, 22 Oct 2022 00:33:50 +0000 (19:33 -0500)
committerHarmon <Harmon758@gmail.com>
Sat, 22 Oct 2022 00:33:50 +0000 (19:33 -0500)
Resolves part of #1986

tweepy/asynchronous/streaming.py
tweepy/streaming.py

index afe281289059a82e2f8507ec023a952dc1e9bb1d..b8bebf525c485cc7be197ff09c165f89d2af1cd7 100644 (file)
@@ -39,13 +39,12 @@ class AsyncBaseStream:
 
     async def _connect(
         self, method, url, params=None, headers=None, body=None,
-        oauth_client=None
+        oauth_client=None, timeout=20
     ):
         error_count = 0
         # https://developer.twitter.com/en/docs/twitter-api/v1/tweets/filter-realtime/guides/connecting
         # https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/handling-disconnections
         # https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/integrate/handling-disconnections
-        stall_timeout = 90
         network_error_wait = network_error_wait_step = 0.25
         network_error_wait_max = 16
         http_error_wait = http_error_wait_start = 5
@@ -54,7 +53,7 @@ class AsyncBaseStream:
 
         if self.session is None or self.session.closed:
             self.session = aiohttp.ClientSession(
-                timeout=aiohttp.ClientTimeout(sock_read=stall_timeout)
+                timeout=aiohttp.ClientTimeout(sock_read=timeout)
             )
         self.session.headers["User-Agent"] = self.user_agent
 
@@ -258,7 +257,8 @@ class AsyncStream(AsyncBaseStream):
         url = f"https://stream.twitter.com/1.1/{endpoint}.json"
         url = str(URL(url).with_query(sorted(params.items())))
         await super()._connect(
-            method, url, headers=headers, body=body, oauth_client=oauth_client
+            method, url, headers=headers, body=body, oauth_client=oauth_client,
+            timeout=90
         )
 
     def filter(self, *, follow=None, track=None, locations=None,
index e815743c5968c03d676557601281310b33c873a0..9228a9e21496d8cdb62fdf0c6018196e33236b67 100644 (file)
@@ -50,15 +50,16 @@ class BaseStream:
             f"Tweepy/{tweepy.__version__}"
         )
 
-    def _connect(self, method, url, auth=None, params=None, headers=None,
-                 body=None):
+    def _connect(
+        self, method, url, auth=None, params=None, headers=None, body=None,
+        timeout=20
+    ):
         self.running = True
 
         error_count = 0
         # https://developer.twitter.com/en/docs/twitter-api/v1/tweets/filter-realtime/guides/connecting
         # https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/handling-disconnections
         # https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/integrate/handling-disconnections
-        stall_timeout = 90
         network_error_wait = network_error_wait_step = 0.25
         network_error_wait_max = 16
         http_error_wait = http_error_wait_start = 5
@@ -72,7 +73,7 @@ class BaseStream:
                 try:
                     with self.session.request(
                         method, url, params=params, headers=headers, data=body,
-                        timeout=stall_timeout, stream=True, auth=auth,
+                        timeout=timeout, stream=True, auth=auth,
                         verify=self.verify, proxies=self.proxies
                     ) as resp:
                         if resp.status_code == 200:
@@ -276,7 +277,7 @@ class Stream(BaseStream):
         auth = OAuth1(self.consumer_key, self.consumer_secret,
                       self.access_token, self.access_token_secret)
         url = f"https://stream.twitter.com/1.1/{endpoint}.json"
-        super()._connect(method, url, auth=auth, **kwargs)
+        super()._connect(method, url, auth=auth, timeout=90, **kwargs)
 
     def filter(self, *, follow=None, track=None, locations=None,
                filter_level=None, languages=None, stall_warnings=False,