Files
rengine/shared/redis.py
T
Yogesh OjhaandClaude Sonnet 5 1b44da479f refactor(redis): one connection per process, shared across packages
mcp/, toolbox/ and the api's rate limiter each held their own lazy
Redis client, three copies of the same lru-singleton pattern reaching
into api.core.ratelimit from worker-side code that has no FastAPI app.
shared/redis.py is the one place that builds the async and sync
clients; ratelimit, throttle, mcp/limits and toolbox/store now import
from it. pyproject.toml's isort first-party list gains the packages
that have shipped since it was last written (channels, connectors,
interest, mcp, reports, stages, toolbox).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VckaDx6mBB4TsG76u61gvv
2026-09-16 08:03:19 +05:30

26 lines
668 B
Python

"""One Redis connection per process, for every package that keeps state there."""
from __future__ import annotations
import redis
import redis.asyncio as aioredis
from shared.config import base_settings
_async: aioredis.Redis | None = None
_sync: redis.Redis | None = None
def async_client() -> aioredis.Redis:
global _async # noqa: PLW0603
if _async is None:
_async = aioredis.from_url(base_settings().redis_url, decode_responses=True)
return _async
def sync_client() -> redis.Redis:
global _sync # noqa: PLW0603
if _sync is None:
_sync = redis.from_url(base_settings().redis_url, decode_responses=True)
return _sync