Skip to content

Commit

Permalink
Properly log the state of the circuits breakers.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiaraujo committed Nov 18, 2017
1 parent 258967e commit d72258b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 26 deletions.
17 changes: 4 additions & 13 deletions circuit/consecutivebreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

type consecutiveBreaker struct {
settings BreakerSettings
open bool
gb *gobreaker.TwoStepCircuitBreaker
}

Expand All @@ -21,18 +20,16 @@ func newConsecutive(s BreakerSettings) *consecutiveBreaker {
MaxRequests: uint32(s.HalfOpenRequests),
Timeout: s.Timeout,
ReadyToTrip: b.readyToTrip,
OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
log.Infof("circuit breaker %v went from %v to %v", name, from.String(), to.String())
},
})

return b
}

func (b *consecutiveBreaker) readyToTrip(c gobreaker.Counts) bool {
b.open = int(c.ConsecutiveFailures) >= b.settings.Failures
if b.open {
log.Infof("circuit breaker open: %v", b.settings)
}

return b.open
return int(c.ConsecutiveFailures) >= b.settings.Failures
}

func (b *consecutiveBreaker) Allow() (func(bool), bool) {
Expand All @@ -44,11 +41,5 @@ func (b *consecutiveBreaker) Allow() (func(bool), bool) {
if !closed {
return nil, false
}

if b.open {
b.open = false
log.Infof("circuit breaker closed: %v", b.settings)
}

return done, true
}
17 changes: 4 additions & 13 deletions circuit/ratebreaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

type rateBreaker struct {
settings BreakerSettings
open bool
mx *sync.Mutex
sampler *binarySampler
gb *gobreaker.TwoStepCircuitBreaker
Expand All @@ -31,6 +30,9 @@ func newRate(s BreakerSettings) *rateBreaker {
MaxRequests: uint32(s.HalfOpenRequests),
Timeout: s.Timeout,
ReadyToTrip: func(gobreaker.Counts) bool { return b.readyToTrip() },
OnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {
log.Infof("circuit breaker %v went from %v to %v", name, from.String(), to.String())
},
})

return b
Expand All @@ -44,13 +46,7 @@ func (b *rateBreaker) readyToTrip() bool {
return false
}

b.open = b.sampler.count >= b.settings.Failures
if b.open {
log.Infof("circuit breaker open: %v", b.settings)
b.sampler = nil
}

return b.open
return b.sampler.count >= b.settings.Failures
}

// count the failures in closed and half-open state
Expand All @@ -75,11 +71,6 @@ func (b *rateBreaker) Allow() (func(bool), bool) {
return nil, false
}

if b.open {
b.open = false
log.Infof("circuit breaker closed: %v", b.settings)
}

return func(success bool) {
b.countRate(success)
done(success)
Expand Down

0 comments on commit d72258b

Please sign in to comment.