From 0ac64469b07600b4ce79f5edbf793551afbfdd64 Mon Sep 17 00:00:00 2001 From: mrpes <68982655+mrpes@users.noreply.github.com> Date: Thu, 30 Jul 2020 09:34:51 +0500 Subject: [PATCH] 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 --- telebot/apihelper.py | 45 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 138c438..a47976b 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -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 - result = _get_req_session().request( - method, request_url, params=params, files=files, - timeout=(connect_timeout, read_timeout), proxies=proxy) + + if RETRY_ON_ERROR: + got_result = False + current_try = 0 + + while not got_result and current_try