Harden the login throttle and cache readiness aggregates (R2-S4, R2-C5; #36, #45)

S4: the throttle keyed on the first X-Forwarded-For entry, which a client
or a proxy that appends can choose, and disabled itself entirely when
Redis was down.

EVECTION_TRUSTED_PROXIES (IPs/CIDRs of your reverse proxy) makes it
strict: X-Forwarded-For is only believed when the TCP peer is a trusted
proxy, and the rightmost entry that isn't itself a trusted proxy — the
address the proxy actually saw — is used instead of the spoofable first
one. Unset keeps the previous behavior so existing installs don't
suddenly share one bucket behind a proxy, and `manage.py check --deploy`
warns (evection.W001) until it's set. During a Redis outage the throttle
now falls back to a bounded in-process limiter instead of switching off.

C5: /health/ready/ reuses its queue/feed aggregates for 15s
(EVECTION_HEALTH_CACHE_SECONDS) so a fast-polling monitor can't create
steady load; the database, Redis and poller probes stay live. Docs
cover both settings, restricting the endpoint at the proxy, and the
tombstone/purge behavior from the previous changes.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ksmithandClaude Sonnet 5.5 committed 2026-11-01 12:08:13 -07:00
1 parent aacb41d1d3
commit ac5e951749
12 files changed
+386 -53

No files matched your search

+28
View File
@@ -2,6 +2,7 @@ from datetime import timedelta
import redis
from django.conf import settings
from django.core.cache import cache
from django.db.models import Max, Min
from django.utils import timezone
@@ -42,6 +43,33 @@ def _success_window() -> timedelta:
return timedelta(minutes=max(3 * base, MIN_SUCCESS_WINDOW_MINUTES))
PIPELINE_CACHE_KEY = "health-ready-pipeline"
def cached_fetch_pipeline_status() -> tuple[dict[str, bool], dict]:
"""fetch_pipeline_status, reused for EVECTION_HEALTH_CACHE_SECONDS
(R2-C5): the queue/feed aggregates behind it cost real queries, and a
monitor that polls /health/ready/ every second shouldn't turn that
into recurring database and Redis load. The cheap liveness probes
(database, Redis, poll heartbeat) are not cached — those stay live.
A cache failure just means computing it fresh."""
ttl = settings.EVECTION_HEALTH_CACHE_SECONDS
if ttl > 0:
try:
cached = cache.get(PIPELINE_CACHE_KEY)
except Exception:
cached = None
if cached is not None:
return cached
result = fetch_pipeline_status()
if ttl > 0:
try:
cache.set(PIPELINE_CACHE_KEY, result, timeout=ttl)
except Exception:
pass
return result
def fetch_pipeline_status(now=None) -> tuple[dict[str, bool], dict]:
"""Checks that fetching is actually *succeeding*, not just scheduled
(the poll heartbeat only proves beat and a worker are alive): returns