From 7dc537ba1f0fb0f0d6e611639e36186eaf222f19 Mon Sep 17 00:00:00 2001 From: martbost Date: Sun, 11 Oct 2026 18:21:29 -0500 Subject: [PATCH] RPC circuit breaker: bench a failing provider for 10 min, 4 s timeout on eth_call (cold member lookups were paying the 15 s timeout) Co-Authored-By: Claude Fable 5.1 --- chain.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/chain.js b/chain.js index 3715275..8a4fca7 100644 --- a/chain.js +++ b/chain.js @@ -134,10 +134,18 @@ const RPC_COUNT = { since: Date.now(), m: {} }; const rpcCount = method => { RPC_COUNT.m[method] = (RPC_COUNT.m[method] || 0) + 1; }; setInterval(() => { try { fs.writeFileSync(path.join(DATA_DIR, 'rpc-count.json'), JSON.stringify(Object.assign({ at: Date.now() }, RPC_COUNT))); } catch (e) {} }, 300000).unref(); let rpcIdx = 0; +// Circuit breaker (2026-10-11): a provider that fails or hangs is benched for RPC_BENCH_MS so the next lookups skip it instead of +// each paying the full timeout first (cold member lookups were taking 15 s whenever the round-robin landed on a hanging +// provider). Member lookups also use a short timeout: a healthy provider answers eth_call in well under a second. +const RPC_BENCH_MS = 10 * 60 * 1000; const rpcBenched = new Map(); // url -> benched until (ms) +const ETH_CALL_TIMEOUT_MS = 4000; async function rpc(method, params, timeoutMs = 15000, urls = RPCS) { let lastErr = new Error('no rpc'); - for (let i = 0; i < urls.length; i++) { - const url = urls[(rpcIdx + i) % urls.length]; + const now = Date.now(); const order = []; const benched = []; + for (let i = 0; i < urls.length; i++) { const url = urls[(rpcIdx + i) % urls.length]; ((rpcBenched.get(url) || 0) > now ? benched : order).push(url); } + order.push(...benched); // benched providers only as a last resort, after every healthy one failed + for (let i = 0; i < order.length; i++) { + const url = order[i]; rpcCount(method); try { const ctrl = new AbortController(); @@ -149,9 +157,10 @@ async function rpc(method, params, timeoutMs = 15000, urls = RPCS) { clearTimeout(t); const j = await r.json(); if (j.error) throw new Error(j.error.message || JSON.stringify(j.error)); - if (urls === RPCS) rpcIdx = (rpcIdx + i + 1) % urls.length; // round-robin: spread the load, never camp on one provider + if (urls === RPCS) rpcIdx = (rpcIdx + 1) % urls.length; // round-robin: spread the load, never camp on one provider + rpcBenched.delete(url); return j.result; - } catch (e) { lastErr = e; } + } catch (e) { lastErr = e; rpcBenched.set(url, Date.now() + RPC_BENCH_MS); console.warn('rpc', method, 'failed on', url, '->', String(e.message || e).slice(0, 80), '(benched 10 min)'); } } throw lastErr; } @@ -166,7 +175,7 @@ const topicAddr = t => '0x' + t.slice(26); const pol = wei => Number(wei / 1000000000000n) / 1e6; // 6-decimal POL const encU = v => BigInt(v).toString(16).padStart(64, '0'); -async function ethCall(data) { return await rpc('eth_call', [{ to: CONTRACT, data }, 'latest']); } +async function ethCall(data) { return await rpc('eth_call', [{ to: CONTRACT, data }, 'latest'], ETH_CALL_TIMEOUT_MS); } // Native POL balance of a wallet (public on-chain data). Used ONLY to derive a // funded / not-funded flag for a member's next upgrade — the API never returns // the raw balance, only the boolean, to keep members' holdings private.