Workspace/Coding labs
Loading progress

Advance a circuit breaker

Intermediate50 min

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

  1. Complete the starter function using the contract above.
  2. Use the examples and visible tests to check normal inputs, boundaries, and rejected inputs.
  3. Run tests to record your result, then compare with the explained reference solution.

Examples

EXAMPLE 1

InputSecond failure at time 1, threshold 2, cooldown 5

Outputopen until 6

New calls are held during cooldown.
EXAMPLE 2

InputRequest at 6, then another request before the probe outcome

OutputFirst admitted, second denied

Only one half-open probe is allowed.
solution.pyPython 3.12