tests: 429 rate-limiting fix — polling 5s/8s/12s/20s, 429-backoff 5s/10s/20s/40s, rate limiter 15 calls/min
This commit is contained in:
@@ -94,10 +94,10 @@ class RateLimiter:
|
||||
"""Simple token-bucket rate limiter.
|
||||
|
||||
Allows max_tokens calls per window_seconds. Thread-safe via lock.
|
||||
Default: 30 calls/minute (was 60 — RC triggers 429 at 3-4 calls in <10s).
|
||||
Default: 15 calls/minute (was 30 — RC triggers 429 aggressively).
|
||||
"""
|
||||
|
||||
def __init__(self, max_tokens: int = 30, window_seconds: float = 60.0):
|
||||
def __init__(self, max_tokens: int = 15, window_seconds: float = 60.0):
|
||||
self.max_tokens = max_tokens
|
||||
self.window_seconds = window_seconds
|
||||
self.tokens = float(max_tokens)
|
||||
@@ -138,7 +138,7 @@ _rate_limiter: RateLimiter | None = None
|
||||
def _get_rate_limiter() -> RateLimiter:
|
||||
global _rate_limiter
|
||||
if _rate_limiter is None:
|
||||
_rate_limiter = RateLimiter(max_tokens=30, window_seconds=60.0)
|
||||
_rate_limiter = RateLimiter(max_tokens=15, window_seconds=60.0)
|
||||
return _rate_limiter
|
||||
|
||||
|
||||
|
||||
@@ -24,13 +24,13 @@ from .rc_rest import RCRestClient
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Progressive backoff intervals in seconds (rate-limiter-friendly).
|
||||
# Default: 2s -> 3s -> 5s -> 8s (max 18s total).
|
||||
# Default: 5s -> 8s -> 12s -> 20s (max 45s total).
|
||||
# Callers can override the first interval via initial_interval.
|
||||
_DEFAULT_BACKOFF_INTERVALS = [2.0, 3.0, 5.0, 8.0]
|
||||
_MAX_TOTAL_WAIT = 18.0 # seconds
|
||||
_DEFAULT_BACKOFF_INTERVALS = [5.0, 8.0, 12.0, 20.0]
|
||||
_MAX_TOTAL_WAIT = 45.0 # seconds
|
||||
|
||||
# 429 exponential backoff: 3s -> 6s -> 12s -> 20s -> 30s (max)
|
||||
_MAX_429_BACKOFF = 30.0
|
||||
# 429 exponential backoff: 5s -> 10s -> 20s -> 40s (max)
|
||||
_MAX_429_BACKOFF = 40.0
|
||||
# After 5 consecutive 429s, abort with SKIP instead of retrying forever
|
||||
_MAX_CONSECUTIVE_429 = 5
|
||||
|
||||
@@ -89,7 +89,7 @@ def wait_for_bot_message(
|
||||
cached_messages: list = [] # Cursor caching: reuse fetched batch
|
||||
|
||||
# Build backoff intervals with caller's initial_interval
|
||||
backoff_intervals = [initial_interval, 3.0, 5.0, 8.0]
|
||||
backoff_intervals = [initial_interval, 8.0, 12.0, 20.0]
|
||||
|
||||
while time.monotonic() < deadline:
|
||||
# Cursor caching: if we already have messages, scan them first
|
||||
@@ -137,8 +137,8 @@ def wait_for_bot_message(
|
||||
"aggressive. Consider increasing poll intervals or "
|
||||
"reducing concurrent test load."
|
||||
)
|
||||
# Exponential backoff on rate-limit errors: 3s -> 6s -> 12s -> 20s -> 30s
|
||||
backoff = min(3.0 * (2 ** (consecutive_429 - 1)), _MAX_429_BACKOFF)
|
||||
# Exponential backoff on rate-limit errors: 5s -> 10s -> 20s -> 40s
|
||||
backoff = min(5.0 * (2 ** (consecutive_429 - 1)), _MAX_429_BACKOFF)
|
||||
logger.warning(
|
||||
"Poll: HTTP 429 (consecutive=%d/%d) -- backing off %ds",
|
||||
consecutive_429, _MAX_CONSECUTIVE_429, backoff,
|
||||
@@ -170,12 +170,12 @@ def wait_for_bot_message(
|
||||
)
|
||||
return msg
|
||||
|
||||
# Progressive backoff: initial_interval -> 3s -> 5s -> 8s
|
||||
# Progressive backoff: initial_interval -> 8s -> 12s -> 20s
|
||||
if interval_idx < len(backoff_intervals):
|
||||
delay = backoff_intervals[interval_idx]
|
||||
interval_idx += 1
|
||||
else:
|
||||
delay = 8.0
|
||||
delay = 20.0
|
||||
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
|
||||
@@ -77,9 +77,9 @@ class RCRestClient:
|
||||
|
||||
req = urllib.request.Request(url, data=body, headers=headers, method=method)
|
||||
|
||||
# 429 retry with exponential backoff: 3s → 6s → 12s → 20s → 30s (max 5 retries)
|
||||
# Total possible wait: 71s — gives RC enough time to reset its rate-limit window.
|
||||
_429_BACKOFFS = [3.0, 6.0, 12.0, 20.0, 30.0]
|
||||
# 429 retry with exponential backoff: 5s → 10s → 20s → 40s (max 4 retries)
|
||||
# Total possible wait: 75s — gives RC enough time to reset its rate-limit window.
|
||||
_429_BACKOFFS = [5.0, 10.0, 20.0, 40.0]
|
||||
max_429_retries = len(_429_BACKOFFS)
|
||||
for attempt in range(max_429_retries + 1):
|
||||
# Rate-limit before every API call (protects post, history, delete, etc.)
|
||||
|
||||
Reference in New Issue
Block a user