From d72258bcda13a69f51878dc38af80d33fac1b31c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rui=20Ara=C3=BAjo?= Date: Sat, 18 Nov 2017 19:28:49 +0100 Subject: [PATCH] Properly log the state of the circuits breakers. --- circuit/consecutivebreaker.go | 17 ++++------------- circuit/ratebreaker.go | 17 ++++------------- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/circuit/consecutivebreaker.go b/circuit/consecutivebreaker.go index 377265da33..5ebf46b5d0 100644 --- a/circuit/consecutivebreaker.go +++ b/circuit/consecutivebreaker.go @@ -7,7 +7,6 @@ import ( type consecutiveBreaker struct { settings BreakerSettings - open bool gb *gobreaker.TwoStepCircuitBreaker } @@ -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) { @@ -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 } diff --git a/circuit/ratebreaker.go b/circuit/ratebreaker.go index 321d399b69..378b7c7d5b 100644 --- a/circuit/ratebreaker.go +++ b/circuit/ratebreaker.go @@ -14,7 +14,6 @@ import ( type rateBreaker struct { settings BreakerSettings - open bool mx *sync.Mutex sampler *binarySampler gb *gobreaker.TwoStepCircuitBreaker @@ -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 @@ -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 @@ -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)