Spaces:
Running on Zero
Running on Zero
fix: default rate_limiter to memory:// when local redis is used
Browse files12345678910111 unmodified line1314151617181920212223242526272829303132"""Detection Service — Rate LimiterUses slowapi with Redis storage (or in-memory fallback if Redis unavailable).Per-token rate limiting is handled by security.py via Redis."""from __future__ import annotationsimport socketfrom slowapi import Limiterfrom slowapi.util import get_remote_address1 unmodified linedef _make_limiter() -> Limiter:"""Create limiter lazily — gracefully fallback to memory if local Redis is unreachable."""settings = get_settings()storage_uri = settings.redis_urlif "localhost" in storage_uri or "127.0.0.1" in storage_uri:try:sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)sock.settimeout(0.5)res = sock.connect_ex(("127.0.0.1", 6379))sock.close()if res != 0:storage_uri = "memory://"except Exception:storage_uri = "memory://"return Limiter(key_func=get_remote_address,123456789101 unmodified line12131415161718192021222324"""Detection Service — Rate LimiterUses slowapi with in-memory storage (or Redis if explicitly configured with external REDIS_URL).Per-token rate limiting is handled by security.py via Redis/memory."""from __future__ import annotationsfrom slowapi import Limiterfrom slowapi.util import get_remote_address1 unmodified linedef _make_limiter() -> Limiter:"""Create limiter — defaults to memory:// for cloud/standalone deployments unless remote Redis specified."""settings = get_settings()storage_uri = settings.redis_url# Fallback to in-memory rate limiting if redis_url points to local or unconfigured hostif not storage_uri or "localhost" in storage_uri or "127.0.0.1" in storage_uri:storage_uri = "memory://"return Limiter(key_func=get_remote_address,
app/core/rate_limiter.py+6-14