Files
romm/backend/utils/rate_limiter.py
Spinnich 5fb27a55f9 feat(screenscraper): make scanning aware of the account's reported limits
ScreenScraper returns the account's allowances on every response, but only
maxthreads was read. Use the rest:

- Pace requests against the reported maxrequestspermin (falling back to the
  documented threads x 50), so fast lookups can no longer overrun the
  per-minute budget without ever exceeding the thread cap.
- Report the remaining daily and unrecognized-ROM quotas after each platform
  and at the end of a scan, and warn once when either drops below 10%.
- Hint once per scan when SCAN_WORKERS is below the thread allowance (scans
  slower than the account allows) or above it (extra workers just queue).
- Raise a distinct error when a request is refused twice for exceeding the
  rate, so the ROM is reported as skipped in the scan summary instead of being
  saved with no ScreenScraper data.
- Route ScreenScraper media downloads (covers, screenshots, manuals, mixed
  media) through the same limiters, since they are api2 endpoints that count
  against the same allowances, and derive their timeout from the account's
  advertised download speed.

Fixes #3978

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-07-28 17:09:52 +00:00

121 lines
4.2 KiB
Python

import asyncio
from collections import deque
class RateLimiter:
"""Pre-emptive async rate limiter.
Spaces grants so callers stay at or below ``requests_per_second``. Concurrent
callers each reserve the next slot before awaiting, so they are evenly spaced
instead of being released in a burst.
"""
def __init__(self, requests_per_second: float) -> None:
if requests_per_second <= 0:
raise ValueError("requests_per_second must be positive")
self._min_interval = 1.0 / requests_per_second
self._next_slot = 0.0
@property
def requests_per_second(self) -> float:
return 1.0 / self._min_interval
def set_requests_per_second(self, requests_per_second: float) -> None:
"""Re-pace the limiter, for APIs that report their own per-account rate.
Slots already reserved keep their time; only later ones use the new
spacing.
"""
if requests_per_second <= 0:
raise ValueError("requests_per_second must be positive")
self._min_interval = 1.0 / requests_per_second
async def acquire(self) -> None:
# Reserving the slot is a read-then-write on _next_slot with no await
# between, so it is atomic on the single-threaded loop and needs no lock.
loop = asyncio.get_running_loop()
now = loop.time()
slot = max(now, self._next_slot)
self._next_slot = slot + self._min_interval
delay = slot - now
if delay > 0:
await asyncio.sleep(delay)
class ConcurrencyLimiter:
"""Caps the number of in-flight operations, with a runtime-adjustable capacity.
It suits APIs that enforce a per-account thread/connection cap (e.g. ScreenScraper)
rather than a call rate. Because a slot is held for the whole request, slow
responses can never cause overlapping requests to exceed the cap.
Use it as an async context manager so the slot is always released, even if the
wrapped request raises:
async with limiter:
await do_request()
"""
def __init__(self, max_concurrency: int) -> None:
if max_concurrency < 1:
raise ValueError("max_concurrency must be at least 1")
self._max_concurrency = max_concurrency
self._in_flight = 0
self._waiters: deque[asyncio.Future[None]] = deque()
@property
def max_concurrency(self) -> int:
return self._max_concurrency
@property
def in_flight(self) -> int:
return self._in_flight
def set_max_concurrency(self, max_concurrency: int) -> None:
if max_concurrency < 1:
raise ValueError("max_concurrency must be at least 1")
previous = self._max_concurrency
self._max_concurrency = max_concurrency
# Wake one waiter per newly opened slot; each re-checks capacity itself.
for _ in range(max(0, max_concurrency - previous)):
self._wake_next()
async def acquire(self) -> None:
# Re-check on every wake-up, as another coroutine may have taken the slot,
# or the capacity may have been lowered while we waited.
while self._in_flight >= self._max_concurrency:
loop = asyncio.get_running_loop()
waiter = loop.create_future()
self._waiters.append(waiter)
try:
try:
await waiter
finally:
self._waiters.remove(waiter)
except asyncio.CancelledError:
# We were granted a slot but cancelled before using it, so pass the
# grant on so a waiting peer is not stranded.
if not waiter.cancelled():
self._wake_next()
raise
self._in_flight += 1
def release(self) -> None:
if self._in_flight > 0:
self._in_flight -= 1
self._wake_next()
def _wake_next(self) -> None:
for waiter in self._waiters:
if not waiter.done():
waiter.set_result(None)
return
async def __aenter__(self) -> "ConcurrencyLimiter":
await self.acquire()
return self
async def __aexit__(self, *exc_info: object) -> None:
self.release()