aboutsummaryrefslogtreecommitdiffstats
path: root/lib/db.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/db.js')
-rw-r--r--lib/db.js17
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/db.js b/lib/db.js
index 76653c81..c21e224f 100644
--- a/lib/db.js
+++ b/lib/db.js
@@ -1,8 +1,6 @@
import { createClient } from "redis";
-export const db = createClient({
- url: "redis://rd"
-});
+export const db = createClient({ url: "redis://db" });
db.on("error", err => console.log("db error", err));
@@ -12,3 +10,16 @@ export default db;
export let g = async k => JSON.parse(await db.get(k));
export let s = (k, v) => db.set(k, JSON.stringify(v));
+
+let retries = {};
+export let t = async (k, f) => {
+ retries[k] = retries[k] ? retries[k]++ : 0;
+ await db.watch(k);
+ let o = f(await g(k));
+ let m = await db.multi();
+ await s(k, o);
+
+ if (retries[k] < 10) {
+ if (!(await m.exec())) setTimeout(() => t(k, f), 100);
+ } else throw new Error("could not obtain lock");
+};