feat: wrap all external dependencies with timeout/retry/circuit-breaker - #128
Open
MerlinTheWhiz wants to merge 1 commit into
Open
feat: wrap all external dependencies with timeout/retry/circuit-breaker#128MerlinTheWhiz wants to merge 1 commit into
MerlinTheWhiz wants to merge 1 commit into
Conversation
Guard every outbound integration — MongoDB, Stellar (Horizon + RPC), Pinata, Nodemailer, Redis, and webhook dispatcher — with consistent timeout, retry, and circuit-breaker primitives. Translate dependency errors to proper HTTP 503 responses, emit circuit-breaker metrics, propagate traceparent/correlation IDs, and add integration tests. Also fixes pre-existing module corruption in entitlement.js.
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.
Description
Closes #97
Guards every external dependency behind consistent timeout, retry, and circuit-breaker policies. Adds structured error translation so clients receive safe, actionable HTTP responses. Propagates correlation IDs across the entire request lifecycle.
Changes
Resilience primitives (
src/lib/resilience/):withTimeout(promise, ms, label)— races against a deadline with optional external abort signalwithRetry(fn, opts)— bounded exponential backoff with full jitter, gated by an idempotency flag (non-idempotent writes never retry)createCircuitBreaker(name, opts)— in-memory state machine (CLOSED → OPEN → HALF_OPEN → CLOSED) with configurable thresholds andonStateChangecallback for metricsDependencyError— structured error carryingdependency,action,retryable,statusCode, anduserMessageAdapter wrappers (each wraps its external call with the primitives above):
src/lib/mongodb.jssrc/lib/stellar/horizonClient.jssrc/lib/stellar/rpcClient.js, wired intoentitlement.js,stellarIndexer.js, andready/route.jscallPinata()wrapper insrc/lib/pinata.js; 7 call sites migrated across upload routes and ready routewithEmailResilience()insrc/lib/email.jssrc/lib/cache/redis.jssrc/lib/webhooks/dispatcher.jsError translation (
src/lib/api/hardening.js):DependencyErrorand returns HTTP 503 with{ error, dependency }bodycircuit_breaker_open_totalcounter on each 503 responseMetrics (all adapters):
circuit_breaker_stategauge set on every state transitioncircuit_breaker_open_totalcounter incremented on OPEN transitionsrpc_errors_totalcounter on CB-open fast-failCorrelation propagation:
traceparentheader on RPC and webhook requestsx-correlation-idheader on error responses via hardening layerFix: Removed dead code and duplicate module content in
src/lib/entitlement.jsthat prevented the file from loading.Testing
63 integration tests across 8 files covering:
Checklist