Skip to content

Commit

Permalink
Lower API v2 streaming timeout to 20 seconds
Browse files Browse the repository at this point in the history
Resolves part of tweepy#1986
  • Loading branch information
Harmon758 committed Oct 22, 2022
1 parent d7f2622 commit 3a71c9e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 4 additions & 4 deletions tweepy/asynchronous/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ def __init__(self, *, max_retries=inf, proxy=None):

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
Expand All @@ -54,7 +53,7 @@ async def _connect(

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

Expand Down Expand Up @@ -258,7 +257,8 @@ async def _connect(
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,
Expand Down
11 changes: 6 additions & 5 deletions tweepy/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ def __init__(self, *, chunk_size=512, daemon=False, max_retries=inf,
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
Expand All @@ -72,7 +73,7 @@ def _connect(self, method, url, auth=None, params=None, headers=None,
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:
Expand Down Expand Up @@ -276,7 +277,7 @@ def _connect(self, method, endpoint, **kwargs):
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,
Expand Down

0 comments on commit 3a71c9e

Please sign in to comment.