Practice ยท 1 of 1
CLOSED โ OPEN โ HALF-OPEN โ CLOSED
Implement CircuitBreaker(threshold=3, cooldown=30.0) with allow(now) and record(now, ok):
- starts CLOSED; allow returns True
- each record(now, False) in CLOSED increments failures; reaching threshold trips state to OPEN and stamps _opened_at = now
- while OPEN, allow returns False until now - _opened_at >= cooldown, then switches to HALF-OPEN and returns True exactly once (the probe)
- record(now, True) in any state: resets failures and closes the circuit
- record(now, False) in HALF-OPEN: re-opens (stamp _opened_at = now)
now is always a float parameter โ no time functions inside.
Difficulty: advanced
Press Submit to check your solution.
Back to lesson: Practice: Trip the Breaker