From fb99e8b6ea33048decaee5dccf2d980ad1524a58 Mon Sep 17 00:00:00 2001 From: Your Name <1185430411@qq.com> Date: Mon, 2 Nov 2020 22:09:23 +0800 Subject: [PATCH 1/6] fix bug in circuit_breaker.go about SlowRequestRatio --- core/circuitbreaker/circuit_breaker.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/circuitbreaker/circuit_breaker.go b/core/circuitbreaker/circuit_breaker.go index 70c019a56..4f41ed68d 100644 --- a/core/circuitbreaker/circuit_breaker.go +++ b/core/circuitbreaker/circuit_breaker.go @@ -290,7 +290,8 @@ func (b *slowRtCircuitBreaker) OnRequestComplete(rt uint64, err error) { return } - if slowRatio > b.maxSlowRequestRatio { + // Special case where maxSlowRequestRatio is set to 1.0 should be handled + if slowRatio > b.maxSlowRequestRatio || (slowRatio == 1.0 && b.maxSlowRequestRatio == 1.0) { curStatus = b.CurrentState() switch curStatus { case Closed: From a282badf2687a76902c5ca87ee1ebde0c19cca27 Mon Sep 17 00:00:00 2001 From: Your Name <1185430411@qq.com> Date: Tue, 3 Nov 2020 13:07:31 +0800 Subject: [PATCH 2/6] fix errorRatioThreshold --- core/circuitbreaker/circuit_breaker.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/circuitbreaker/circuit_breaker.go b/core/circuitbreaker/circuit_breaker.go index 4f41ed68d..7f09a1a82 100644 --- a/core/circuitbreaker/circuit_breaker.go +++ b/core/circuitbreaker/circuit_breaker.go @@ -290,7 +290,7 @@ func (b *slowRtCircuitBreaker) OnRequestComplete(rt uint64, err error) { return } - // Special case where maxSlowRequestRatio is set to 1.0 should be handled + // Special case when maxSlowRequestRatio is set to 1.0 should be handled if slowRatio > b.maxSlowRequestRatio || (slowRatio == 1.0 && b.maxSlowRequestRatio == 1.0) { curStatus = b.CurrentState() switch curStatus { @@ -469,7 +469,8 @@ func (b *errorRatioCircuitBreaker) OnRequestComplete(rt uint64, err error) { if totalCount < b.minRequestAmount { return } - if errorRatio > b.errorRatioThreshold { + // Special case when errorRatioThreshold is set to 1.0 should be handled + if errorRatio > b.errorRatioThreshold || (b.errorRatioThreshold == 1.0 && errorRatio == 1.0) { curStatus = b.CurrentState() switch curStatus { case Closed: From fd26c50dac17f1c4d8c4ee7f6928b8fed51f3a07 Mon Sep 17 00:00:00 2001 From: Your Name <1185430411@qq.com> Date: Wed, 4 Nov 2020 23:27:32 +0800 Subject: [PATCH 3/6] use gte --- core/circuitbreaker/circuit_breaker.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/circuitbreaker/circuit_breaker.go b/core/circuitbreaker/circuit_breaker.go index 7f09a1a82..82dca5a93 100644 --- a/core/circuitbreaker/circuit_breaker.go +++ b/core/circuitbreaker/circuit_breaker.go @@ -290,8 +290,7 @@ func (b *slowRtCircuitBreaker) OnRequestComplete(rt uint64, err error) { return } - // Special case when maxSlowRequestRatio is set to 1.0 should be handled - if slowRatio > b.maxSlowRequestRatio || (slowRatio == 1.0 && b.maxSlowRequestRatio == 1.0) { + if slowRatio >= b.maxSlowRequestRatio { curStatus = b.CurrentState() switch curStatus { case Closed: @@ -469,8 +468,7 @@ func (b *errorRatioCircuitBreaker) OnRequestComplete(rt uint64, err error) { if totalCount < b.minRequestAmount { return } - // Special case when errorRatioThreshold is set to 1.0 should be handled - if errorRatio > b.errorRatioThreshold || (b.errorRatioThreshold == 1.0 && errorRatio == 1.0) { + if errorRatio >= b.errorRatioThreshold { curStatus = b.CurrentState() switch curStatus { case Closed: From dfb33e7a3b6fee7ad6530f03a80c5d8432c109f3 Mon Sep 17 00:00:00 2001 From: Your Name <1185430411@qq.com> Date: Wed, 4 Nov 2020 23:35:06 +0800 Subject: [PATCH 4/6] improve logic check --- core/circuitbreaker/circuit_breaker.go | 4 ++-- core/circuitbreaker/rule_manager.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/circuitbreaker/circuit_breaker.go b/core/circuitbreaker/circuit_breaker.go index 82dca5a93..70c019a56 100644 --- a/core/circuitbreaker/circuit_breaker.go +++ b/core/circuitbreaker/circuit_breaker.go @@ -290,7 +290,7 @@ func (b *slowRtCircuitBreaker) OnRequestComplete(rt uint64, err error) { return } - if slowRatio >= b.maxSlowRequestRatio { + if slowRatio > b.maxSlowRequestRatio { curStatus = b.CurrentState() switch curStatus { case Closed: @@ -468,7 +468,7 @@ func (b *errorRatioCircuitBreaker) OnRequestComplete(rt uint64, err error) { if totalCount < b.minRequestAmount { return } - if errorRatio >= b.errorRatioThreshold { + if errorRatio > b.errorRatioThreshold { curStatus = b.CurrentState() switch curStatus { case Closed: diff --git a/core/circuitbreaker/rule_manager.go b/core/circuitbreaker/rule_manager.go index 348341483..d2f94561a 100644 --- a/core/circuitbreaker/rule_manager.go +++ b/core/circuitbreaker/rule_manager.go @@ -357,11 +357,11 @@ func IsValid(r *Rule) error { if r.Threshold < 0.0 { return errors.New("invalid Threshold") } - if r.Strategy == SlowRequestRatio && r.Threshold > 1.0 { - return errors.New("invalid slow request ratio threshold (valid range: [0.0, 1.0])") + if r.Strategy == SlowRequestRatio && r.Threshold >= 1.0 { + return errors.New("invalid slow request ratio threshold (valid range: [0.0, 1.0) )") } - if r.Strategy == ErrorRatio && r.Threshold > 1.0 { - return errors.New("invalid error ratio threshold (valid range: [0.0, 1.0])") + if r.Strategy == ErrorRatio && r.Threshold >= 1.0 { + return errors.New("invalid error ratio threshold (valid range: [0.0, 1.0) )") } return nil } From 7c4ecf3d186c9ad6fde22158aed7f08b78851820 Mon Sep 17 00:00:00 2001 From: Your Name <1185430411@qq.com> Date: Thu, 5 Nov 2020 00:08:48 +0800 Subject: [PATCH 5/6] improve logic check --- core/circuitbreaker/circuit_breaker.go | 4 ++-- core/circuitbreaker/rule_manager.go | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/core/circuitbreaker/circuit_breaker.go b/core/circuitbreaker/circuit_breaker.go index 70c019a56..7533a2aac 100644 --- a/core/circuitbreaker/circuit_breaker.go +++ b/core/circuitbreaker/circuit_breaker.go @@ -468,7 +468,7 @@ func (b *errorRatioCircuitBreaker) OnRequestComplete(rt uint64, err error) { if totalCount < b.minRequestAmount { return } - if errorRatio > b.errorRatioThreshold { + if errorRatio >= b.errorRatioThreshold { curStatus = b.CurrentState() switch curStatus { case Closed: @@ -642,7 +642,7 @@ func (b *errorCountCircuitBreaker) OnRequestComplete(rt uint64, err error) { if totalCount < b.minRequestAmount { return } - if errorCount > b.errorCountThreshold { + if errorCount >= b.errorCountThreshold { curStatus = b.CurrentState() switch curStatus { case Closed: diff --git a/core/circuitbreaker/rule_manager.go b/core/circuitbreaker/rule_manager.go index d2f94561a..348341483 100644 --- a/core/circuitbreaker/rule_manager.go +++ b/core/circuitbreaker/rule_manager.go @@ -357,11 +357,11 @@ func IsValid(r *Rule) error { if r.Threshold < 0.0 { return errors.New("invalid Threshold") } - if r.Strategy == SlowRequestRatio && r.Threshold >= 1.0 { - return errors.New("invalid slow request ratio threshold (valid range: [0.0, 1.0) )") + if r.Strategy == SlowRequestRatio && r.Threshold > 1.0 { + return errors.New("invalid slow request ratio threshold (valid range: [0.0, 1.0])") } - if r.Strategy == ErrorRatio && r.Threshold >= 1.0 { - return errors.New("invalid error ratio threshold (valid range: [0.0, 1.0) )") + if r.Strategy == ErrorRatio && r.Threshold > 1.0 { + return errors.New("invalid error ratio threshold (valid range: [0.0, 1.0])") } return nil } From b4c57d41fab60f7c91e539508f7698f139bcb477 Mon Sep 17 00:00:00 2001 From: louyuting <1849491904@qq.com> Date: Thu, 5 Nov 2020 13:00:15 +0800 Subject: [PATCH 6/6] refine code --- core/circuitbreaker/circuit_breaker.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/circuitbreaker/circuit_breaker.go b/core/circuitbreaker/circuit_breaker.go index 7533a2aac..26555e7a3 100644 --- a/core/circuitbreaker/circuit_breaker.go +++ b/core/circuitbreaker/circuit_breaker.go @@ -290,7 +290,7 @@ func (b *slowRtCircuitBreaker) OnRequestComplete(rt uint64, err error) { return } - if slowRatio > b.maxSlowRequestRatio { + if slowRatio > b.maxSlowRequestRatio || util.Float64Equals(slowRatio, b.maxSlowRequestRatio) { curStatus = b.CurrentState() switch curStatus { case Closed: @@ -468,7 +468,7 @@ func (b *errorRatioCircuitBreaker) OnRequestComplete(rt uint64, err error) { if totalCount < b.minRequestAmount { return } - if errorRatio >= b.errorRatioThreshold { + if errorRatio > b.errorRatioThreshold || util.Float64Equals(errorRatio, b.errorRatioThreshold) { curStatus = b.CurrentState() switch curStatus { case Closed: