mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 18:00:00 +08:00
optimize: simplify breaker algorithm (#4151)
This commit is contained in:
@@ -11,15 +11,12 @@ import (
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// 250ms for bucket duration
|
// 250ms for bucket duration
|
||||||
window = time.Second * 10
|
window = time.Second * 10
|
||||||
buckets = 40
|
buckets = 40
|
||||||
maxFailBucketsToDecreaseK = 30
|
forcePassDuration = time.Second
|
||||||
minBucketsToSpeedUp = 3
|
k = 1.5
|
||||||
forcePassDuration = time.Second
|
minK = 1.1
|
||||||
k = 1.5
|
protection = 5
|
||||||
minK = 1.1
|
|
||||||
recoveryK = 4 - k
|
|
||||||
protection = 5
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// googleBreaker is a netflixBreaker pattern from google.
|
// googleBreaker is a netflixBreaker pattern from google.
|
||||||
@@ -56,13 +53,8 @@ func newGoogleBreaker() *googleBreaker {
|
|||||||
func (b *googleBreaker) accept() error {
|
func (b *googleBreaker) accept() error {
|
||||||
var w float64
|
var w float64
|
||||||
history := b.history()
|
history := b.history()
|
||||||
if history.failingBuckets >= minBucketsToSpeedUp {
|
w = b.k - (b.k-minK)*float64(history.failingBuckets)/buckets
|
||||||
w = b.k - float64(history.failingBuckets-1)*(b.k-minK)/maxFailBucketsToDecreaseK
|
weightedAccepts := mathx.AtLeast(w, minK) * float64(history.accepts)
|
||||||
w = mathx.AtLeast(w, minK)
|
|
||||||
} else {
|
|
||||||
w = b.k
|
|
||||||
}
|
|
||||||
weightedAccepts := w * float64(history.accepts)
|
|
||||||
// https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101
|
// https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101
|
||||||
// for better performance, no need to care about the negative ratio
|
// for better performance, no need to care about the negative ratio
|
||||||
dropRatio := (float64(history.total-protection) - weightedAccepts) / float64(history.total+1)
|
dropRatio := (float64(history.total-protection) - weightedAccepts) / float64(history.total+1)
|
||||||
@@ -76,11 +68,7 @@ func (b *googleBreaker) accept() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we have more than 2 working buckets, we are in recovery mode,
|
dropRatio *= float64(buckets-history.workingBuckets) / buckets
|
||||||
// the latest bucket is the current one, so we ignore it.
|
|
||||||
if history.workingBuckets >= minBucketsToSpeedUp {
|
|
||||||
dropRatio /= recoveryK
|
|
||||||
}
|
|
||||||
|
|
||||||
if b.proba.TrueOnProba(dropRatio) {
|
if b.proba.TrueOnProba(dropRatio) {
|
||||||
return ErrServiceUnavailable
|
return ErrServiceUnavailable
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ func TestGoogleBreakerMoreFallingBuckets(t *testing.T) {
|
|||||||
b := getGoogleBreaker()
|
b := getGoogleBreaker()
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
stopChan := time.After(testInterval * minBucketsToSpeedUp * 2)
|
stopChan := time.After(testInterval * 6)
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Millisecond)
|
time.Sleep(time.Millisecond)
|
||||||
select {
|
select {
|
||||||
|
|||||||
Reference in New Issue
Block a user