Skip to content

The agent that grows with you

refactor(telegram): build PerChatUpdateProcessor on SimpleUpdateProcessor

kshitijk4poor03395f9de713
diff --git a/plugins/platforms/telegram/update_admission.py b/plugins/platforms/telegram/update_admission.py
index 5e6bbb02246..4557902afe3 100644
--- a/plugins/platforms/telegram/update_admission.py
+++ b/plugins/platforms/telegram/update_admission.py
@@ -17,7 +17,7 @@ from dataclasses import dataclass
from functools import wraps
from telegram import Update
-from telegram.ext import Application, ApplicationHandlerStop, BaseUpdateProcessor, ConversationHandler
+from telegram.ext import Application, ApplicationHandlerStop, ConversationHandler, SimpleUpdateProcessor
from gateway.platforms.helpers import bounded_put
from utils import atomic_json_write
@@ -125,7 +125,7 @@ class _ErrorCallback:
return await self.__wrapped__(update, context)
-class PerChatUpdateProcessor(BaseUpdateProcessor):
+class PerChatUpdateProcessor(SimpleUpdateProcessor):
"""Concurrent across chats, FIFO within one chat.
PTB's default processor (max_concurrent_updates=1) awaits every update inline, so one slow
@@ -165,16 +165,10 @@ class PerChatUpdateProcessor(BaseUpdateProcessor):
else:
prev.add_done_callback(release)
- async def initialize(self) -> None:
- pass
-
- async def shutdown(self) -> None:
- pass
-
def build_update_processor(extra, name: str) -> PerChatUpdateProcessor:
"""``platforms.telegram.extra.max_concurrent_updates`` (default 32); bad values warn and fall back."""
- raw = (extra or {}).get("max_concurrent_updates", DEFAULT_MAX_CONCURRENT_UPDATES)
+ raw = extra.get("max_concurrent_updates", DEFAULT_MAX_CONCURRENT_UPDATES)
try:
limit = int(raw)
except (TypeError, ValueError):
diff --git a/tests/gateway/conftest.py b/tests/gateway/conftest.py
index a8ffb5bd934..bdffb2ba820 100644
--- a/tests/gateway/conftest.py
+++ b/tests/gateway/conftest.py
@@ -166,11 +166,11 @@ def _ensure_telegram_mock() -> None:
# PerChatUpdateProcessor subclasses this at import time: a MagicMock base
# would turn the subclass itself into a mock that fails on its second call.
- class BaseUpdateProcessor:
+ class SimpleUpdateProcessor:
def __init__(self, max_concurrent_updates):
self.max_concurrent_updates = max_concurrent_updates
- mod.BaseUpdateProcessor = BaseUpdateProcessor
+ mod.SimpleUpdateProcessor = SimpleUpdateProcessor
for name in (
"telegram",
diff --git a/tests/plugins/test_telegram_update_admission.py b/tests/plugins/test_telegram_update_admission.py
index 85d3b9edeb3..0f74738512c 100644
--- a/tests/plugins/test_telegram_update_admission.py
+++ b/tests/plugins/test_telegram_update_admission.py
@@ -22,6 +22,7 @@ from gateway.config import PlatformConfig
from gateway.platforms.event import MessageType
from plugins.platforms.telegram import adapter as tg_adapter
from plugins.platforms.telegram.adapter import TelegramAdapter
+from plugins.platforms.telegram.update_admission import DEFAULT_MAX_CONCURRENT_UPDATES, PerChatUpdateProcessor
class NoNetwork(BaseRequest):
@@ -800,14 +801,14 @@ async def test_connect_builds_concurrent_update_processor(monkeypatch):
update deafens every other chat (and local commands) until it finishes. The adapter
must enable a bounded concurrent processor, overridable via config extra (#125098)."""
async with connected(monkeypatch) as (adapter, app, delivered):
- assert app.concurrent_updates > 1
- assert app._update_processor.max_concurrent_updates == app.concurrent_updates
+ assert isinstance(app.update_processor, PerChatUpdateProcessor)
+ assert app.concurrent_updates == DEFAULT_MAX_CONCURRENT_UPDATES
async with connected(monkeypatch, extra={"max_concurrent_updates": 7}) as (adapter, app, delivered):
assert app.concurrent_updates == 7
async with connected(monkeypatch, extra={"max_concurrent_updates": "lots"}) as (adapter, app, delivered):
- assert app.concurrent_updates == 32
+ assert app.concurrent_updates == DEFAULT_MAX_CONCURRENT_UPDATES
# Cancelling a waiting same-chat update must neither fail its running predecessor nor
# let the next update of that chat overtake it.