fix(circuit_breaker): clear rate keys on auto-reset#147
Open
levleontiev wants to merge 1 commit intomainfrom
Open
fix(circuit_breaker): clear rate keys on auto-reset#147levleontiev wants to merge 1 commit intomainfrom
levleontiev wants to merge 1 commit intomainfrom
Conversation
When auto_reset_after_minutes > 0 and the cooldown elapses, the circuit breaker deletes the state key but leaves the rate accumulation keys alive (TTL = WINDOW_TTL_SECONDS = 120 s). For any cooldown shorter than 120 s the sliding-window estimator reads the stale high-rate data and immediately re-trips the breaker on the very first post-reset request, making auto-reset a no-op. Fix: mirror what _M.reset() already does — delete current and previous rate keys immediately after clearing the state key. Also introduce SECONDS_PER_MINUTE (= 60) so the elapsed-minutes calculation is not semantically coupled to WINDOW_SIZE_SECONDS; the two constants are coincidentally equal but represent different concepts. Test coverage: AC-4 uses auto_reset_after_minutes=5 (300 s > 120 s WINDOW_TTL_SECONDS), so rate keys expire naturally and the bug never manifests. A future test with auto_reset_after_minutes=1 will cover the fixed path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The circuit breaker's auto-reset path had a bug: when the cooldown elapsed and the state key was deleted, the rate accumulation keys were not cleared. For
auto_reset_after_minutes < 2(shorter thanWINDOW_TTL_SECONDS = 120 s), the old high-rate data from the triggering spike was still alive in the shared dict. The sliding-window estimator would then read that stale data and immediately re-trip the breaker on the very first post-reset request — making auto-reset effectively a no-op.Root cause
Fix
Mirror what
_M.reset()already does correctly — delete current and previous rate keys after clearing the state key:Also introduces
SECONDS_PER_MINUTE = 60to decouple the elapsed-minutes calculation fromWINDOW_SIZE_SECONDS— the two constants are coincidentally equal but represent different concepts.Why tests didn't catch this
AC-4 uses
auto_reset_after_minutes = 5(300 s), well aboveWINDOW_TTL_SECONDS = 120 s, so rate keys expired naturally before auto-reset fired. The failure mode only manifests forauto_reset_after_minutes < 2.🤖 Generated with Claude Code