INNER CODE UNIT · Python
_respect_rate_limit
OpenOSINT/OpenOSINT · actors/news-monitor/src/main.py:65
async def _respect_rate_limit() -> None:
"""Never issue a GDELT request less than 5s after the previous one."""
global _last_request_at
now = time.monotonic()
wait = _MIN_REQUEST_INTERVAL_SECONDS - (now - _last_request_at)
if wait > 0:
await asyncio.sleep(wait)
_last_request_at = time.monotonic()
async def fetch_with_backoff(query: str, timespan_minutes: int) -> dict:
"""Fetch GDELT DOC data, retrying with backoff (longer after a 429)."""
last_exc: Exception | None = None
for attempt in range(_MAX_ATTEMPTS):
await _respect_rate_limit()
try:
return await asyncio.to_thread(
fetch_gdelt_doc_data,