From 3a71c9e42ae9bcda479905def0f41b656d2dab5a Mon Sep 17 00:00:00 2001 From: Harmon Date: Fri, 21 Oct 2022 19:33:50 -0500 Subject: [PATCH] Lower API v2 streaming timeout to 20 seconds Resolves part of #1986 --- tweepy/asynchronous/streaming.py | 8 ++++---- tweepy/streaming.py | 11 ++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/tweepy/asynchronous/streaming.py b/tweepy/asynchronous/streaming.py index afe2812..b8bebf5 100644 --- a/tweepy/asynchronous/streaming.py +++ b/tweepy/asynchronous/streaming.py @@ -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, diff --git a/tweepy/streaming.py b/tweepy/streaming.py index e815743..9228a9e 100644 --- a/tweepy/streaming.py +++ b/tweepy/streaming.py @@ -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, -- 2.25.1