diff options
| author | Adam Soltys <[email protected]> | 2023-01-17 12:30:30 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-01-17 12:30:30 -0800 |
| commit | 5ae4209579287c571559edcf00dce24754553aaf (patch) | |
| tree | f897e334e3781345af66b11cd7a2b3b712912354 /lib/db.js | |
| parent | e918a818acf789a6619cbf4432f0c4713d4c53e6 (diff) | |
| download | coinos-server-5ae4209579287c571559edcf00dce24754553aaf.tar.gz coinos-server-5ae4209579287c571559edcf00dce24754553aaf.zip | |
rename db
Diffstat (limited to 'lib/db.js')
| -rw-r--r-- | lib/db.js | 17 |
1 files changed, 14 insertions, 3 deletions
@@ -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"); +}; |
