From 791f0ca64c8c924b8fe0f8de64bd2c51961f4f99 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:19:51 +0330 Subject: [PATCH] Import source: app/backend/src/redis Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- app/backend/src/redis.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 app/backend/src/redis.ts diff --git a/app/backend/src/redis.ts b/app/backend/src/redis.ts new file mode 100644 index 0000000..83fefa4 --- /dev/null +++ b/app/backend/src/redis.ts @@ -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(); + } +}