1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Retry on requests error

Added RETRY_ON_ERROR var. If its value is true, we'll try to get proper result MAX_RETRIES times, with RETRY_TIMEOUT delay between requests. Last request will be called outside of the try block, so it will throw an exception on failure

P.S.
I'm actually not sure if there are better ways to solve this problem, but this was my way of solving it
This commit is contained in:
mrpes 2020-07-30 09:34:51 +05:00 committed by GitHub
parent ce3c91b619
commit 0ac64469b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
import time
try:
import ujson as json
@ -6,6 +7,7 @@ except ImportError:
import json
import requests
from requests.exceptions import HTTPError, ConnectionError, Timeout
try:
from requests.packages.urllib3 import fields
@ -28,6 +30,10 @@ FILE_URL = None
CONNECT_TIMEOUT = 3.5
READ_TIMEOUT = 9999
RETRY_ON_ERROR = False
RETRY_TIMEOUT = 2
MAX_RETRIES = 15
CUSTOM_SERIALIZER = None
ENABLE_MIDDLEWARE = False
@ -62,9 +68,42 @@ def _make_request(token, method_name, method='get', params=None, files=None):
read_timeout = params.pop('timeout') + 10
if 'connect-timeout' in params:
connect_timeout = params.pop('connect-timeout') + 10
if RETRY_ON_ERROR:
got_result = False
current_try = 0
while not got_result and current_try<MAX_RETRIES-1:
current_try+=1
try:
result = _get_req_session().request(
method, request_url, params=params, files=files,
timeout=(connect_timeout, read_timeout), proxies=proxy)
got_result = True
except HTTPError:
logger.debug("HTTP Error on {0} method (Try #{1})".format(method_name, current_try))
time.sleep(RETRY_TIMEOUT)
except ConnectionError:
logger.debug("Connection Error on {0} method (Try #{1})".format(method_name, current_try))
time.sleep(RETRY_TIMEOUT)
except Timeout:
logger.debug("Timeout Error on {0} method (Try #{1})".format(method_name, current_try))
time.sleep(RETRY_TIMEOUT)
if not got_result:
result = _get_req_session().request(
method, request_url, params=params, files=files,
timeout=(connect_timeout, read_timeout), proxies=proxy)
else:
result = _get_req_session().request(
method, request_url, params=params, files=files,
timeout=(connect_timeout, read_timeout), proxies=proxy)
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
return _check_result(method_name, result)['result']