Retry sending messages if server returns 502

This commit is contained in:
Tulir Asokan
2020-11-09 21:01:30 +02:00
parent 04c64949e7
commit 0b3014ff88
2 changed files with 27 additions and 4 deletions
+26 -3
View File
@@ -30,7 +30,8 @@ from telethon.errors import (AuthBytesInvalidError, AuthKeyInvalidError, Locatio
SecurityError, FileIdInvalidError)
from mautrix.appservice import IntentAPI
from mautrix.types import EncryptedFile
from mautrix.types import ContentURI
from mautrix.errors import MatrixRequestError, MatrixConnectionError
from ..tgclient import MautrixTelegramClient
from ..db import TelegramFile as DBTelegramFile
@@ -107,6 +108,28 @@ def _location_to_id(location: TypeLocation) -> str:
return f"{location.volume_id}-{location.local_id}"
bad_gateway_sleep = 5
async def _try_upload_media(intent: IntentAPI, file: bytes, mime: str, retries: int = 2
) -> ContentURI:
while True:
try:
return await intent.upload_media(file, mime)
except MatrixRequestError as e:
if not retries or e.http_status not in (502, 504):
raise
log.warning("Got gateway error trying to upload media, retrying in "
f"{bad_gateway_sleep} seconds")
except MatrixConnectionError as e:
if not retries:
raise
log.warning(f"Got connection error trying to upload media: {e}, retrying in "
f"{bad_gateway_sleep} seconds")
await asyncio.sleep(bad_gateway_sleep)
retries -= 1
async def transfer_thumbnail_to_matrix(client: MautrixTelegramClient, intent: IntentAPI,
thumbnail_loc: TypeLocation, mime_type: str, encrypt: bool,
video: Optional[bytes], custom_data: Optional[bytes] = None,
@@ -145,7 +168,7 @@ async def transfer_thumbnail_to_matrix(client: MautrixTelegramClient, intent: In
if encrypt:
file, decryption_info = encrypt_attachment(file)
upload_mime_type = "application/octet-stream"
content_uri = await intent.upload_media(file, upload_mime_type)
content_uri = await _try_upload_media(intent, file, upload_mime_type)
if decryption_info:
decryption_info.url = content_uri
@@ -246,7 +269,7 @@ async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, inten
if encrypt and encrypt_attachment:
file, decryption_info = encrypt_attachment(file)
upload_mime_type = "application/octet-stream"
content_uri = await intent.upload_media(file, upload_mime_type)
content_uri = await _try_upload_media(intent, file, upload_mime_type)
if decryption_info:
decryption_info.url = content_uri
+1 -1
View File
@@ -5,6 +5,6 @@ python-magic>=0.4,<0.5
commonmark>=0.8,<0.10
aiohttp>=3,<4
yarl>=1,<2
mautrix>=0.8,<0.9
mautrix>=0.8.1,<0.9
telethon>=1.17,<1.18
telethon-session-sqlalchemy>=0.2.14,<0.3