The circuit breaker pattern prevents cascading failures by "tripping" after
consecutive errors and blocking calls while the downstream service recovers.
Implement createCircuitBreaker(threshold, cooldown):
threshold — consecutive failures to open the circuit.cooldown — number of blocked calls before a test call is allowed.States:
cooldown blocked calls → HALF_OPEN. - Success → CLOSED (reset failure counter).
- Failure → OPEN (reset cooldown counter).
cb.call(fn) — fn is () => result (throws on failure).
Returns the fn result, or throws 'CIRCUIT_OPEN' when blocked.
Sample tests