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 <noreply@anthropic.com>
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user