fix: share a single async Redis client per URL to avoid duplicate connection pools

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Hugh Brown
2026-03-04 14:13:45 -07:00
committed by Abhimanyu Saharan
parent a30b94c887
commit e053fd4a46
2 changed files with 15 additions and 3 deletions

View File

@@ -171,7 +171,7 @@ def _make_redis_limiter(
window_seconds: float = 60.0,
) -> RedisRateLimiter:
"""Build a RedisRateLimiter wired to a _FakeRedis instance."""
with patch("redis.asyncio.from_url", return_value=fake):
with patch("app.core.rate_limit._get_async_redis", return_value=fake):
return RedisRateLimiter(
namespace=namespace,
max_requests=max_requests,
@@ -285,7 +285,7 @@ def test_factory_returns_redis_when_configured(monkeypatch: pytest.MonkeyPatch)
monkeypatch.setattr("app.core.config.settings.rate_limit_backend", RateLimitBackend.REDIS)
monkeypatch.setattr("app.core.config.settings.rate_limit_redis_url", "redis://localhost:6379/0")
fake = _FakeRedis()
with patch("redis.asyncio.from_url", return_value=fake):
with patch("app.core.rate_limit._get_async_redis", return_value=fake):
limiter = create_rate_limiter(namespace="test", max_requests=10, window_seconds=60.0)
assert isinstance(limiter, RedisRateLimiter)