Stop hammering a dependency that is already down; fail fast, then cautiously probe for recovery.
A circuit breaker wraps calls to an unreliable remote dependency and moves between three states so that cascading failures don't take down the whole system.
States:
0.failureThreshold, the breaker trips: it prints "TRIPPED -> OPEN" and records the trip timestamp."FAST_FAIL (Circuit Open)".recoveryTimeoutMs have elapsed since the trip, the next call is treated as a probe and the breaker moves to HALF_OPEN before evaluating that call's outcome.0, transition to CLOSED, print "PROBE SUCCESS -> CLOSED"."PROBE FAILED -> OPEN".Successful calls in CLOSED print "OK (<consecutiveFailures> failures)" and failed calls that don't trip the breaker print "FAIL (<consecutiveFailures>/<failureThreshold>)".
Input format: CONFIG:<failureThreshold>,<recoveryTimeoutMs> then CALL:<timestampMs>,<OK|FAIL>.
Input: CONFIG:2,1000;CALL:0,FAIL;CALL:50,FAIL;CALL:100,FAIL;CALL:1200,FAIL;CALL:2300,OK;CALL:2350,FAIL
Output: FAIL (1/2)
TRIPPED -> OPEN
FAST_FAIL (Circuit Open)
PROBE FAILED -> OPEN
PROBE SUCCESS -> CLOSED
FAIL (1/2)