RPC breaker: bench a provider only for health failures (timeouts, connection errors, rate limits, 5xx); request errors such as invalid block range just move to the next provider

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-12 11:40:15 -05:00
parent f417d416da
commit 8d3fc12862
+4 -1
View File
@@ -100,6 +100,9 @@ function rpcOnce(url, method, params, timeoutMs) {
// 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.
// bench only for provider-health failures (timeouts, connection errors, rate limits, 5xx). A deterministic JSON-RPC reply such as
// "invalid block range" or "query returned more than N results" is about the request, so the next provider is tried without benching.
const benchable = e => /timeout|timed out|abort|ECONN|EAI_AGAIN|ENOTFOUND|EPIPE|socket|hang up|429|too many|rate.?limit|temporar|internal error|service unavailable|50[234]/i.test(String(e && e.message || e));
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) {
@@ -113,7 +116,7 @@ async function rpc(method, params, timeoutMs) {
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)'); }
catch (e) { last = e; const bench = benchable(e); if (bench) rpcBenched.set(url, Date.now() + RPC_BENCH_MS); console.warn('rpc', method, 'failed on', url, '->', String(e && e.message || e).slice(0, 80), bench ? '(benched 10 min)' : '(request error, next provider)'); }
}
if (round < 1) await new Promise(r => setTimeout(r, 400));
}