Practice · 1 of 1
CLOSED → OPEN → HALF-OPEN → CLOSED
CHALLENGE
Difficulty: advanced+25 XP
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.
Press Submit to check your solution.
Back to lesson: Practice: Trip the Breaker