Skip to content

Commit

Permalink
add ignores counter for internal state error
Browse files Browse the repository at this point in the history
Signed-off-by: hlts2 <hiroto.funakoshi.hiroto@gmail.com>
  • Loading branch information
hlts2 committed Oct 19, 2022
1 parent ba3136a commit 13429d2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 2 additions & 1 deletion internal/circuitbreaker/breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func newBreaker(key string, opts ...BreakerOption) (*breaker, error) {
// If the current breaker state is "Open", this function returns ErrCircuitBreakerOpenState.
func (b *breaker) do(ctx context.Context, fn func(ctx context.Context) (val interface{}, err error)) (val interface{}, st State, err error) {
if st, err := b.isReady(); err != nil {
b.count.Load().(*count).onIgnore()
return nil, st, err
}
val, err = fn(ctx)
Expand Down Expand Up @@ -121,7 +122,7 @@ func (b *breaker) success() {
// halfOpenErrShouldTrip.ShouldTrip returns true when the sum of the number of successes and failures is greater than the b.minSamples and when the error rate is greater than the b.halfOpenErrRate.
// In other words, if the error rate is less than the b.halfOpenErrRate, it can be judged that the success rate is high, so this function change to the "Close" state from "Half-Open".
if st := b.currentState(); st == StateHalfOpen &&
cnt.Total() >= b.minSamples &&
cnt.Successes()+cnt.Fails() >= b.minSamples &&
!b.halfOpenErrShouldTrip.ShouldTrip(cnt) {
log.Infof("the operation succeeded, circuit breaker state for '%s' changed,\tfrom: %s, to: %s", b.key, st.String(), StateClosed.String())
b.reset()
Expand Down
6 changes: 6 additions & 0 deletions internal/circuitbreaker/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Counter interface {
}

type count struct {
ignores int64
successes int64
failures int64
}
Expand All @@ -46,9 +47,14 @@ func (c *count) onFail() {
atomic.AddInt64(&c.failures, 1)
}

func (c *count) onIgnore() {
atomic.AddInt64(&c.ignores, 1)
}

func (c *count) reset() {
atomic.StoreInt64(&c.failures, 0)
atomic.StoreInt64(&c.successes, 0)
atomic.StoreInt64(&c.ignores, 0)
}

var _ Counter = (*count)(nil)

0 comments on commit 13429d2

Please sign in to comment.