diff options
| author | Adam Soltys <[email protected]> | 2023-11-30 15:34:45 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-11-30 15:34:45 -0800 |
| commit | a7322928c45848cfb9984454bf8ece6e3439c673 (patch) | |
| tree | 9b1031c31ee1f8160306ac66749afe0802f8aa54 /lib/db.js | |
| parent | acb93f29e83b35c0917d88ebbc62812e2dff989f (diff) | |
| download | coinos-server-a7322928c45848cfb9984454bf8ece6e3439c673.tar.gz coinos-server-a7322928c45848cfb9984454bf8ece6e3439c673.zip | |
zaps and emails
Diffstat (limited to 'lib/db.js')
| -rw-r--r-- | lib/db.js | 34 |
1 files changed, 29 insertions, 5 deletions
@@ -1,7 +1,7 @@ import config from "$config"; -import { sleep } from "$lib/utils"; +import { wait, sleep } from "$lib/utils"; import { createClient, defineScript } from "redis"; -import { warn } from "$lib/logging"; +import { warn, l, err } from "$lib/logging"; let SCRIPT = ` local balanceKey = KEYS[1] @@ -33,11 +33,35 @@ let debit = defineScript({ transformArguments: (...args) => args.map(a => a.toString()) }); -export const db = createClient({ +export let db = createClient({ url: config.db, - scripts: { debit } + scripts: { debit }, + socket: { + reconnectStrategy: retries => Math.min(retries * 50, 5000) + } +}); + + +async function connectWithRetry() { + try { + await db.connect(); + } catch (err) { + console.error('Failed to connect to Redis, retrying...', err); + setTimeout(connectWithRetry, 5000); // Retry after 5 seconds + } +} + +connectWithRetry(); + +db.on("error", e => { + if (e.message.startsWith("getaddr")) return; + err("Redis error", e.message); }); -await db.connect(); + +db.on("end", () => { + warn("Redis connection ended"); +}); + export default db; export let g = async k => { |
