recordSuccess records a success and potentially closes the circuit
()
| 161 | |
| 162 | // recordSuccess records a success and potentially closes the circuit |
| 163 | func (cb *CircuitBreaker) recordSuccess() { |
| 164 | cb.lastSuccessTime.Store(time.Now().Unix()) |
| 165 | |
| 166 | state := CircuitBreakerState(cb.state.Load()) |
| 167 | |
| 168 | switch state { |
| 169 | case CircuitBreakerClosed: |
| 170 | // Reset failure count on success in closed state |
| 171 | cb.failures.Store(0) |
| 172 | case CircuitBreakerHalfOpen: |
| 173 | successes := cb.successes.Add(1) |
| 174 | |
| 175 | // If we've had enough successful requests, close the circuit |
| 176 | if successes >= int64(cb.maxRequests) { |
| 177 | if cb.state.CompareAndSwap(int32(CircuitBreakerHalfOpen), int32(CircuitBreakerClosed)) { |
| 178 | cb.failures.Store(0) |
| 179 | if internal.LogLevel.InfoOrAbove() { |
| 180 | internal.Logger.Printf(context.Background(), logs.CircuitBreakerClosed(cb.endpoint, successes)) |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // GetState returns the current state of the circuit breaker |
| 188 | func (cb *CircuitBreaker) GetState() CircuitBreakerState { |
no test coverage detected