Skip to content

feat(risk-management): production-ready circuit breaker & health checks#17

Open
BigBen-7 wants to merge 1 commit into
SourceXXL:mainfrom
BigBen-7:feat/risk-management-circuit-breaker-health
Open

feat(risk-management): production-ready circuit breaker & health checks#17
BigBen-7 wants to merge 1 commit into
SourceXXL:mainfrom
BigBen-7:feat/risk-management-circuit-breaker-health

Conversation

@BigBen-7

Copy link
Copy Markdown

Summary

Closes #11

This PR aligns the investment/risk-management module to production standards by replacing the minimal circuit breaker stub with a full implementation backed by @nestjs/terminus health reporting and EventEmitter2 event emission.


What changed

CircuitBreakerService (rewrite)

  • Three-state machine: CLOSEDOPENHALF_OPENCLOSED with clean transition logging.
  • Configurable thresholds per protected service via configure(serviceName, config):
    • failureThreshold — number of failures before opening (default: 5)
    • slowCallDurationMs — duration that qualifies a call as slow (default: 5 000 ms)
    • slowCallRateThreshold — % of slow calls that trips the breaker (default: 50 %)
    • recoveryTimeMs — time before transitioning to HALF_OPEN (default: 60 s)
  • In-memory metrics: failure count, success count, slow-call count, total call count, last failure time, last transition time — all queryable per service.
  • Half-open probe: after recovery time the next isOpen() call transitions to HALF_OPEN. One successful recordSuccess() closes the circuit; one recordFailure() reopens it.
  • Events emitted on every state transition:
    • circuit-breaker.opened { serviceName, failureCount, timestamp }
    • circuit-breaker.closed { serviceName, timestamp }
  • Per-service isolation — multiple services can each have their own breaker state and config.

RiskManagementService

  • Injected EventEmitter2 and emits risk.threshold.breached { userId, alert } for every alert produced by calculatePortfolioRisk, enabling the alerts module to subscribe without coupling.

RiskManagementHealthIndicator (new)

  • Extends HealthIndicator from @nestjs/terminus.
  • isHealthy('risk-management') reports healthy when the circuit is CLOSED or HALF_OPEN, unhealthy (throws HealthCheckError) when OPEN.

HealthModule / HealthController (new — src/health/)

  • TerminusModule + RiskManagementModule imported.
  • GET /api/v1/health now returns a proper terminus health-check response including the risk-management indicator.
  • Replaced the old basic { status, timestamp } handler in AppController.

RiskManagementModule

  • Imports EventEmitterModule.forRoot().
  • Provides and exports RiskManagementHealthIndicator.

AppModule

  • Registers EventEmitterModule.forRoot() globally.
  • Imports HealthModule.

Tests

19 unit tests in circuit-breaker.service.spec.ts covering:

  • CLOSED state: initial state, threshold not reached, success metrics, isOpen returns false.
  • OPEN state: threshold breach, circuit-breaker.opened event, auto-transition to HALF_OPEN after recovery time.
  • HALF_OPEN state: successful probe → CLOSED + circuit-breaker.closed event; failed probe → OPEN + circuit-breaker.opened event.
  • reset: counters cleared, emits circuit-breaker.closed only when previously non-CLOSED.
  • Per-service config: custom threshold respected, services isolated from each other.

Test plan

  • npm run build — webpack compiled successfully ✅
  • npx jest --testPathPattern="circuit-breaker" — 19/19 passed ✅
  • GET /api/v1/health returns terminus JSON with risk-management status
  • Calling POST /api/v1/risk/circuit-breaker/reset still works as before
  • GET /api/v1/risk/circuit-breaker returns the new richer status shape

Closes SourceXXL#11

- Rewrote CircuitBreakerService with full CLOSED/OPEN/HALF_OPEN state
  machine, configurable thresholds (failure rate, slow-call rate) per
  protected service, and in-memory metrics (failure count, success
  count, last failure time, transition timestamps).

- Half-open probe support: the first call after recovery time acts as a
  probe — success closes the circuit, failure reopens it.

- EventEmitter2 integration: emits `circuit-breaker.opened` and
  `circuit-breaker.closed` on every state transition. RiskManagementService
  emits `risk.threshold.breached` for each alert generated so the alerts
  module can subscribe without coupling.

- Added RiskManagementHealthIndicator extending HealthIndicator from
  @nestjs/terminus; reports OPEN state as unhealthy.

- Created HealthModule/HealthController that wires the terminus health
  check into GET /api/v1/health, replacing the previous basic handler.

- Registered EventEmitterModule.forRoot() in AppModule.

- 19 unit tests covering all three circuit breaker states, event
  emissions, per-service isolation, and reset behaviour.
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.

Align investment/risk-management Module — Production-Ready Circuit Breaker & Health Checks

1 participant