Import source: app/backend/src/redis
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// Single Redis client for the whole backend (sessions, rate-limit, view buffer).
|
||||
// Lazy-connected so test suites can swap it out before connection.
|
||||
import { Redis } from 'ioredis';
|
||||
import { config } from './config.js';
|
||||
|
||||
export const redis = new Redis(config.REDIS_URL, {
|
||||
lazyConnect: true,
|
||||
// Reconnect with exponential backoff up to 5s; ioredis handles command queuing while reconnecting.
|
||||
retryStrategy: (times: number) => Math.min(times * 200, 5000),
|
||||
enableReadyCheck: true,
|
||||
});
|
||||
|
||||
redis.on('error', (err: Error) => {
|
||||
// Don't crash on transient errors — Fastify will surface 503s via /health.
|
||||
console.error('[redis]', err.message);
|
||||
});
|
||||
|
||||
export async function connectRedis() {
|
||||
if (redis.status === 'wait' || redis.status === 'end') {
|
||||
await redis.connect();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user