Some checks failed
build-images / web (push) Has been cancelled
vision2026-ci / Python SDK resource tests (push) Has been cancelled
build-images / api (push) Has been cancelled
build-images / worker-gpu (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (comfy-workflow-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (effects-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (eventbus-types) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (growth-analytics) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (provider-adapter-kit) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (publishing-connectors) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (render-core) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (shot-template-schema) (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (trends-schema) (push) Has been cancelled
vision2026-ci / Desktop engine crates (test + clippy + fmt) (push) Has been cancelled
codeql / Analyze (javascript-typescript) (push) Has been cancelled
codeql / Analyze (python) (push) Has been cancelled
perf / API hot-path micro-smoke (budgeted) (push) Has been cancelled
perf / Desktop engine crates (release build check) (push) Has been cancelled
vision2026-ci / API phase modules (ruff + pytest) (push) Has been cancelled
vision2026-ci / Alembic upgrade + drift check (push) Has been cancelled
vision2026-ci / Shared packages (TS≡Python golden parity) (caption-engine) (push) Has been cancelled
Paid, hosted product at menivor.com, now retired as a company project. Fresh squashed history of the cleaned working tree (secret-scanned, no .env, no customer data, no outreach logs). Built on MoneyPrinterTurbo (MIT, harry0703) — see NOTICE.
59 lines
1.6 KiB
Python
59 lines
1.6 KiB
Python
"""Per-hop cooldown for the moderation provider chain.
|
|
|
|
After a hop answers 429 it is skipped for ``MENIVOR_MODERATION_COOLDOWN_S``
|
|
seconds (default 60; a larger ``Retry-After`` wins, capped at 300) so a
|
|
rate-limited provider is not hammered while the next hop serves traffic. State
|
|
is in-process (per API worker) and keyed only by hop name.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import threading
|
|
import time
|
|
|
|
DEFAULT_COOLDOWN_S = 60.0
|
|
MAX_COOLDOWN_S = 300.0
|
|
|
|
_lock = threading.Lock()
|
|
_until: dict[str, float] = {}
|
|
|
|
|
|
def _now() -> float:
|
|
return time.monotonic()
|
|
|
|
|
|
def default_cooldown_s() -> float:
|
|
raw = (os.getenv("MENIVOR_MODERATION_COOLDOWN_S") or "").strip()
|
|
try:
|
|
return min(MAX_COOLDOWN_S, max(0.0, float(raw))) if raw else DEFAULT_COOLDOWN_S
|
|
except ValueError:
|
|
return DEFAULT_COOLDOWN_S
|
|
|
|
|
|
def trip(hop: str, retry_after_s: float | None = None) -> float:
|
|
"""Start (or extend) the cooldown for ``hop``; returns the window in seconds."""
|
|
window = default_cooldown_s()
|
|
if retry_after_s is not None and retry_after_s > window:
|
|
window = min(MAX_COOLDOWN_S, retry_after_s)
|
|
with _lock:
|
|
_until[hop] = max(_until.get(hop, 0.0), _now() + window)
|
|
return window
|
|
|
|
|
|
def active(hop: str) -> bool:
|
|
with _lock:
|
|
until = _until.get(hop)
|
|
if until is None:
|
|
return False
|
|
if _now() >= until:
|
|
_until.pop(hop, None)
|
|
return False
|
|
return True
|
|
|
|
|
|
def reset() -> None:
|
|
"""Clear every cooldown (tests, operator reload)."""
|
|
with _lock:
|
|
_until.clear()
|