diff options
| author | Adam Soltys <[email protected]> | 2026-03-29 23:40:15 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-03-29 23:40:15 -0700 |
| commit | 51428790ea558f8e34c3034e75d2e49a076261b8 (patch) | |
| tree | 82a31a86b4e1cb3a185863e2976fab753b4350c7 /lib | |
| parent | ef06f2eb48581c201e62fc2cde27b106e898e54d (diff) | |
| download | coinos-server-51428790ea558f8e34c3034e75d2e49a076261b8.tar.gz coinos-server-51428790ea558f8e34c3034e75d2e49a076261b8.zip | |
nwc heartbeat
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/changeid.ts | 13 | ||||
| -rw-r--r-- | lib/nwc.ts | 29 |
2 files changed, 28 insertions, 14 deletions
diff --git a/lib/changeid.ts b/lib/changeid.ts index 49d3a0fb..1c6a6688 100644 --- a/lib/changeid.ts +++ b/lib/changeid.ts @@ -80,7 +80,7 @@ export default async function changeid(un: string) { } else if (k.includes("user:")) { await client.del(k); } else { - await client.rename(k, nk); + if (await client.exists(k)) await client.rename(k, nk); } } catch (e: any) { err(`changeid [${label}]`, k, e.message); @@ -100,17 +100,18 @@ export default async function changeid(un: string) { } await db.del(`${id}:apps`); - // Update invoices that reference the old uid - for await (const k of db.scanIterator({ MATCH: "invoice:*" })) { + // Update uid/aid in invoice objects and their offer pointers + const invoiceIds = await db.lRange(`${nid}:invoices`, 0, -1); + for (const iid of invoiceIds) { try { - const inv = await g(k); + const inv = await g(`invoice:${iid}`); if (!inv || typeof inv === "string") continue; let changed = false; if (inv.uid === id) { inv.uid = nid; changed = true; } if (inv.aid === id) { inv.aid = nid; changed = true; } if (changed) { - await s(k, inv); - l("changeid: updated invoice", k); + await s(`invoice:${iid}`, inv); + l("changeid: updated invoice", iid); } } catch (e) {} } @@ -49,12 +49,30 @@ const nwcRequestTimes: Map<string, number[]> = new Map(); export default () => { let r: any; + let heartbeatInterval: any; function connect() { + if (heartbeatInterval) clearInterval(heartbeatInterval); r = new Relay("ws://sf:7777", { reconnect: false }); r.on("open", async (_) => { l("nwc connected to strfry"); + + // Periodically check if connection is alive + heartbeatInterval = setInterval(() => { + try { + if (!r?.ws || r.ws.readyState !== 1) { + warn("nwc heartbeat: connection dead, reconnecting"); + clearInterval(heartbeatInterval); + try { r.close(); } catch (_) {} + setTimeout(connect, 1000); + } + } catch (_) { + warn("nwc heartbeat: error, reconnecting"); + clearInterval(heartbeatInterval); + setTimeout(connect, 1000); + } + }, 30000); r.subscribe("nwc", { kinds: [23194], "#p": [serverPubkey, serverPubkey2], since: Math.floor(Date.now() / 1000) - nwcEventMaxAgeSeconds }); const info = await finalizeEvent( { @@ -87,6 +105,7 @@ export default () => { r.on("close", () => { warn("nwc strfry connection lost, reconnecting in 5s"); + if (heartbeatInterval) clearInterval(heartbeatInterval); setTimeout(connect, 5000); }); @@ -147,16 +166,10 @@ export default () => { response = await finalizeEvent(response, hexToBytes(sk)); r.send(["EVENT", response]); } catch (e) { - // err( - // "problem with nwc", - // pubkey, - // method, - // JSON.stringify(params), - // e.message, - // ); + err("problem with nwc", pubkey, method, e.message); } } catch (e) { - // err("problem with nwc", e.message); + err("problem with nwc event", e.message); } }); } |
