mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-04-30 14:56:01 +00:00
fix(telegram): handle RetryAfter delay internally in channel (#2552)
This commit is contained in:
parent
94e6d569b3
commit
ebf6201b42
@ -432,7 +432,9 @@ class TelegramChannel(BaseChannel):
|
|||||||
await self._send_text(chat_id, chunk, reply_params, thread_kwargs)
|
await self._send_text(chat_id, chunk, reply_params, thread_kwargs)
|
||||||
|
|
||||||
async def _call_with_retry(self, fn, *args, **kwargs):
|
async def _call_with_retry(self, fn, *args, **kwargs):
|
||||||
"""Call an async Telegram API function with retry on pool/network timeout."""
|
"""Call an async Telegram API function with retry on pool/network timeout and RetryAfter."""
|
||||||
|
from telegram.error import RetryAfter
|
||||||
|
|
||||||
for attempt in range(1, _SEND_MAX_RETRIES + 1):
|
for attempt in range(1, _SEND_MAX_RETRIES + 1):
|
||||||
try:
|
try:
|
||||||
return await fn(*args, **kwargs)
|
return await fn(*args, **kwargs)
|
||||||
@ -445,6 +447,15 @@ class TelegramChannel(BaseChannel):
|
|||||||
attempt, _SEND_MAX_RETRIES, delay,
|
attempt, _SEND_MAX_RETRIES, delay,
|
||||||
)
|
)
|
||||||
await asyncio.sleep(delay)
|
await asyncio.sleep(delay)
|
||||||
|
except RetryAfter as e:
|
||||||
|
if attempt == _SEND_MAX_RETRIES:
|
||||||
|
raise
|
||||||
|
delay = float(e.retry_after)
|
||||||
|
logger.warning(
|
||||||
|
"Telegram Flood Control (attempt {}/{}), retrying in {:.1f}s",
|
||||||
|
attempt, _SEND_MAX_RETRIES, delay,
|
||||||
|
)
|
||||||
|
await asyncio.sleep(delay)
|
||||||
|
|
||||||
async def _send_text(
|
async def _send_text(
|
||||||
self,
|
self,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user