-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package resty | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"sync/atomic" | ||
"time" | ||
) | ||
|
||
// CircuitBreaker can be in one of three states: Closed, Open, or Half-Open. | ||
// When the circuit breaker is Closed, requests are allowed to pass through. | ||
// If a failure count threshold is reached within a specified time-frame, the circuit breaker transitions to the Open state. | ||
// When the circuit breaker is Open, requests are blocked. | ||
// After a specified timeout, the circuit breaker transitions to the Half-Open state. | ||
// When the circuit breaker is Half-Open, a single request is allowed to pass through. | ||
// If that request fails, the circuit breaker returns to the Open state. | ||
// If that request succeeds, the circuit breaker returns to the Closed state. | ||
type CircuitBreaker struct { | ||
policies []BreakerPolicy | ||
timeout time.Duration | ||
failThreshold, successThreshold int | ||
|
||
state circuitBreakerState | ||
failCount, successCount int | ||
lastFail time.Time | ||
} | ||
|
||
// NewCircuitBreaker creates a new CircuitBreaker with default settings. | ||
// The default settings are: | ||
// - Timeout: 10 seconds | ||
// - FailThreshold: 3 | ||
// - SuccessThreshold: 1 | ||
// - Policies: Count5xxPolicy | ||
func NewCircuitBreaker() *CircuitBreaker { | ||
return &CircuitBreaker{ | ||
policies: []BreakerPolicy{Count5xxPolicy}, | ||
timeout: 10 * time.Second, | ||
failThreshold: 3, | ||
successThreshold: 1, | ||
} | ||
} | ||
|
||
// Policies sets the BreakerPolicy's that the CircuitBreaker will use to determine whether a response is a failure. | ||
func (cb *CircuitBreaker) Policies(policies []BreakerPolicy) *CircuitBreaker { | ||
cb.policies = policies | ||
return cb | ||
} | ||
|
||
// Timeout sets the timeout duration for the CircuitBreaker | ||
func (cb *CircuitBreaker) Timeout(timeout time.Duration) *CircuitBreaker { | ||
cb.timeout = timeout | ||
return cb | ||
} | ||
|
||
// FailThreshold sets the number of failures that must occur within the timeout duration for the CircuitBreaker to | ||
// transition to the Open state. | ||
func (cb *CircuitBreaker) FailThreshold(threshold int) *CircuitBreaker { | ||
cb.failThreshold = threshold | ||
return cb | ||
} | ||
|
||
// SuccessThreshold sets the number of successes that must occur to transition the CircuitBreaker from the Half-Open state | ||
// to the Closed state. | ||
func (cb *CircuitBreaker) SuccessThreshold(threshold int) *CircuitBreaker { | ||
cb.successThreshold = threshold | ||
return cb | ||
} | ||
|
||
// BreakerPolicy is a function that determines whether a response should trip the circuit breaker | ||
type BreakerPolicy func(resp *http.Response) bool | ||
|
||
// Count5xxPolicy is a BreakerPolicy that trips the circuit breaker if the response status code is 500 or greater | ||
func Count5xxPolicy(resp *http.Response) bool { | ||
return resp.StatusCode > 499 | ||
} | ||
|
||
var ErrBreakerOpen = errors.New("circuit breaker open") | ||
|
||
type circuitBreakerState uint32 | ||
|
||
const ( | ||
closed circuitBreakerState = iota | ||
open | ||
halfOpen | ||
) | ||
|
||
func (cb *CircuitBreaker) getState() circuitBreakerState { | ||
return circuitBreakerState(atomic.LoadUint32((*uint32)(&cb.state))) | ||
} | ||
|
||
func (cb *CircuitBreaker) allow() error { | ||
if cb == nil { | ||
return nil | ||
} | ||
|
||
if cb.getState() == open { | ||
return ErrBreakerOpen | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cb *CircuitBreaker) processResponse(resp *http.Response) { | ||
if cb == nil { | ||
return | ||
} | ||
|
||
failed := false | ||
for _, policy := range cb.policies { | ||
if policy(resp) { | ||
failed = true | ||
break | ||
} | ||
} | ||
|
||
if failed { | ||
if cb.failCount > 0 && time.Since(cb.lastFail) > cb.timeout { | ||
cb.failCount = 0 | ||
} | ||
|
||
switch cb.getState() { | ||
case closed: | ||
cb.failCount++ | ||
if cb.failCount == cb.failThreshold { | ||
cb.open() | ||
} else { | ||
cb.lastFail = time.Now() | ||
} | ||
case halfOpen: | ||
cb.open() | ||
} | ||
} else { | ||
switch cb.getState() { | ||
case closed: | ||
return | ||
case halfOpen: | ||
cb.successCount++ | ||
if cb.successCount == cb.successThreshold { | ||
cb.changeState(closed) | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func (cb *CircuitBreaker) open() { | ||
cb.changeState(open) | ||
go func() { | ||
time.Sleep(cb.timeout) | ||
cb.changeState(halfOpen) | ||
}() | ||
} | ||
|
||
func (cb *CircuitBreaker) changeState(state circuitBreakerState) { | ||
cb.failCount = 0 | ||
cb.successCount = 0 | ||
atomic.StoreUint32((*uint32)(&cb.state), uint32(state)) | ||
} |
This file contains 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
This file contains 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