fix(redis): handle offline state in rate limiter and mount health routes under /api/v1
Summary of Changes
This PR fixes a critical unhandled Connection is closed error during authentication/API requests when Redis is offline or reconnected, and resolves 404 Not Found errors when accessing /api/v1/health/* endpoints through the Next.js BFF proxy.
Key Improvements
1. Rate Limiter Resilience & Fail-Open Behavior
-
Redis Readiness Guard: Updated
createRateLimitStoreinrate-limit.middleware.tsto checkisRedisConnected(). If Redis is offline or not ready, it cleanly falls back to the in-memoryMemoryStore. -
Fail-Open Strategy: Added
passOnStoreError: trueto bothrateLimitMiddlewareandauthRateLimiter. Any transient Redis hiccups or network drops will no longer throw an unhandled 500 error or block user logins.
2. Redis Connection Lifecycle
-
Connection Status Validation: Added exported
isRedisConnected()helper inredis-client.tsto verifyclient.status === 'ready'. -
Dead Instance Cleanup: Automatically reset
generalClient = nullwhen Redis connection transitions into'end'or'close'states, or when initial handshake fails during server startup.
3. Health Routes Mounting
-
BFF Proxy Compatibility: Mounted
healthRouteunderrouter.use("/health", healthRoute)insrc/routes/index.ts, making/api/v1/health/readiness,/liveness, and/metricsaccessible to the frontend proxy while preserving root-level/healthfor container orchestrators.