Skip to content

feat: Implement Circuit Breaker pattern for external services - #222

Open
Opulencechuks wants to merge 1 commit into
aid-linkk:masterfrom
Opulencechuks:feat/circuit-breaker
Open

feat: Implement Circuit Breaker pattern for external services#222
Opulencechuks wants to merge 1 commit into
aid-linkk:masterfrom
Opulencechuks:feat/circuit-breaker

Conversation

@Opulencechuks

Copy link
Copy Markdown

Closes #211

Description

This PR introduces a production-grade Circuit Breaker pattern to protect the application from cascading failures, resource exhaustion, and degraded performance when interacting with external service dependencies.

The Circuit Breaker evaluates request success rates and latencies over a sliding window (circular buffer) and manages state transitions between CLOSED, OPEN, and HALF_OPEN.

Key Changes

  • Core State Machine: Implemented in src/utils/circuitBreaker.ts using zero external dependencies. Supports configurable failure rate thresholds, p95 latency thresholds, and configurable delays. Memory bounds are strictly managed via a circular buffer.
  • Strategy Fallbacks: Defines a generic FallbackStrategy. Included FailFastFallback for services that should trigger retries or dead-letter queueing upstream, and DefaultValueFallback for soft-degrading services.
  • Horizon API Integration (horizonClient.ts): Wraps network calls so the Soroban indexer doesn't exhaust connections when Horizon is down.
  • Payment Providers (stripeProvider.ts, stellarProvider.ts): Ensures timeouts fail fast to gracefully defer to the pledge.worker.ts idempotency flow without hanging requests.
  • Third-Party Fraud API (kycFraud.service.ts): Uses a DefaultValueFallback returning null. This allows the assessment flow to gracefully omit the 3rd-party score and use local signals instead of blocking.
  • Email Service (notification.service.ts): Wraps the SMTP NodeMailer client. When open, the fallback throws natively, properly activating the existing internal retry loops and BullMQ's queue mechanics.
  • Unit Tests: circuitBreaker.test.ts thoroughly tests edge cases and timeout transitions.

Testing

  • Simulating timeouts validates fallback thresholds execute securely.
  • Tests verify bounds checks for the internal CircularBuffer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Circuit Breaker Pattern for External Dependencies

1 participant