aboutsummaryrefslogtreecommitdiffstats
path: root/lib/lightning.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-17 16:05:00 -0800
committerAdam Soltys <[email protected]>2023-01-17 16:05:00 -0800
commit33229e38a910e26083175a3e272278745378114c (patch)
treec63b3fc8c5e9563ae0aa694617ebd3419a3fac8b /lib/lightning.js
parent98a611d8965d8b896d124c78bdc1f7ceea6593cf (diff)
downloadcoinos-server-33229e38a910e26083175a3e272278745378114c.tar.gz
coinos-server-33229e38a910e26083175a3e272278745378114c.zip
redlock
Diffstat (limited to 'lib/lightning.js')
-rw-r--r--lib/lightning.js67
1 files changed, 56 insertions, 11 deletions
diff --git a/lib/lightning.js b/lib/lightning.js
index be5929b7..57667be2 100644
--- a/lib/lightning.js
+++ b/lib/lightning.js
@@ -4,7 +4,8 @@ import { notify } from "$lib/notifications";
import { callWebhook } from "$lib/webhooks";
import ln from "$lib/ln";
import { l, err, warn } from "$lib/logging";
-import { g, s, db } from "$lib/db";
+import { g, s, t, db } from "$lib/db";
+import Redlock from "redlock";
export async function listenForLightning() {
let {
@@ -50,22 +51,66 @@ export async function listenForLightning() {
invoice.status = "paid";
invoice.received += received;
+
+ const redlock = new Redlock([db]);
+
await s(`invoice:${hash}`, invoice);
+ console.log("BAL", await g(`balance:${uid}`));
+ setTimeout(async () => {
+ let lock = await redlock.acquire(["b"], 5000);
+ try {
+ let bal = await g(`balance:${uid}`);
+ console.log("b bal", bal);
+ await new Promise(r => setTimeout(r, 50));
- let t = async () => {
- await db.watch(`balance:${uid}`);
- let b = await g(`balance:${uid}`);
- b += received;
- let m = await db.multi();
- await s(`balance:${uid}`, b);
- if (!(await m.exec())) t();
- };
- await t();
+ await s(`balance:${uid}`, bal + 100);
+ console.log("set b bal", bal + 100);
+ } finally {
+ await lock.release();
+ }
+ // try {
+ // console.log("tweaking balance");
+ // await s(`balance:${uid}`, 0);
+ // await new Promise(r => setTimeout(r, 5));
+ // console.log("mwahaha", await g(`balance:${uid}`));
+ // await new Promise(r => setTimeout(r, 600));
+ // console.log("hmm", await g(`balance:${uid}`));
+ // } catch (e) {
+ // console.log(e);
+ // }
+ }, 50);
+
+ await new Promise(r => setTimeout(r, 5));
+
+ let lock = await redlock.acquire(["a"], 5000);
+ try {
+ let bal = await g(`balance:${uid}`);
+ console.log("a bal", bal, received);
+ await new Promise(r => setTimeout(r, 500));
+
+ await s(`balance:${uid}`, bal + received);
+ console.log("set a bal", bal + received);
+ } finally {
+ await lock.release();
+ }
+
+ // await t(async signal => {
+ // console.log("acquiring lock");
+ // let bal = await g(`balance:${uid}`);
+ // console.log("bal", bal);
+ // await new Promise(r => setTimeout(r, 500));
+ //
+ // if (signal.aborted) throw signal.error;
+ //
+ // await s(`balance:${uid}`, bal + received);
+ // console.log("set bal", bal + received);
+ // });
+ console.log("done", await g(`balance:${uid}`));
payment.invoice = invoice;
await s(`payment:${hash}`, payment);
- await db.lPush(`${uid}:payments`, hash);
+ await db.lpush(`${uid}:payments`, hash);
callWebhook(invoice, payment);