mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Merge pull request #927 from mrpes/patch-1
Optional retry on requests error
This commit is contained in:
commit
b790e4e6ba
@ -1,4 +1,5 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
import time
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import ujson as json
|
import ujson as json
|
||||||
@ -6,6 +7,7 @@ except ImportError:
|
|||||||
import json
|
import json
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from requests.exceptions import HTTPError, ConnectionError, Timeout
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from requests.packages.urllib3 import fields
|
from requests.packages.urllib3 import fields
|
||||||
@ -28,6 +30,10 @@ FILE_URL = None
|
|||||||
CONNECT_TIMEOUT = 3.5
|
CONNECT_TIMEOUT = 3.5
|
||||||
READ_TIMEOUT = 9999
|
READ_TIMEOUT = 9999
|
||||||
|
|
||||||
|
RETRY_ON_ERROR = False
|
||||||
|
RETRY_TIMEOUT = 2
|
||||||
|
MAX_RETRIES = 15
|
||||||
|
|
||||||
CUSTOM_SERIALIZER = None
|
CUSTOM_SERIALIZER = None
|
||||||
|
|
||||||
ENABLE_MIDDLEWARE = False
|
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
|
read_timeout = params.pop('timeout') + 10
|
||||||
if 'connect-timeout' in params:
|
if 'connect-timeout' in params:
|
||||||
connect_timeout = params.pop('connect-timeout') + 10
|
connect_timeout = params.pop('connect-timeout') + 10
|
||||||
result = _get_req_session().request(
|
|
||||||
method, request_url, params=params, files=files,
|
if RETRY_ON_ERROR:
|
||||||
timeout=(connect_timeout, read_timeout), proxies=proxy)
|
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')))
|
logger.debug("The server returned: '{0}'".format(result.text.encode('utf8')))
|
||||||
return _check_result(method_name, result)['result']
|
return _check_result(method_name, result)['result']
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user