Skip to content

Commit

Permalink
Allow custom inspection of errors (#29)
Browse files Browse the repository at this point in the history
* support custom inspection of errors

* support custom inspection of errors

* renamed shouldtrip to issuccessful

* fixed IsSuccesful return values and comments

* fixed IsSuccesful return values and comments

* added test

Co-authored-by: dan.markhasin <[email protected]>
  • Loading branch information
dmarkhas and dmarkhas authored Jan 5, 2021
1 parent 3354bba commit 9c3b944
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
19 changes: 18 additions & 1 deletion gobreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,19 @@ func (c *Counts) clear() {
// Default ReadyToTrip returns true when the number of consecutive failures is more than 5.
//
// OnStateChange is called whenever the state of the CircuitBreaker changes.
//
// IsSuccessful is called with the error returned from the request, if not nil.
// If IsSuccessful returns false, the error is considered a failure, and is counted towards tripping the circuit breaker.
// If IsSuccessful returns true, the error will be returned to the caller without tripping the circuit breaker.
// If IsSuccessful is nil, default IsSuccessful is used, which returns false for all non-nil errors.
type Settings struct {
Name string
MaxRequests uint32
Interval time.Duration
Timeout time.Duration
ReadyToTrip func(counts Counts) bool
OnStateChange func(name string, from State, to State)
IsSuccessful func(err error) bool
}

// CircuitBreaker is a state machine to prevent sending requests that are likely to fail.
Expand All @@ -114,6 +120,7 @@ type CircuitBreaker struct {
interval time.Duration
timeout time.Duration
readyToTrip func(counts Counts) bool
isSuccessful func(err error) bool
onStateChange func(name string, from State, to State)

mutex sync.Mutex
Expand Down Expand Up @@ -161,6 +168,12 @@ func NewCircuitBreaker(st Settings) *CircuitBreaker {
cb.readyToTrip = st.ReadyToTrip
}

if st.IsSuccessful == nil {
cb.isSuccessful = defaultIsSuccessful
} else {
cb.isSuccessful = st.IsSuccessful
}

cb.toNewGeneration(time.Now())

return cb
Expand All @@ -180,6 +193,10 @@ func defaultReadyToTrip(counts Counts) bool {
return counts.ConsecutiveFailures > 5
}

func defaultIsSuccessful(err error) bool {
return err == nil
}

// Name returns the name of the CircuitBreaker.
func (cb *CircuitBreaker) Name() string {
return cb.name
Expand Down Expand Up @@ -215,7 +232,7 @@ func (cb *CircuitBreaker) Execute(req func() (interface{}, error)) (interface{},
}()

result, err := req()
cb.afterRequest(generation, err == nil)
cb.afterRequest(generation, cb.isSuccessful(err))
return result, err
}

Expand Down
24 changes: 24 additions & 0 deletions gobreaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,30 @@ func TestGeneration(t *testing.T) {
assert.Equal(t, Counts{0, 0, 0, 0, 0}, customCB.counts)
}

func TestCustomIsSuccessful(t *testing.T) {
isSuccessful := func(error) bool {
return true
}
cb := NewCircuitBreaker(Settings{IsSuccessful: isSuccessful})

for i := 0; i < 5; i++ {
assert.Nil(t, fail(cb))
}
assert.Equal(t, StateClosed, cb.State())
assert.Equal(t, Counts{5, 5, 0, 5, 0}, cb.counts)

cb.counts.clear()

cb.isSuccessful = func(err error) bool {
return err == nil
}
for i := 0; i < 6; i++ {
assert.Nil(t, fail(cb))
}
assert.Equal(t, StateOpen, cb.State())

}

func TestCircuitBreakerInParallel(t *testing.T) {
runtime.GOMAXPROCS(runtime.NumCPU())

Expand Down

0 comments on commit 9c3b944

Please sign in to comment.