mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-19 04:47:48 +00:00
Fix dead CircuitBreaker.ResetTimeout config (round 39)
error_recovery.go: ResetTimeout was copied into the CircuitBreaker but never consulted by the state machine (used only for a metric), so the documented "how long to wait before fully closing the circuit" (reset_timeout, default 30s) had no effect: Open->HalfOpen used Timeout (60s), and a single half-open success closed the circuit immediately, losing the intended open -> half-open -> (reset cooldown) -> closed hysteresis and letting a stray success immediately reopen (bounce). Now the circuit remains half-open after entering it until resetTimeout has elapsed; only then can a successful request fully close it. Half-open requests are still allowed as probes. Default behavior is more conservative (up to Timeout+ResetTimeout before full close). Tests: updated TestCircuitBreakerHalfOpenTransition to the corrected semantics; added TestCircuitBreakerResetTimeoutEnforced (fails on the old dead-field code, where resetTimeout was ignored). Verified: full source-order suite, full -race -count=1 -shuffle=on ./... (all packages), golangci-lint run ./... (0 issues).
This commit is contained in:
+15
-1
@@ -191,12 +191,16 @@ type CircuitBreaker struct {
|
||||
maxFailures int
|
||||
// timeout is how long to wait before allowing requests in half-open state
|
||||
timeout time.Duration
|
||||
// resetTimeout is how long to wait before transitioning from open to half-open
|
||||
// resetTimeout is how long to stay in half-open (after the open timeout)
|
||||
// before a successful response fully closes the circuit
|
||||
resetTimeout time.Duration
|
||||
// state tracks the current circuit breaker state
|
||||
state CircuitBreakerState
|
||||
// failures counts consecutive failures
|
||||
failures int64
|
||||
// halfOpenSince is when the circuit entered half-open; used to enforce
|
||||
// resetTimeout before a success can fully close it
|
||||
halfOpenSince time.Time
|
||||
}
|
||||
|
||||
// CircuitBreakerConfig holds configuration parameters for circuit breakers.
|
||||
@@ -275,6 +279,7 @@ func (cb *CircuitBreaker) allowRequest() bool {
|
||||
case CircuitBreakerOpen:
|
||||
if now.Sub(cb.lastFailureTime) > cb.timeout {
|
||||
cb.state = CircuitBreakerHalfOpen
|
||||
cb.halfOpenSince = now
|
||||
cb.logger.Infof("Circuit breaker transitioning to half-open state")
|
||||
return true
|
||||
}
|
||||
@@ -317,6 +322,15 @@ func (cb *CircuitBreaker) recordSuccess() {
|
||||
|
||||
switch cb.state {
|
||||
case CircuitBreakerHalfOpen:
|
||||
// Honor resetTimeout: do not fully close until the circuit has been
|
||||
// in half-open for at least resetTimeout. This provides the
|
||||
// documented open -> half-open -> (reset cooldown) -> closed
|
||||
// hysteresis instead of closing on the very first half-open
|
||||
// success, which would let a stray success immediately reopen
|
||||
// (bounce). While in half-open requests are still allowed as probes.
|
||||
if time.Since(cb.halfOpenSince) < cb.resetTimeout {
|
||||
return
|
||||
}
|
||||
cb.failures = 0
|
||||
cb.state = CircuitBreakerClosed
|
||||
cb.LogInfo("Circuit breaker closed after successful request in half-open state")
|
||||
|
||||
Reference in New Issue
Block a user