recordFailure records a failure and potentially opens the circuit
()
| 135 | |
| 136 | // recordFailure records a failure and potentially opens the circuit |
| 137 | func (cb *CircuitBreaker) recordFailure() { |
| 138 | cb.lastFailureTime.Store(time.Now().Unix()) |
| 139 | failures := cb.failures.Add(1) |
| 140 | |
| 141 | state := CircuitBreakerState(cb.state.Load()) |
| 142 | |
| 143 | switch state { |
| 144 | case CircuitBreakerClosed: |
| 145 | if failures >= int64(cb.failureThreshold) { |
| 146 | if cb.state.CompareAndSwap(int32(CircuitBreakerClosed), int32(CircuitBreakerOpen)) { |
| 147 | if internal.LogLevel.WarnOrAbove() { |
| 148 | internal.Logger.Printf(context.Background(), logs.CircuitBreakerOpened(cb.endpoint, failures)) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | case CircuitBreakerHalfOpen: |
| 153 | // Any failure in half-open state immediately opens the circuit |
| 154 | if cb.state.CompareAndSwap(int32(CircuitBreakerHalfOpen), int32(CircuitBreakerOpen)) { |
| 155 | if internal.LogLevel.WarnOrAbove() { |
| 156 | internal.Logger.Printf(context.Background(), logs.CircuitBreakerReopened(cb.endpoint)) |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // recordSuccess records a success and potentially closes the circuit |
| 163 | func (cb *CircuitBreaker) recordSuccess() { |
no test coverage detected