From 2f8d878f063019f1268a4fed990ddbdab4da6e8b Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sat, 10 Sep 2022 14:34:56 +0400 Subject: [PATCH 1/4] Fixed difference between request_timeout and timeout. getUpdates parameter may contain 2 parameters: request_timeout & timeout. other methods may contain timeout parameter that should be applied to ClientTimeout. --- telebot/asyncio_helper.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 00d4aa3..bf97033 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -56,10 +56,29 @@ class SessionManager: session_manager = SessionManager() -async def _process_request(token, url, method='get', params=None, files=None, request_timeout=None): +async def _process_request(token, url, method='get', params=None, files=None, **kwargs): + # Let's resolve all timeout parameters. + # getUpdates parameter may contain 2 parameters: request_timeout & timeout. + # other methods may contain timeout parameter that should be applied to + # ClientTimeout only. + # timeout should be added to params for getUpdates. All other timeout's should be used + # for request timeout. + try: + request_timeout = kwargs.pop('request_timeout') + # if exception wasn't raised, then we have request_timeout in kwargs(this is getUpdates method) + # so we will simply apply that request_timeout to ClientTimeout + except KeyError: + # exception was raised, so we know that this method is not getUpdates. + # let's check for timeout in params + request_timeout = kwargs.pop('timeout', None) + # we will apply default request_timeout if there is no timeout in params + # otherwise, we will use timeout parameter applied for payload. + request_timeout = REQUEST_TIMEOUT if request_timeout is None else request_timeout + + + # Preparing data by adding all parameters and files to FormData params = _prepare_data(params, files) - if request_timeout is None: - request_timeout = REQUEST_TIMEOUT + timeout = aiohttp.ClientTimeout(total=request_timeout) got_result = False current_try=0 From da5084f53c937cf96d569d4d4c11c78aa01607e5 Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sat, 10 Sep 2022 14:36:56 +0400 Subject: [PATCH 2/4] Update asyncio_helper.py --- telebot/asyncio_helper.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index bf97033..47095a2 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -70,7 +70,7 @@ async def _process_request(token, url, method='get', params=None, files=None, ** except KeyError: # exception was raised, so we know that this method is not getUpdates. # let's check for timeout in params - request_timeout = kwargs.pop('timeout', None) + request_timeout = params.pop('timeout', None) # we will apply default request_timeout if there is no timeout in params # otherwise, we will use timeout parameter applied for payload. request_timeout = REQUEST_TIMEOUT if request_timeout is None else request_timeout From 0028feb4c5888ce65fec69a3064275e21b1bcd11 Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sat, 10 Sep 2022 20:14:48 +0400 Subject: [PATCH 3/4] Update asyncio_helper.py --- telebot/asyncio_helper.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 47095a2..8b20d91 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -21,7 +21,7 @@ session = None FILE_URL = None -REQUEST_TIMEOUT = None +REQUEST_TIMEOUT = 300 MAX_RETRIES = 3 REQUEST_LIMIT = 50 @@ -63,12 +63,11 @@ async def _process_request(token, url, method='get', params=None, files=None, ** # ClientTimeout only. # timeout should be added to params for getUpdates. All other timeout's should be used # for request timeout. - try: - request_timeout = kwargs.pop('request_timeout') - # if exception wasn't raised, then we have request_timeout in kwargs(this is getUpdates method) - # so we will simply apply that request_timeout to ClientTimeout - except KeyError: - # exception was raised, so we know that this method is not getUpdates. + request_timeout = kwargs.pop('request_timeout', False) + # if we got some value, then we have request_timeout in kwargs(this is getUpdates method) + # so we will simply apply that request_timeout to ClientTimeout + # and if we got False, then we don't have request_timeout in kwargs(this is not getUpdates method) + if request_timeout is False: # let's check for timeout in params request_timeout = params.pop('timeout', None) # we will apply default request_timeout if there is no timeout in params From 4f97b26e81eb5f7fd6518357aa80e0d506d7666e Mon Sep 17 00:00:00 2001 From: coder2020official Date: Sat, 10 Sep 2022 20:37:13 +0400 Subject: [PATCH 4/4] Update asyncio_helper.py --- telebot/asyncio_helper.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/telebot/asyncio_helper.py b/telebot/asyncio_helper.py index 8b20d91..4b44532 100644 --- a/telebot/asyncio_helper.py +++ b/telebot/asyncio_helper.py @@ -63,16 +63,17 @@ async def _process_request(token, url, method='get', params=None, files=None, ** # ClientTimeout only. # timeout should be added to params for getUpdates. All other timeout's should be used # for request timeout. - request_timeout = kwargs.pop('request_timeout', False) - # if we got some value, then we have request_timeout in kwargs(this is getUpdates method) - # so we will simply apply that request_timeout to ClientTimeout - # and if we got False, then we don't have request_timeout in kwargs(this is not getUpdates method) - if request_timeout is False: + # here we got request_timeout, so this is getUpdates method. + if 'request_timeout' in kwargs: + request_timeout = kwargs.pop('request_timeout') + + else: # let's check for timeout in params request_timeout = params.pop('timeout', None) # we will apply default request_timeout if there is no timeout in params # otherwise, we will use timeout parameter applied for payload. - request_timeout = REQUEST_TIMEOUT if request_timeout is None else request_timeout + + request_timeout = REQUEST_TIMEOUT if request_timeout is None else request_timeout # Preparing data by adding all parameters and files to FormData