Skip to content

Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)

Python

implemented task compaction for telegram rate limiter

Alishahryar15465fefdd3a8
diff --git a/messaging/limiter.py b/messaging/limiter.py
index 98ba9963..37db3ce5 100644
--- a/messaging/limiter.py
+++ b/messaging/limiter.py
@@ -18,8 +18,8 @@ class GlobalRateLimiter:
"""
A thread-safe global rate limiter for messaging.
- Uses an asyncio.Queue to accept tasks and a background worker to
- process them at a rate defined by MESSAGING_RATE_LIMIT and MESSAGING_RATE_WINDOW.
+ Uses a custom queue with task compaction (deduplication) to ensure
+ only the latest version of a message update is processed.
"""
_instance: Optional["GlobalRateLimiter"] = None
@@ -27,8 +27,6 @@ class GlobalRateLimiter:
def __new__(cls, *args, **kwargs):
if not cls._instance:
- # Note: The actual initialization happens in __init__
- # but we use a singleton pattern for the global limiter.
pass
return super(GlobalRateLimiter, cls).__new__(cls)
@@ -51,14 +49,18 @@ class GlobalRateLimiter:
rate_window = float(os.getenv("MESSAGING_RATE_WINDOW", "2.0"))
self.limiter = AsyncLimiter(rate_limit, rate_window)
- self.queue: asyncio.Queue[
- tuple[Callable[[], Awaitable[Any]], asyncio.Future]
- ] = asyncio.Queue()
+ # Custom queue state
+ self._queue_list: List[str] = [] # List of dedup_keys in order
+ self._queue_map: Dict[
+ str, tuple[Callable[[], Awaitable[Any]], asyncio.Future]
+ ] = {}
+ self._condition = asyncio.Condition()
+
self._initialized = True
self._paused_until = 0
logger.info(
- f"GlobalRateLimiter initialized ({rate_limit} req / {rate_window}s)"
+ f"GlobalRateLimiter initialized ({rate_limit} req / {rate_window}s with Task Compaction)"
)
async def _worker(self):
@@ -67,7 +69,12 @@ class GlobalRateLimiter:
while True:
try:
# Get a task from the queue
- func, future = await self.queue.get()
+ async with self._condition:
+ while not self._queue_list:
+ await self._condition.wait()
+
+ dedup_key = self._queue_list.pop(0)
+ func, future = self._queue_map.pop(dedup_key)
# Check for manual pause (FloodWait)
now = asyncio.get_event_loop().time()
@@ -88,10 +95,8 @@ class GlobalRateLimiter:
# Handle Telegram FloodWaitError specifically
error_msg = str(e).lower()
if "flood" in error_msg or "wait" in error_msg:
- # Attempt to extract seconds if possible, else default to 30
seconds = 30
try:
- # Telethon's FloodWaitError usually has .seconds
if hasattr(e, "seconds"):
seconds = e.seconds
except:
@@ -102,37 +107,60 @@ class GlobalRateLimiter:
asyncio.get_event_loop().time() + seconds
)
- # Re-queue the task at the front if possible,
- # but simple approach is to retry after pause
+ # Re-queue the task at the front (as a high priority update)
+ await self._enqueue_internal(
+ func, future, dedup_key, front=True
+ )
await asyncio.sleep(seconds)
- # Simple retry: put it back
- await self.queue.put((func, future))
else:
if not future.done():
future.set_exception(e)
- finally:
- self.queue.task_done()
except asyncio.CancelledError:
break
except Exception as e:
logger.error(f"Error in limiter worker: {e}", exc_info=True)
await asyncio.sleep(1)
- async def enqueue(self, func: Callable[[], Awaitable[Any]]) -> Any:
+ async def _enqueue_internal(self, func, future, dedup_key, front=False):
+ async with self._condition:
+ if dedup_key in self._queue_map:
+ # Compaction: Update existing task with new func, keep old future
+ old_func, old_future = self._queue_map[dedup_key]
+ self._queue_map[dedup_key] = (func, old_future)
+ # Chain them so the new one resolves the old caller's future
+ # but we actually just use the same future object for simplicity
+ # The user just wants to know when "their" request is done,
+ # which is now the new request's completion.
+ logger.debug(f"Compacted task for key: {dedup_key}")
+ else:
+ self._queue_map[dedup_key] = (func, future)
+ if front:
+ self._queue_list.insert(0, dedup_key)
+ else:
+ self._queue_list.append(dedup_key)
+ self._condition.notify_all()
+
+ async def enqueue(
+ self, func: Callable[[], Awaitable[Any]], dedup_key: Optional[str] = None
+ ) -> Any:
"""
Enqueue a messaging task and return its future result.
-
- This makes the call non-blocking for the caller if they don't await the result,
- or they can await it to get the message_id/result.
+ If dedup_key is provided, subsequent tasks with the same key will replace this one.
"""
+ if dedup_key is None:
+ # Unique key to avoid deduplication
+ dedup_key = f"task_{id(func)}_{asyncio.get_event_loop().time()}"
+
future = asyncio.get_event_loop().create_future()
- await self.queue.put((func, future))
+ await self._enqueue_internal(func, future, dedup_key)
return await future
- def fire_and_forget(self, func: Callable[[], Awaitable[Any]]):
+ def fire_and_forget(
+ self, func: Callable[[], Awaitable[Any]], dedup_key: Optional[str] = None
+ ):
"""Enqueue a task without waiting for the result."""
+ if dedup_key is None:
+ dedup_key = f"task_{id(func)}_{asyncio.get_event_loop().time()}"
+
future = asyncio.get_event_loop().create_future()
- # We don't await the put because we want it to be really fast
- # but asyncio.Queue.put is a coroutine.
- # However, in most cases it won't block unless queue is full.
- asyncio.create_task(self.queue.put((func, future)))
+ asyncio.create_task(self._enqueue_internal(func, future, dedup_key))
diff --git a/messaging/telegram.py b/messaging/telegram.py
index 40004e6e..2f6bb3fe 100644
--- a/messaging/telegram.py
+++ b/messaging/telegram.py
@@ -201,10 +201,11 @@ class TelegramPlatform(MessagingPlatform):
async def _edit():
return await self.edit_message(chat_id, message_id, text, parse_mode)
+ dedup_key = f"edit:{chat_id}:{message_id}"
if fire_and_forget:
- self._limiter.fire_and_forget(_edit)
+ self._limiter.fire_and_forget(_edit, dedup_key=dedup_key)
else:
- await self._limiter.enqueue(_edit)
+ await self._limiter.enqueue(_edit, dedup_key=dedup_key)
def fire_and_forget(self, task: Awaitable[Any]) -> None:
"""Execute a coroutine without awaiting it."""
diff --git a/tests/verify_limiter.py b/tests/verify_limiter.py
index a178759a..1b78ed9f 100644
--- a/tests/verify_limiter.py
+++ b/tests/verify_limiter.py
@@ -1,97 +1,57 @@
import asyncio
-import os
-import sys
import time
-from unittest.mock import AsyncMock, MagicMock
+import os
+from messaging.limiter import GlobalRateLimiter
-# Add current directory to path
-sys.path.append(os.getcwd())
+# Mocking logging for test
+import logging
-from messaging.limiter import GlobalRateLimiter
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
-async def test_rate_limiting():
- print("\n--- Testing Rate Limiting (1 req / 2 sec) ---")
+async def test_compaction():
+ # Set small rate for testing
os.environ["MESSAGING_RATE_LIMIT"] = "1"
- os.environ["MESSAGING_RATE_WINDOW"] = "2.0"
+ os.environ["MESSAGING_RATE_WINDOW"] = "1.0"
limiter = await GlobalRateLimiter.get_instance()
- start_time = time.time()
- results = []
-
- async def mock_task(i):
- ts = time.time() - start_time
- print(f"[{ts:.2f}s] Task {i} started")
- await asyncio.sleep(0.1) # Simulate network latency
- print(f"[{time.time() - start_time:.2f}s] Task {i} completed")
- return i
-
- # Enqueue 5 tasks simultaneously
- tasks = [limiter.enqueue(lambda i=i: mock_task(i)) for i in range(5)]
-
- print("Tasks enqueued, waiting for results...")
- completed = await asyncio.gather(*tasks)
+ call_counts = {}
- total_time = time.time() - start_time
- print(f"All tasks completed in {total_time:.2f}s")
- print(f"Results: {completed}")
+ async def mock_edit(msg_id, content):
+ call_counts[msg_id] = call_counts.get(msg_id, 0) + 1
+ logger.info(f"Executing edit for {msg_id}: {content}")
+ return f"done_{content}"
- # Check if timing is correct: 5 tasks at 1 req / 2 sec should take about 8 seconds
- # T0: Task 0
- # T2: Task 1
- # T4: Task 2
- # T6: Task 3
- # T8: Task 4
- if total_time >= 8.0:
- print("[SUCCESS] Rate limiting working as expected!")
- else:
- print(f"[FAILURE] Rate limiting failed! Took only {total_time:.2f}s")
-
-
-async def test_flood_wait():
- print("\n--- Testing FloodWait Recovery ---")
- limiter = await GlobalRateLimiter.get_instance()
-
- # Reset limiter for fresh test
- limiter._paused_until = 0
+ print("\n--- Starting Compaction Test ---")
+ # Spam 5 edits for the same message
start_time = time.time()
+ futures = []
+ for i in range(5):
+ # We use fire_and_forget to fill the queue fast
+ limiter.fire_and_forget(
+ lambda i=i: mock_edit("msg1", f"update_{i}"), dedup_key="edit:msg1"
+ )
+ print(f"Queued update_{i}")
- class FloodError(Exception):
- def __init__(self, seconds):
- self.seconds = seconds
- super().__init__(f"FloodWait for {seconds}s")
-
- attempt = 0
-
- async def task_that_fails_once():
- nonlocal attempt
- ts = time.time() - start_time
- attempt += 1
- if attempt == 1:
- print(f"[{ts:.1f}s] First attempt: Simulating FloodWait(5s)")
- raise FloodError(5)
- print(f"[{ts:.1f}s] Second attempt: Success!")
- return "success"
-
- result = await limiter.enqueue(task_that_fails_once)
- total_time = time.time() - start_time
- print(f"Task completed with result: {result} in {total_time:.1f}s")
+ # Wait for processing (should take ~2 seconds total for 1-2 actual calls)
+ # The first one might go through immediately if limiter has capacity
+ # The next 4 should be compacted into 1 if they arrive before the first one finishes?
+ # Actually, the FIRST one is already "popped" by the worker while the loop is still queueing.
+ # So we expect roughly 2 calls: the 1st one, and the LAST one (all intermediate ones compacted).
- if total_time >= 5.0:
- print("[SUCCESS] FloodWait recovery working as expected!")
- else:
- print(f"[FAILURE] FloodWait recovery failed! Took only {total_time:.1f}s")
+ await asyncio.sleep(2.5)
+ print(f"\nTotal calls to Telegram: {sum(call_counts.values())}")
+ print(f"Call breakdown: {call_counts}")
-async def main():
- await test_rate_limiting()
- await test_flood_wait()
- print("\nVerification complete.")
- # Exit script
- os._exit(0)
+ assert call_counts["msg1"] <= 2, (
+ f"Should have at most 2 calls (first and last), but got {call_counts['msg1']}"
+ )
+ print("✅ Compaction test passed!")
if __name__ == "__main__":
- asyncio.run(main())
+ asyncio.run(test_compaction())