RPC circuit breaker for InstantAdPay: bench a failing provider for 10 min, 4 s timeout on eth_call, log provider failures; log any request over 3 s (members felt slow sign-ins; the sticky index was paying the 15 s timeout on every call when a node hung)
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -83,13 +83,13 @@ 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;
|
||||
function rpcOnce(url, method, params) {
|
||||
function rpcOnce(url, method, params, timeoutMs) {
|
||||
rpcCount(method);
|
||||
return new Promise((resolve, reject) => {
|
||||
const body = JSON.stringify({ jsonrpc: '2.0', id: 1, method, params });
|
||||
const u = new URL(url);
|
||||
const req = https.request({ hostname: u.hostname, path: u.pathname + u.search, method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }, timeout: 15000 },
|
||||
headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(body) }, timeout: timeoutMs || 15000 },
|
||||
res => { let d = ''; res.on('data', c => d += c); res.on('end', () => {
|
||||
try { const j = JSON.parse(d); if (j.error) reject(new Error(j.error.message)); else resolve(j.result); }
|
||||
catch (e) { reject(e); } }); });
|
||||
@@ -97,20 +97,25 @@ function rpcOnce(url, method, params) {
|
||||
req.end(body);
|
||||
});
|
||||
}
|
||||
async function rpc(method, params) {
|
||||
// Circuit breaker (2026-10-12, ported from RM Circle): a provider that fails or hangs is benched for RPC_BENCH_MS so the next
|
||||
// calls skip it instead of each paying the full timeout in turn (a dead node at the sticky index was stalling every member
|
||||
// lookup, which members felt as slow sign-ins). eth_call reads use a 4 s timeout; logs/blocks keep 15 s. Failures are logged.
|
||||
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) {
|
||||
const urls = getConfig().rpcs;
|
||||
let last;
|
||||
// 3 rounds over the RPC pool with backoff — the public Amoy nodes routinely
|
||||
// return transient "Temporary internal error. Please retry" on eth_call, and a
|
||||
// single miss was silently surfacing as 0 credits / chainReadError on the
|
||||
// dashboard. Retry so a real read isn't lost to a momentary node hiccup.
|
||||
for (let round = 0; round < 3; round++) {
|
||||
for (let i = 0; i < urls.length; i++) {
|
||||
const url = urls[(rpcIdx + i) % urls.length];
|
||||
try { const r = await rpcOnce(url, method, params); rpcIdx = (rpcIdx + i) % urls.length; return r; }
|
||||
catch (e) { last = e; }
|
||||
// two passes: healthy providers first (round-robin from the last good one), benched ones only as a last resort; a short
|
||||
// backoff before the second pass covers the public nodes' transient "Temporary internal error" replies.
|
||||
for (let round = 0; round < 2; round++) {
|
||||
const now = Date.now(); const order = [], 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);
|
||||
for (const url of order) {
|
||||
try { const r = await rpcOnce(url, method, params, timeoutMs); rpcIdx = urls.indexOf(url); rpcBenched.delete(url); return r; }
|
||||
catch (e) { last = e; rpcBenched.set(url, Date.now() + RPC_BENCH_MS); console.warn('rpc', method, 'failed on', url, '->', String(e && e.message || e).slice(0, 80), '(benched 10 min)'); }
|
||||
}
|
||||
if (round < 2) await new Promise(r => setTimeout(r, 400 * (round + 1)));
|
||||
if (round < 1) await new Promise(r => setTimeout(r, 400));
|
||||
}
|
||||
throw last || new Error('all RPCs failed');
|
||||
}
|
||||
@@ -131,7 +136,7 @@ function decodeString(data, wordIdx) {
|
||||
}
|
||||
async function call(sel, args) {
|
||||
const data = sel + (args || []).map(a => pad(a)).join('');
|
||||
return rpc('eth_call', [{ to: getConfig().contract, data }, 'latest']);
|
||||
return rpc('eth_call', [{ to: getConfig().contract, data }, 'latest'], ETH_CALL_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
// ---- reads ----
|
||||
|
||||
@@ -1691,6 +1691,7 @@ async function gameAnswer(g, slug, acct) { // the game token (only once a userna
|
||||
}
|
||||
|
||||
const server = http.createServer(async (req, res) => {
|
||||
{ const t0 = Date.now(); res.on('finish', () => { const ms = Date.now() - t0; if (ms > 3000) console.warn('slow request', req.method, String(req.url || '').split('?')[0].slice(0, 80), ms + 'ms', res.statusCode); }); } // visibility for member-felt delays (2026-10-12)
|
||||
try {
|
||||
const u = new URL(req.url, 'http://x');
|
||||
const p = u.pathname;
|
||||
|
||||
Reference in New Issue
Block a user