From 3c5e9c347a4b1a48d19d1a20ab4ace6f19852770 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 10 Nov 2026 07:26:34 -0700 Subject: nwcpush: a claim says when the request was already answered, can be handed back, and /publish returns once one relay has the reply. A background zap's worker asked /answered, then (after starting the wallet) /claim, one after the other, and then sat in /publish until the slowest of three relays answered — about four seconds after its payment had settled. /claim now refuses a request that already has a reply with {ok:false, answered:true} (old workers read ok:false and stand down, as they should), takes {release:true} from a holder that can't make good on it (the wallet's server unreachable), and /publish marks the request answered at once, answers after the first relay's word and logs how long the first and the last took MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5.2 --- nwcpush/server.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'nwcpush') diff --git a/nwcpush/server.js b/nwcpush/server.js index 7f4cfe1..bdf30aa 100644 --- a/nwcpush/server.js +++ b/nwcpush/server.js @@ -611,9 +611,21 @@ const server = Bun.serve({ if (JSON.stringify(ev).length > 4096) return json({ error: 'event too large' }, 400); if (!verifyEvent(ev)) return json({ error: 'bad signature' }, 400); const eTag = (ev.tags || []).find((t) => t[0] === 'e')?.[1]; - try { await Promise.allSettled(pool.publish(RELAYS, ev)); } catch {} - if (eTag) answeredIds.set(eTag, Date.now()); - log(`published a reply for request ${(eTag || '?').slice(0, 12)}`); + // The worker waits on this call with the phone's clock running: answer + // as soon as one relay has the reply, and let the slow ones finish + // behind it (waiting for all three held every background zap's worker + // about four seconds after its payment had settled). + const t0 = Date.now(); + if (eTag) answeredIds.set(eTag, t0); + let sends = []; + try { sends = pool.publish(RELAYS, ev); } catch {} + const all = Promise.allSettled(sends); + let firstMs = null; + try { + await Promise.race([Promise.any(sends), new Promise((r) => setTimeout(r, 4000))]); + firstMs = Date.now() - t0; + } catch { await all; } + all.then((rs) => log(`published a reply for request ${(eTag || '?').slice(0, 12)} (${rs.filter((r) => r.status === 'fulfilled').length}/${rs.length} relays, first ${firstMs ?? '-'} ms, all ${Date.now() - t0} ms)`)); return json({ ok: true }); } @@ -692,6 +704,14 @@ const server = Bun.serve({ if (!/^[0-9a-f]{64}$/.test(id || '') || !by) return json({ error: 'event and by required' }, 400); if (claims.size > 5000) pruneRequests(); const cur = claims.get(id); + // a claim its holder can't make good on, handed back + if (body.release) { + if (cur && cur.by === by) claims.delete(id); + return json({ ok: true, released: true }); + } + // already replied to: nothing left to claim (saves the caller asking + // /answered first — one round trip of a zap's wait, not two) + if (answeredIds.has(id)) return json({ ok: false, answered: true }); if (cur && cur.by !== by && Date.now() - cur.ts < 300_000) return json({ ok: false, by: cur.by }); claims.set(id, { by, ts: Date.now() }); return json({ ok: true }); -- cgit v1.2.3