From b52b725f625d4bd380201b7f655dae0f6cf87ffa Mon Sep 17 00:00:00 2001 From: kshitij <82637225+kshitijk4poor@users.noreply.github.com> Date: Mon, 17 Aug 2026 15:53:32 +0530 Subject: [PATCH] refactor(telegram): collapse sticky state onto one sentinel _has_sticky + _sticky_ip=None overloaded "unset" and "sticky hostname". One _UNSET sentinel is enough. Drop the unused _SEED_FALLBACK_IPS alias. --- .../platforms/telegram/telegram_network.py | 29 +++++++++---------- tests/gateway/test_telegram_network.py | 4 +-- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/plugins/platforms/telegram/telegram_network.py b/plugins/platforms/telegram/telegram_network.py index 2511438b62..bdec305ff6 100644 --- a/plugins/platforms/telegram/telegram_network.py +++ b/plugins/platforms/telegram/telegram_network.py @@ -43,7 +43,7 @@ _DOH_PROVIDERS: list[dict] = [ # first-try connect targets so a blackholed IPv6 AAAA for the hostname # cannot pin initialize() (#87015). SEED_FALLBACK_IPS: list[str] = ["149.154.166.110", "149.154.167.220"] -_SEED_FALLBACK_IPS = SEED_FALLBACK_IPS # back-compat alias +_UNSET = object() def _resolve_proxy_url(target_hosts=None) -> str | None: @@ -79,12 +79,10 @@ class TelegramFallbackTransport(httpx.AsyncBaseTransport): # Built on demand and discarded on failure — see _reset_fallback. self._fallbacks: dict[str, httpx.AsyncHTTPTransport] = {} self._fallback_lock = asyncio.Lock() - # ``_has_sticky`` distinguishes "unknown path" from "sticky primary" - # (``_sticky_ip is None``). A blackholed IPv6 AAAA for the hostname - # can sit in connect() without erroring, so the first request must - # try known IPv4 literals BEFORE ``api.telegram.org`` (#87015). - self._has_sticky: bool = False - self._sticky_ip: Optional[str] = None + # ``_UNSET`` vs ``None`` vs ``str``: unset / sticky hostname / sticky IPv4. + # ``None`` cannot mean both "no sticky yet" and "sticky dual-stack + # hostname" (#87015). + self._sticky_ip: object = _UNSET self._sticky_lock = asyncio.Lock() async def _get_fallback(self, ip: str) -> httpx.AsyncHTTPTransport: @@ -134,8 +132,9 @@ class TelegramFallbackTransport(httpx.AsyncBaseTransport): kept as a last resort for IPv6-only networks. """ order: list[Optional[str]] = [] - if self._has_sticky: - order.append(self._sticky_ip) + if self._sticky_ip is not _UNSET: + sticky = self._sticky_ip + order.append(sticky if sticky is None else str(sticky)) for ip in self._fallback_ips: if ip not in order: order.append(ip) @@ -155,10 +154,9 @@ class TelegramFallbackTransport(httpx.AsyncBaseTransport): transport = self._primary if ip is None else await self._get_fallback(ip) try: response = await transport.handle_async_request(candidate) - if not self._has_sticky or self._sticky_ip != ip: + if self._sticky_ip is _UNSET or self._sticky_ip != ip: async with self._sticky_lock: - if not self._has_sticky or self._sticky_ip != ip: - self._has_sticky = True + if self._sticky_ip is _UNSET or self._sticky_ip != ip: self._sticky_ip = ip if ip is not None: logger.warning( @@ -171,11 +169,10 @@ class TelegramFallbackTransport(httpx.AsyncBaseTransport): last_error = exc if not _is_retryable_connect_error(exc): raise - if self._has_sticky and ip == self._sticky_ip: + if self._sticky_ip is not _UNSET and ip == self._sticky_ip: async with self._sticky_lock: - if self._has_sticky and self._sticky_ip == ip: - self._has_sticky = False - self._sticky_ip = None + if self._sticky_ip is not _UNSET and self._sticky_ip == ip: + self._sticky_ip = _UNSET logger.warning( "[Telegram] Sticky Telegram path %s failed; " "re-walking IPv4 literals before the hostname", diff --git a/tests/gateway/test_telegram_network.py b/tests/gateway/test_telegram_network.py index 4d9f359daa..cc599f6d77 100644 --- a/tests/gateway/test_telegram_network.py +++ b/tests/gateway/test_telegram_network.py @@ -127,7 +127,6 @@ class TestFallbackTransport: resp = await transport.handle_async_request(_telegram_request()) assert resp.status_code == 200 - assert transport._has_sticky is True assert transport._sticky_ip == "149.154.167.220" assert [c["url_host"] for c in calls] == ["149.154.167.220"] assert calls[0]["host_header"] == "api.telegram.org" @@ -208,7 +207,6 @@ class TestFallbackTransport: resp = await transport.handle_async_request(_telegram_request()) assert resp.status_code == 200 assert [c["url_host"] for c in calls] == ["149.154.167.220", "api.telegram.org"] - assert transport._has_sticky is True assert transport._sticky_ip is None @@ -227,7 +225,7 @@ class TestFallbackTransportPassthrough: assert resp.status_code == 200 assert calls[0]["url_host"] == "example.com" - assert transport._sticky_ip is None + assert transport._sticky_ip is tnet._UNSET class TestFallbackTransportInit: