Skip to content

The agent that grows with you

refactor(telegram): share the flood-wait cap between send and edit paths

Kshitij Kapoor1db0a7d825ae
diff --git a/plugins/platforms/telegram/adapter.py b/plugins/platforms/telegram/adapter.py
index a5433f5ed84..9624fe5f69b 100644
--- a/plugins/platforms/telegram/adapter.py
+++ b/plugins/platforms/telegram/adapter.py
@@ -311,6 +311,25 @@ from plugins.platforms.telegram.telegram_network import (
from utils import atomic_replace, env_float, env_int
_TELEGRAM_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".webp", ".gif"}
+
+# Max seconds a send/edit coroutine may sleep inline on a Telegram
+# flood-control RetryAfter. Longer server penalties fail closed with a
+# ``flood_control:{wait}`` SendResult so the caller's retry machinery
+# (delivery ledger, streaming fallback) owns the wait instead of the
+# coroutine pinning its worker — a 97-minute penalty on the boot path
+# froze inbound on every platform (#91969).
+_FLOOD_INLINE_WAIT_CAP_SECS = 5.0
+
+
+def _flood_cap_result(wait: float) -> "SendResult":
+ """The shared fail-closed SendResult for an over-cap flood wait."""
+ return SendResult(
+ success=False,
+ error=f"flood_control:{wait}",
+ retry_after=float(wait),
+ )
+
+
_TELEGRAM_IMAGE_MIME_TO_EXT = {
"image/png": ".png",
"image/jpeg": ".jpg",
@@ -5449,20 +5468,17 @@ class TelegramAdapter(BasePlatformAdapter):
# pinned send() for 97 minutes in production and
# froze inbound on every platform when it ran on
# the gateway boot path (#91969).
- if wait > 5.0:
+ if wait > _FLOOD_INLINE_WAIT_CAP_SECS:
logger.warning(
"[%s] Telegram flood control on send "
- "(retry_after=%.1fs > 5s); failing closed "
+ "(retry_after=%.1fs > %.0fs); failing closed "
"instead of sleeping: %s",
self.name,
wait,
+ _FLOOD_INLINE_WAIT_CAP_SECS,
safe_send_error,
)
- return SendResult(
- success=False,
- error=f"flood_control:{wait}",
- retry_after=float(wait),
- )
+ return _flood_cap_result(wait)
if _send_attempt < 2:
logger.warning(
"[%s] Telegram flood control on send (attempt %d/3), retrying in %.1fs: %s",
@@ -5714,12 +5730,8 @@ class TelegramAdapter(BasePlatformAdapter):
"[%s] Telegram flood control, waiting %.1fs",
self.name, wait,
)
- if wait > 5.0:
- return SendResult(
- success=False,
- error=f"flood_control:{wait}",
- retry_after=float(wait),
- )
+ if wait > _FLOOD_INLINE_WAIT_CAP_SECS:
+ return _flood_cap_result(wait)
await asyncio.sleep(wait)
try:
await self._bot.edit_message_text(