Skip to content

fix(api): let only one request probe a half-open circuit - #414

Open
Ivancheng7 wants to merge 1 commit into
xvirobotics:mainfrom
Ivancheng7:fix/circuit-breaker-single-half-open-probe
Open

Ivancheng7 wants to merge 1 commit into
xvirobotics:mainfrom
Ivancheng7:fix/circuit-breaker-single-half-open-probe

Conversation

@Ivancheng7

Copy link
Copy Markdown
Contributor

Problem

Once a circuit turns half-open, isAvailable() returns true for every request — the "probe" is not a probe. Under load all requests that arrive while the probe is in flight are admitted to a bot that has not recovered yet, and the burst also drives halfOpenSuccesses to halfOpenMaxAttempts, closing the circuit without any genuine recovery having been verified.

Fix

Track halfOpenProbeInFlight on the circuit:

  • the request that transitions open → half-open takes the probe slot;
  • further requests are rejected until the probe settles — the slot is released in recordSuccess() / recordFailure(), and a failed probe re-opens the circuit exactly as before.

Tests

New tests/circuit-breaker.test.ts:

  • a burst behind the first probe is rejected; the probe sequence then closes the circuit;
  • a failed probe re-opens the circuit and the next timeout window gates again.

Full local suite: no new failures.

isAvailable() returned true for every request while the circuit was
half-open, so a traffic burst was admitted in full to a bot that had not
actually recovered — and the burst finished halfOpenMaxAttempts quickly,
closing the circuit without a verified recovery.

- Track halfOpenProbeInFlight: the request that transitions open -> half-open
  takes the probe slot; further requests are rejected until it settles.
- recordSuccess()/recordFailure() release the slot; a failed probe re-opens
  the circuit as before.
- New tests/circuit-breaker.test.ts: one probe per window with a burst behind
  it, successful probe sequence closes, failed probe re-opens and the next
  window gates again.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant