2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-08-29 05:18:10 +00:00

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