PythiloLesson 8 of 12 · progress kept in this browser

Separate breaker state from retry policy

A circuit breaker stops repeated work against a dependency after enough consecutive failures. Its states express permission to attempt a call: closed, open and a single half-open probe after a cooldown. A retry policy instead decides whether and when to repeat one logical operation.

The exercise models sequential attempts and injects time. Its half-open state permits only one probe; concurrent production use would require synchronization around admission. Outcomes are reported explicitly after each admitted attempt. A timeout must be treated as an uncertain side effect, not automatic evidence that repeating a write is safe.

failures=3
threshold=3
opened_at=10
cooldown=5
print(failures>=threshold)
print(14-opened_at>=cooldown)
Your task

Implement Breaker(threshold,cooldown): permit(now), success(), failure(now). Start closed; threshold consecutive failures opens. After cooldown allow one probe and deny others until outcome. Success closes and resets; failed probe reopens. Positive threshold and nonnegative cooldown required.