feat(rpc): add timeout and circuit breaker for blockchain RPC calls - #1036
Open
retkatmun wants to merge 1 commit into
Open
feat(rpc): add timeout and circuit breaker for blockchain RPC calls#1036retkatmun wants to merge 1 commit into
retkatmun wants to merge 1 commit into
Conversation
…martdevs17#941) - Add backend/services/shared/rpcTimeout.ts withRpcTimeout() — AbortController-based deadline with optional jitter; wrapWithTimeout() — Promise.race wrapper for non-signal-aware calls; RpcCallTimeoutError / RpcCallCancelledError typed errors; isRpcTimeout() / isRpcCancelled() type guards; defaultTimeoutForChain() — chain-aware defaults (Eth 10s, Polygon/Arb 15s) - Add backend/services/shared/rpcResilienceMiddleware.ts ResilientEthersProvider extends MonitoringJsonRpcProvider, adds per-send AbortController timeout + RpcCircuitBreakerService integration; createResilientProvider() factory; getOrCreateResilientProvider() singleton registry; ProviderHealthSnapshot / EndpointHealth types - Add src/services/rpcProvider.ts (client-side, React Native safe) ResilientJsonRpcProvider — drop-in for JsonRpcProvider with per-URL circuit breaker (closed→open→half-open→closed) + AbortController timeout + ordered URL fallback; getOrCreateResilientProvider() singleton registry - Update src/services/walletService.ts WalletServiceManager.getProvider() now calls getOrCreateResilientProvider() with all fallback URLs from EVM_RPC_URLS instead of new JsonRpcProvider() - Update backend/services/shared/index.ts Export rpcTimeout and rpcResilienceMiddleware public API - Add unit tests: rpcTimeout.test.ts (25+ cases), rpcResilienceMiddleware.test.ts - Add integration tests: walletServiceRpc.integration.test.ts (7 suites — timeout, circuit trips/recovers, URL fallback, per-URL circuit, registry, manual reset, audit trail) - Add benchmarks: backend/benchmark/rpcBenchmark.ts (9 benchmarks, budget gated: overhead <1ms avg, p95 <2ms, defaultTimeoutForChain >100k ops/s) - Add docs/rpc-resilience.md Closes Smartdevs17#941
|
@retkatmun Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
7 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #941
Implements production-ready timeout and circuit breaker protection for all external blockchain RPC calls in SubTrackr.
Problem
Without this fix, any slow or unreachable RPC endpoint causes:
provider.getBalance()/getGasPrice()never rejects; the caller waits foreverWhat changed
New files
backend/services/shared/rpcTimeout.tswithRpcTimeout(AbortController+jitter),wrapWithTimeout(Promise.race), typed errors, type guards,defaultTimeoutForChainbackend/services/shared/rpcResilienceMiddleware.tsResilientEthersProvider(extendsMonitoringJsonRpcProvider) + factory + singleton registrysrc/services/rpcProvider.tsResilientJsonRpcProvider— per-URL circuit breaker + timeout + ordered URL fallbackbackend/services/shared/__tests__/rpcTimeout.test.tsbackend/services/shared/__tests__/rpcResilienceMiddleware.test.tsbackend/services/shared/__tests__/walletServiceRpc.integration.test.tsbackend/benchmark/rpcBenchmark.tsdocs/rpc-resilience.mdModified files
src/services/walletService.tsgetProvider()now callsgetOrCreateResilientProvider()instead ofnew JsonRpcProvider()backend/services/shared/index.tsrpcTimeoutandrpcResilienceMiddlewarepublic APIArchitecture
Circuit breaker states
Key API
Tests
rpcTimeout.test.ts): withRpcTimeout, wrapWithTimeout, type guards, defaultTimeoutForChain, error types — 25+ casesrpcResilienceMiddleware.test.ts): factory, registry, send timeout, health snapshot, resetCircuitswalletServiceRpc.integration.test.ts): 7 suitesResilientJsonRpcProviderPerformance benchmarks
withRpcTimeoutoverheadwrapWithTimeoutoverheaddefaultTimeoutForChainAcceptance criteria
walletServicepublic API unchanged;ResilientJsonRpcProvideris a drop-in forJsonRpcProvider)docs/rpc-resilience.md)