feat(risk-management): production-ready circuit breaker & health checks#17
Open
BigBen-7 wants to merge 1 commit into
Open
feat(risk-management): production-ready circuit breaker & health checks#17BigBen-7 wants to merge 1 commit into
BigBen-7 wants to merge 1 commit into
Conversation
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.
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 #11
This PR aligns the
investment/risk-managementmodule to production standards by replacing the minimal circuit breaker stub with a full implementation backed by@nestjs/terminushealth reporting andEventEmitter2event emission.What changed
CircuitBreakerService(rewrite)CLOSED→OPEN→HALF_OPEN→CLOSEDwith clean transition logging.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)isOpen()call transitions toHALF_OPEN. One successfulrecordSuccess()closes the circuit; onerecordFailure()reopens it.circuit-breaker.opened{ serviceName, failureCount, timestamp }circuit-breaker.closed{ serviceName, timestamp }RiskManagementServiceEventEmitter2and emitsrisk.threshold.breached{ userId, alert }for every alert produced bycalculatePortfolioRisk, enabling the alerts module to subscribe without coupling.RiskManagementHealthIndicator(new)HealthIndicatorfrom@nestjs/terminus.isHealthy('risk-management')reports healthy when the circuit isCLOSEDorHALF_OPEN, unhealthy (throwsHealthCheckError) whenOPEN.HealthModule/HealthController(new —src/health/)TerminusModule+RiskManagementModuleimported.GET /api/v1/healthnow returns a proper terminus health-check response including therisk-managementindicator.{ status, timestamp }handler inAppController.RiskManagementModuleEventEmitterModule.forRoot().RiskManagementHealthIndicator.AppModuleEventEmitterModule.forRoot()globally.HealthModule.Tests
19 unit tests in
circuit-breaker.service.spec.tscovering:isOpenreturns false.circuit-breaker.openedevent, auto-transition to HALF_OPEN after recovery time.circuit-breaker.closedevent; failed probe → OPEN +circuit-breaker.openedevent.circuit-breaker.closedonly when previously non-CLOSED.Test plan
npm run build— webpack compiled successfully ✅npx jest --testPathPattern="circuit-breaker"— 19/19 passed ✅GET /api/v1/healthreturns terminus JSON withrisk-managementstatusPOST /api/v1/risk/circuit-breaker/resetstill works as beforeGET /api/v1/risk/circuit-breakerreturns the new richer status shape