43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { Redis } from "npm:ioredis";
|
|
|
|
const VALKEY_URL = Deno.env.get("VALKEY_URL") ||
|
|
(import.meta.main ? "redis://auth-valkey:6379" : "");
|
|
|
|
export const valkey = VALKEY_URL
|
|
? new Redis(VALKEY_URL, {
|
|
enableOfflineQueue: false,
|
|
maxRetriesPerRequest: 1,
|
|
retryStrategy: (times) => (times > 3 ? null : Math.min(times * 100, 1000)),
|
|
})
|
|
: new Redis({
|
|
lazyConnect: true,
|
|
enableOfflineQueue: false,
|
|
maxRetriesPerRequest: 1,
|
|
retryStrategy: () => null,
|
|
});
|
|
|
|
valkey.on("error", (err) => {
|
|
if (Deno.env.get("DEBUG_VALKEY")) {
|
|
console.warn("[Valkey] Connection warning:", err.message);
|
|
}
|
|
});
|
|
|
|
export async function pingValkey(): Promise<void> {
|
|
if (!VALKEY_URL) return; // Skip in test
|
|
try {
|
|
const result = await valkey.ping();
|
|
if (result !== "PONG") {
|
|
throw new Error(`Unexpected ping response: ${result}`);
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof Error) {
|
|
throw new Error(
|
|
`Fatal: Failed to connect to Valkey session cache. Halting boot. ${error.message}`,
|
|
);
|
|
}
|
|
throw new Error(
|
|
`Fatal: Failed to connect to Valkey session cache. Halting boot.`,
|
|
);
|
|
}
|
|
}
|