Retry on internal server errors, up to MAX_RETRIES times

Also add support for custom retry count
This commit is contained in:
Dan 2018-04-18 15:17:46 +02:00
parent f5efb6672a
commit 639828f550

View File

@ -32,7 +32,7 @@ from pyrogram import __copyright__, __license__, __version__
from pyrogram.api import functions, types, core
from pyrogram.api.all import layer
from pyrogram.api.core import Message, Object, MsgContainer, Long, FutureSalt, Int
from pyrogram.api.errors import Error
from pyrogram.api.errors import Error, InternalServerError
from pyrogram.connection import Connection
from pyrogram.crypto import AES, KDF
from .internals import MsgId, MsgFactory, DataCenter
@ -399,17 +399,19 @@ class Session:
else:
return result
def send(self, data: Object):
for i in range(self.MAX_RETRIES):
def send(self, data: Object, retries: int = MAX_RETRIES):
self.is_connected.wait(self.WAIT_TIMEOUT)
try:
return self._send(data)
except (OSError, TimeoutError):
(log.warning if i > 2 else log.info)(
"{}: {} Retrying {}".format(i, datetime.now(), type(data))
)
time.sleep(1)
continue
else:
return None
except (OSError, TimeoutError, InternalServerError) as e:
if retries == 0:
raise e from None
(log.warning if retries < 3 else log.info)(
"{}: {} Retrying {}".format(
Session.MAX_RETRIES - retries,
datetime.now(), type(data)))
time.sleep(0.5)
self.send(data, retries - 1)