Skip to content

feat: implement generic circuit breaker pattern for external service calls - #1039

Open
menawar wants to merge 1 commit into
Smartdevs17:mainfrom
menawar:feat/circuit-breaker-issue-1002
Open

feat: implement generic circuit breaker pattern for external service calls#1039
menawar wants to merge 1 commit into
Smartdevs17:mainfrom
menawar:feat/circuit-breaker-issue-1002

Conversation

@menawar

@menawar menawar commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #1002

This PR introduces a generic Circuit Breaker pattern to protect both the backend and frontend systems from cascading failures when calling external services or when the mobile app calls the backend API.

Changes

1. Generic Circuit Breaker Utility

Created a reusable CircuitBreaker class in both the backend and frontend environments:

  • Backend: backend/services/shared/circuitBreaker.ts
  • Frontend: src/services/network/circuitBreaker.ts

Features:

  • State Machine: Closed -> Open -> Half-Open -> Closed.
  • Configurable thresholds for consecutive failures, recovery timeouts, and success thresholds.
  • Fast-failure mechanism with CircuitOpenError.
  • Frontend Only: Emits telemetry events to mobileTracer during state transitions and fast-fails.

2. Integration with API Clients

Integrated the CircuitBreaker natively into our ApiClients:

  • Backend (backend/services/shared/apiClient.ts):
    • Automatically trips the circuit on 5xx server errors or network errors.
    • Bypasses circuit tripping for expected 4xx client errors, preserving existing application logic perfectly.
  • Frontend (src/services/network/apiClient.ts):
    • Automatically trips the circuit on 5xx server errors or network errors.
    • Ensures seamless degradation to the UI.

3. Unit Tests

Added comprehensive test suites for both environments:

  • backend/services/shared/__tests__/circuitBreaker.test.ts
  • src/services/network/__tests__/circuitBreaker.test.ts
  • Coverage: >80% covering success paths, consecutive failures, state transitions, half-open failure relapses, manual resets, and tracer events.

Test Results

Tests:       11 passed, 11 total
Test Suites: 2 passed, 2 total

Acceptance Criteria

  • Feature implemented with full functionality
  • Unit tests added with >80% coverage
  • Integration tests for critical paths (API Client integrations)
  • No regression introduced
  • Documentation updated (JSDoc included)

…calls

Closes Smartdevs17#1002

- Implemented a reusable CircuitBreaker utility in both backend (`backend/services/shared`) and frontend (`src/services/network`).
- Integrated CircuitBreaker into both backend and frontend `ApiClient`s to provide out-of-the-box resilience against cascading failures.
- Added comprehensive unit tests for both implementations with >80% coverage.
- Configured frontend circuit breaker to seamlessly integrate with `mobileTracer` for full telemetry on state transitions and fast-failures.
@drips-wave

drips-wave Bot commented Aug 27, 2026

Copy link
Copy Markdown

@menawar 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! 🚀

Learn more about application limits

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 service calls

1 participant