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

Migrated telegram rate limiter to sliding window

Alishahryar189edeaa6ac82
diff --git a/messaging/limiter.py b/messaging/limiter.py
index d7ca641a..7e6a5a0b 100644
--- a/messaging/limiter.py
+++ b/messaging/limiter.py
@@ -2,19 +2,71 @@
Global Rate Limiter for Messaging Platforms.
Centralizes outgoing message requests and ensures compliance with rate limits
-using a leaky bucket algorithm (aiolimiter) and a task queue.
+using a strict sliding window algorithm and a task queue.
"""
import asyncio
import logging
import os
from collections import deque
+import time
from typing import Awaitable, Callable, Any, Optional, Dict
-from aiolimiter import AsyncLimiter
logger = logging.getLogger(__name__)
+class SlidingWindowLimiter:
+ """Strict sliding window limiter.
+
+ Guarantees: at most `rate_limit` acquisitions in any interval of length
+ `rate_window` (seconds).
+
+ Implemented as an async context manager so call sites can do:
+ async with limiter:
+ ...
+ """
+
+ def __init__(self, rate_limit: int, rate_window: float) -> None:
+ if rate_limit <= 0:
+ raise ValueError("rate_limit must be > 0")
+ if rate_window <= 0:
+ raise ValueError("rate_window must be > 0")
+
+ self._rate_limit = int(rate_limit)
+ self._rate_window = float(rate_window)
+ self._times: deque[float] = deque()
+ self._lock = asyncio.Lock()
+
+ async def acquire(self) -> None:
+ while True:
+ wait_time = 0.0
+ async with self._lock:
+ now = time.monotonic()
+ cutoff = now - self._rate_window
+
+ while self._times and self._times[0] <= cutoff:
+ self._times.popleft()
+
+ if len(self._times) < self._rate_limit:
+ self._times.append(now)
+ return
+
+ oldest = self._times[0]
+ wait_time = max(0.0, (oldest + self._rate_window) - now)
+
+ if wait_time > 0:
+ await asyncio.sleep(wait_time)
+ else:
+ await asyncio.sleep(0)
+
+ async def __aenter__(self) -> "SlidingWindowLimiter":
+ await self.acquire()
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb) -> bool:
+ return False
+
+
class MessagingRateLimiter:
"""
A thread-safe global rate limiter for messaging.
@@ -49,7 +101,7 @@ class MessagingRateLimiter:
rate_limit = int(os.getenv("MESSAGING_RATE_LIMIT", "1"))
rate_window = float(os.getenv("MESSAGING_RATE_WINDOW", "2.0"))
- self.limiter = AsyncLimiter(rate_limit, rate_window)
+ self.limiter = SlidingWindowLimiter(rate_limit, rate_window)
# Custom queue state - using deque for O(1) popleft
self._queue_list: deque[str] = deque() # Deque of dedup_keys in order
self._queue_map: Dict[
diff --git a/pyproject.toml b/pyproject.toml
index fc0b8593..4eb8d4d2 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -15,7 +15,6 @@ dependencies = [
"websockets>=13.0",
"python-telegram-bot>=21.0",
"pydantic-settings>=2.12.0",
- "aiolimiter>=1.2.1",
"openai>=2.16.0",
]
diff --git a/tests/test_limiter.py b/tests/test_limiter.py
index 23d40870..1c562b84 100644
--- a/tests/test_limiter.py
+++ b/tests/test_limiter.py
@@ -212,3 +212,32 @@ class TestMessagingRateLimiter:
pass
assert limiter._paused_until > 0
+
+ @pytest.mark.asyncio
+ async def test_proactive_strict_sliding_window(self):
+ """
+ Proactive limiter should enforce a strict sliding window:
+ for any i, t[i+rate_limit] - t[i] >= rate_window (within tolerance).
+ """
+ os.environ["MESSAGING_RATE_LIMIT"] = "2"
+ os.environ["MESSAGING_RATE_WINDOW"] = "0.5"
+ MessagingRateLimiter._instance = None
+ limiter = await MessagingRateLimiter.get_instance()
+
+ async def acquire(i: int) -> float:
+ async def _do() -> float:
+ return time.monotonic()
+
+ return await limiter.enqueue(_do, dedup_key=f"strict:{i}")
+
+ acquired = await asyncio.gather(*(acquire(i) for i in range(5)))
+ acquired.sort()
+
+ rate_limit = 2
+ rate_window = 0.5
+ tolerance = 0.05
+ for i in range(len(acquired) - rate_limit):
+ assert acquired[i + rate_limit] - acquired[i] >= rate_window - tolerance, (
+ f"Sliding window violated at i={i}: "
+ f"dt={acquired[i + rate_limit] - acquired[i]:.3f}s"
+ )
diff --git a/uv.lock b/uv.lock
index 7cca4650..a51ed89a 100644
--- a/uv.lock
+++ b/uv.lock
@@ -2,15 +2,6 @@ version = 1
revision = 3
requires-python = "==3.14.2"
-[[package]]
-name = "aiolimiter"
-version = "1.2.1"
-source = { registry = "https://pypi.org/simple" }
-sdist = { url = "https://files.pythonhosted.org/packages/f1/23/b52debf471f7a1e42e362d959a3982bdcb4fe13a5d46e63d28868807a79c/aiolimiter-1.2.1.tar.gz", hash = "sha256:e02a37ea1a855d9e832252a105420ad4d15011505512a1a1d814647451b5cca9", size = 7185, upload-time = "2024-12-08T15:31:51.496Z" }
-wheels = [
- { url = "https://files.pythonhosted.org/packages/f3/ba/df6e8e1045aebc4778d19b8a3a9bc1808adb1619ba94ca354d9ba17d86c3/aiolimiter-1.2.1-py3-none-any.whl", hash = "sha256:d3f249e9059a20badcb56b61601a83556133655c11d1eb3dd3e04ff069e5f3c7", size = 6711, upload-time = "2024-12-08T15:31:49.874Z" },
-]
-
[[package]]
name = "annotated-doc"
version = "0.0.4"
@@ -276,7 +267,6 @@ name = "free-claude-code"
version = "2.0.0"
source = { virtual = "." }
dependencies = [
- { name = "aiolimiter" },
{ name = "fastapi", extra = ["standard"] },
{ name = "httpx" },
{ name = "markdown-it-py" },
@@ -299,7 +289,6 @@ dev = [
[package.metadata]
requires-dist = [
- { name = "aiolimiter", specifier = ">=1.2.1" },
{ name = "fastapi", extras = ["standard"], specifier = ">=0.115.11" },
{ name = "httpx", specifier = ">=0.25.0" },
{ name = "markdown-it-py", specifier = ">=3.0.0" },