fix(telegram): back off on transient polling failures

Give getUpdates a wider HTTP timeout than the long-poll window, log transient SSL/network disconnects as warnings, apply exponential backoff with recovery logging, and only escalate to error after five consecutive failures.
This commit is contained in:
kairee committed 2026-10-31 22:01:05 +08:00
1 parent 2d42d8ed55
commit 71a0e8c1d6
3 files changed
+96 -7

No files matched your search

+14 -3
View File
@@ -86,9 +86,17 @@ class TelegramClient:
self.poll_timeout = poll_timeout
self.logger = logger
def _call(self, method: str, params: Mapping[str, Any] | None = None) -> Any:
def _call(
self,
method: str,
params: Mapping[str, Any] | None = None,
*,
timeout: float | None = None,
) -> Any:
url = f"https://api.telegram.org/bot{self.token}/{method}"
payload = self.transport.post(url, params or {}, timeout=self.timeout)
payload = self.transport.post(
url, params or {}, timeout=self.timeout if timeout is None else timeout
)
if not isinstance(payload, Mapping):
raise TelegramError("Telegram 响应不是对象")
if not payload.get("ok"):
@@ -145,7 +153,10 @@ class TelegramClient:
}
if offset is not None:
params["offset"] = int(offset)
result = self._call("getUpdates", params)
# 长轮询本身会挂起 poll_timeout 秒,HTTP 读超时必须留出更大余量。
result = self._call(
"getUpdates", params, timeout=self.poll_timeout + 15
)
return result if isinstance(result, list) else []
def answer_callback(self, callback_id: str, text: str = "") -> Any: