aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/db.js')
-rw-r--r--lib/db.js34
1 files changed, 29 insertions, 5 deletions
diff --git a/lib/db.js b/lib/db.js
index 197697a0..b790917e 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -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 => {