Advance a circuit breaker
Implement breaker_step(state, event, now, threshold=2, cooldown=5). State has mode (closed/open/half_open), failures, until, and probe. The valid initial state is closed with zero failures, until 0, probe False. A request in closed is admitted. In open, only now >= until admits a request and moves to half_open with probe True. Further half_open requests are denied. A success outcome in closed or half_open resets the initial state. A failure increments failures; reaching threshold or failing a half-open probe opens until now+cooldown and clears probe. Reject outcomes while open. Return (new_state, admitted) for requests and (new_state, None) for outcomes without mutating input. Outcomes in closed are assumed to correspond to admitted requests; this lab does not track concurrent request IDs.
Your task
- Complete the starter function using the contract above.
- Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
- Run tests to record your result, then compare with the explained reference solution.
Examples
InputSecond failure at time 1, threshold 2, cooldown 5
Outputopen until 6
InputRequest at 6, then another request before the probe outcome
OutputFirst admitted, second denied
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor