breaker: remover useless code (#114)

master
刘青 4 years ago committed by GitHub
parent f90c0aa98e
commit e66cca3710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -13,11 +13,6 @@ import (
"github.com/tal-tech/go-zero/core/timex" "github.com/tal-tech/go-zero/core/timex"
) )
const (
StateClosed State = iota
StateOpen
)
const ( const (
numHistoryReasons = 5 numHistoryReasons = 5
timeFormat = "15:04:05" timeFormat = "15:04:05"
@ -27,7 +22,6 @@ const (
var ErrServiceUnavailable = errors.New("circuit breaker is open") var ErrServiceUnavailable = errors.New("circuit breaker is open")
type ( type (
State = int32
Acceptable func(err error) bool Acceptable func(err error) bool
Breaker interface { Breaker interface {

@ -2,7 +2,6 @@ package breaker
import ( import (
"math" "math"
"sync/atomic"
"time" "time"
"github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/core/collection"
@ -21,7 +20,6 @@ const (
// see Client-Side Throttling section in https://landing.google.com/sre/sre-book/chapters/handling-overload/ // see Client-Side Throttling section in https://landing.google.com/sre/sre-book/chapters/handling-overload/
type googleBreaker struct { type googleBreaker struct {
k float64 k float64
state int32
stat *collection.RollingWindow stat *collection.RollingWindow
proba *mathx.Proba proba *mathx.Proba
} }
@ -32,7 +30,6 @@ func newGoogleBreaker() *googleBreaker {
return &googleBreaker{ return &googleBreaker{
stat: st, stat: st,
k: k, k: k,
state: StateClosed,
proba: mathx.NewProba(), proba: mathx.NewProba(),
} }
} }
@ -43,15 +40,9 @@ func (b *googleBreaker) accept() error {
// https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101 // https://landing.google.com/sre/sre-book/chapters/handling-overload/#eq2101
dropRatio := math.Max(0, (float64(total-protection)-weightedAccepts)/float64(total+1)) dropRatio := math.Max(0, (float64(total-protection)-weightedAccepts)/float64(total+1))
if dropRatio <= 0 { if dropRatio <= 0 {
if atomic.LoadInt32(&b.state) == StateOpen {
atomic.CompareAndSwapInt32(&b.state, StateOpen, StateClosed)
}
return nil return nil
} }
if atomic.LoadInt32(&b.state) == StateClosed {
atomic.CompareAndSwapInt32(&b.state, StateClosed, StateOpen)
}
if b.proba.TrueOnProba(dropRatio) { if b.proba.TrueOnProba(dropRatio) {
return ErrServiceUnavailable return ErrServiceUnavailable
} }

@ -27,7 +27,6 @@ func getGoogleBreaker() *googleBreaker {
return &googleBreaker{ return &googleBreaker{
stat: st, stat: st,
k: 5, k: 5,
state: StateClosed,
proba: mathx.NewProba(), proba: mathx.NewProba(),
} }
} }

Loading…
Cancel
Save