Skip to content

Commit

Permalink
Using circuitbreaker.Rule to define slowRequest/errorRatio/errorCount…
Browse files Browse the repository at this point in the history
… Rule
  • Loading branch information
louyuting committed Aug 17, 2020
1 parent d0cf004 commit 812eaf2
Show file tree
Hide file tree
Showing 17 changed files with 413 additions and 548 deletions.
59 changes: 29 additions & 30 deletions core/circuitbreaker/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,20 @@ import (
"github.com/alibaba/sentinel-golang/util"
)

/**
Circuit Breaker State Machine:
switch to open based on rule
+-----------------------------------------------------------------------+
| |
| v
+----------------+ +----------------+ Probe +----------------+
| | | |<----------------| |
| | Probe succeed | | | |
| Closed |<------------------| HalfOpen | | Open |
| | | | Probe failed | |
| | | +---------------->| |
+----------------+ +----------------+ +----------------+
*/
//
// Circuit Breaker State Machine:
//
// switch to open based on rule
// +-----------------------------------------------------------------------+
// | |
// | v
// +----------------+ +----------------+ Probe +----------------+
// | | | |<----------------| |
// | | Probe succeed | | | |
// | Closed |<------------------| HalfOpen | | Open |
// | | | | Probe failed | |
// | | | +---------------->| |
// +----------------+ +----------------+ +----------------+
type State int32

const (
Expand Down Expand Up @@ -80,7 +79,7 @@ type StateChangeListener interface {
// CircuitBreaker is the basic interface of circuit breaker
type CircuitBreaker interface {
// BoundRule returns the associated circuit breaking rule.
BoundRule() Rule
BoundRule() *Rule
// BoundStat returns the associated statistic data structure.
BoundStat() interface{}
// TryPass acquires permission of an invocation only if it is available at the time of invocation.
Expand All @@ -95,7 +94,7 @@ type CircuitBreaker interface {
//================================= circuitBreakerBase ====================================
// circuitBreakerBase encompasses the common fields of circuit breaker.
type circuitBreakerBase struct {
rule Rule
rule *Rule
// retryTimeoutMs represents recovery timeout (in milliseconds) before the circuit breaker opens.
// During the open period, no requests are permitted until the timeout has elapsed.
// After that, the circuit breaker will transform to half-open state for trying a few "trial" requests.
Expand All @@ -106,7 +105,7 @@ type circuitBreakerBase struct {
state *State
}

func (b *circuitBreakerBase) BoundRule() Rule {
func (b *circuitBreakerBase) BoundRule() *Rule {
return b.rule
}

Expand All @@ -128,7 +127,7 @@ func (b *circuitBreakerBase) fromClosedToOpen(snapshot interface{}) bool {
if b.state.casState(Closed, Open) {
b.updateNextRetryTimestamp()
for _, listener := range stateChangeListeners {
listener.OnTransformToOpen(Closed, b.rule, snapshot)
listener.OnTransformToOpen(Closed, *b.rule, snapshot)
}
return true
}
Expand All @@ -140,7 +139,7 @@ func (b *circuitBreakerBase) fromClosedToOpen(snapshot interface{}) bool {
func (b *circuitBreakerBase) fromOpenToHalfOpen() bool {
if b.state.casState(Open, HalfOpen) {
for _, listener := range stateChangeListeners {
listener.OnTransformToHalfOpen(Open, b.rule)
listener.OnTransformToHalfOpen(Open, *b.rule)
}
return true
}
Expand All @@ -153,7 +152,7 @@ func (b *circuitBreakerBase) fromHalfOpenToOpen(snapshot interface{}) bool {
if b.state.casState(HalfOpen, Open) {
b.updateNextRetryTimestamp()
for _, listener := range stateChangeListeners {
listener.OnTransformToOpen(HalfOpen, b.rule, snapshot)
listener.OnTransformToOpen(HalfOpen, *b.rule, snapshot)
}
return true
}
Expand All @@ -165,7 +164,7 @@ func (b *circuitBreakerBase) fromHalfOpenToOpen(snapshot interface{}) bool {
func (b *circuitBreakerBase) fromHalfOpenToClosed() bool {
if b.state.casState(HalfOpen, Closed) {
for _, listener := range stateChangeListeners {
listener.OnTransformToClosed(HalfOpen, b.rule)
listener.OnTransformToClosed(HalfOpen, *b.rule)
}
return true
}
Expand All @@ -181,7 +180,7 @@ type slowRtCircuitBreaker struct {
minRequestAmount uint64
}

func newSlowRtCircuitBreakerWithStat(r *slowRtRule, stat *slowRequestLeapArray) *slowRtCircuitBreaker {
func newSlowRtCircuitBreakerWithStat(r *Rule, stat *slowRequestLeapArray) *slowRtCircuitBreaker {
status := new(State)
status.set(Closed)
return &slowRtCircuitBreaker{
Expand All @@ -193,12 +192,12 @@ func newSlowRtCircuitBreakerWithStat(r *slowRtRule, stat *slowRequestLeapArray)
},
stat: stat,
maxAllowedRt: r.MaxAllowedRtMs,
maxSlowRequestRatio: r.MaxSlowRequestRatio,
maxSlowRequestRatio: r.Threshold,
minRequestAmount: r.MinRequestAmount,
}
}

func newSlowRtCircuitBreaker(r *slowRtRule) (*slowRtCircuitBreaker, error) {
func newSlowRtCircuitBreaker(r *Rule) (*slowRtCircuitBreaker, error) {
interval := r.StatIntervalMs
stat := &slowRequestLeapArray{}
leapArray, err := sbase.NewLeapArray(1, interval, stat)
Expand Down Expand Up @@ -367,7 +366,7 @@ type errorRatioCircuitBreaker struct {
stat *errorCounterLeapArray
}

func newErrorRatioCircuitBreakerWithStat(r *errorRatioRule, stat *errorCounterLeapArray) *errorRatioCircuitBreaker {
func newErrorRatioCircuitBreakerWithStat(r *Rule, stat *errorCounterLeapArray) *errorRatioCircuitBreaker {
status := new(State)
status.set(Closed)

Expand All @@ -384,7 +383,7 @@ func newErrorRatioCircuitBreakerWithStat(r *errorRatioRule, stat *errorCounterLe
}
}

func newErrorRatioCircuitBreaker(r *errorRatioRule) (*errorRatioCircuitBreaker, error) {
func newErrorRatioCircuitBreaker(r *Rule) (*errorRatioCircuitBreaker, error) {
interval := r.StatIntervalMs
stat := &errorCounterLeapArray{}
leapArray, err := sbase.NewLeapArray(1, interval, stat)
Expand Down Expand Up @@ -547,7 +546,7 @@ type errorCountCircuitBreaker struct {
stat *errorCounterLeapArray
}

func newErrorCountCircuitBreakerWithStat(r *errorCountRule, stat *errorCounterLeapArray) *errorCountCircuitBreaker {
func newErrorCountCircuitBreakerWithStat(r *Rule, stat *errorCounterLeapArray) *errorCountCircuitBreaker {
status := new(State)
status.set(Closed)

Expand All @@ -559,12 +558,12 @@ func newErrorCountCircuitBreakerWithStat(r *errorCountRule, stat *errorCounterLe
state: status,
},
minRequestAmount: r.MinRequestAmount,
errorCountThreshold: r.Threshold,
errorCountThreshold: uint64(r.Threshold),
stat: stat,
}
}

func newErrorCountCircuitBreaker(r *errorCountRule) (*errorCountCircuitBreaker, error) {
func newErrorCountCircuitBreaker(r *Rule) (*errorCountCircuitBreaker, error) {
interval := r.StatIntervalMs
stat := &errorCounterLeapArray{}
leapArray, err := sbase.NewLeapArray(1, interval, stat)
Expand Down
4 changes: 2 additions & 2 deletions core/circuitbreaker/circuit_breaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type CircuitBreakerMock struct {
mock.Mock
}

func (m *CircuitBreakerMock) BoundRule() Rule {
func (m *CircuitBreakerMock) BoundRule() *Rule {
args := m.Called()
return args.Get(0).(Rule)
return args.Get(0).(*Rule)
}

func (m *CircuitBreakerMock) BoundStat() interface{} {
Expand Down
Loading

0 comments on commit 812eaf2

Please sign in to comment.