fix(circuit-breaker): preserve recovery timeout when OPEN (#469)#618
fix(circuit-breaker): preserve recovery timeout when OPEN (#469)#618devoliverluccas wants to merge 1 commit intoSynkraAI:mainfrom
Conversation
|
@devoliverluccas is attempting to deploy a commit to the Pedro Valério Lopez's projects Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe fix prevents the recovery timeout from being reset when failures are recorded while the circuit is already in the OPEN state, ensuring the circuit can transition to HALF_OPEN after the original timeout period regardless of subsequent failures. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
.aiox-core/core/ids/circuit-breaker.js (1)
95-119:⚠️ Potential issue | 🟡 MinorAdd explicit test coverage for
lastFailureTimebehavior in circuit-breaker state transitions.While tests exist for state transitions, they do not explicitly verify the critical behavior change referenced in the code comment (fix
#469):
- When circuit is already
OPENandrecordFailure()is called,lastFailureTimemust NOT be updated. Current tests check thatlastFailureTime > 0after entering OPEN state, but do not verify that subsequent failures while OPEN do not update this value.- When circuit is
HALF_OPENandrecordFailure()occurs,lastFailureTimemust be updated before the state changes to OPEN. This is not currently asserted in the "re-opens on any failure" test.Add assertions to verify both behaviors to ensure the recovery timeout mechanism works as intended.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.aiox-core/core/ids/circuit-breaker.js around lines 95 - 119, Add explicit assertions around recordFailure() to cover lastFailureTime behavior: in the test that exercises the STATE_OPEN path, capture the current _lastFailureTime, call circuit.recordFailure() again, and assert that _lastFailureTime did not change (remains equal to the captured value). In the test that exercises the STATE_HALF_OPEN -> STATE_OPEN transition, ensure you capture time before calling recordFailure(), call circuit.recordFailure(), and assert that _lastFailureTime was updated (is greater than the previous value) and that the state switched to STATE_OPEN; reference the recordFailure method and the _lastFailureTime, STATE_OPEN and STATE_HALF_OPEN symbols when adding these assertions.
🧹 Nitpick comments (1)
.aiox-core/core/ids/circuit-breaker.js (1)
164-164: Minor: Missing newline at end of file.The file ends without a trailing newline. Consider adding one for POSIX compliance and to avoid potential diff noise in future commits.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.aiox-core/core/ids/circuit-breaker.js at line 164, The file .aiox-core/core/ids/circuit-breaker.js is missing a trailing newline at EOF; open the file and add a single newline character at the end (after the final closing brace "};") so the file ends with a newline for POSIX compliance and to avoid diff noise in future commits.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In @.aiox-core/core/ids/circuit-breaker.js:
- Around line 95-119: Add explicit assertions around recordFailure() to cover
lastFailureTime behavior: in the test that exercises the STATE_OPEN path,
capture the current _lastFailureTime, call circuit.recordFailure() again, and
assert that _lastFailureTime did not change (remains equal to the captured
value). In the test that exercises the STATE_HALF_OPEN -> STATE_OPEN transition,
ensure you capture time before calling recordFailure(), call
circuit.recordFailure(), and assert that _lastFailureTime was updated (is
greater than the previous value) and that the state switched to STATE_OPEN;
reference the recordFailure method and the _lastFailureTime, STATE_OPEN and
STATE_HALF_OPEN symbols when adding these assertions.
---
Nitpick comments:
In @.aiox-core/core/ids/circuit-breaker.js:
- Line 164: The file .aiox-core/core/ids/circuit-breaker.js is missing a
trailing newline at EOF; open the file and add a single newline character at the
end (after the final closing brace "};") so the file ends with a newline for
POSIX compliance and to avoid diff noise in future commits.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ee227dfe-db1c-4338-95e4-96460fc4c734
📒 Files selected for processing (1)
.aiox-core/core/ids/circuit-breaker.js
Problem
recordFailure()was unconditionally updatingthis._lastFailureTime = Date.now()on every call, including when the circuit was already inOPENstate.This caused
isAllowed()to never findelapsed >= resetTimeoutMsas long as failures kept arriving — the circuit would stay stuck inOPENindefinitely and never transition toHALF_OPEN.Fix
Added an early return in
recordFailure()whenstate === OPEN. Failures are still counted (for stats), but the recovery timer is preserved so the circuit correctly transitions toHALF_OPENafter the configured timeout.Testing
recordFailure()called → timer NOT reset, circuit transitions to HALF_OPEN after timeout ✓Closes #469
Summary by CodeRabbit