Skip to content

Commit

Permalink
Loading circuitbreaker rule supports cache
Browse files Browse the repository at this point in the history
  • Loading branch information
binbin0325 committed Nov 15, 2020
1 parent 14fe759 commit 162dc70
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 11 additions & 0 deletions core/circuitbreaker/rule_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
breakerRules = make(map[string][]*Rule)
breakers = make(map[string][]CircuitBreaker)
updateMux = &sync.RWMutex{}
currentRules = make([]*Rule, 0)

stateChangeListeners = make([]StateChangeListener, 0)
)
Expand Down Expand Up @@ -117,6 +118,14 @@ func ClearRules() error {
// error: was designed to indicate whether occurs the error.
func LoadRules(rules []*Rule) (bool, error) {
// TODO in order to avoid invalid update, should check consistent with last update rules
updateMux.RLock()
isEqual := reflect.DeepEqual(currentRules, rules)
updateMux.RUnlock()
if isEqual {
logging.Info("[CircuitBreaker] Load rules repetition, does not load")
return false, nil
}

err := onRuleUpdate(rules)
return true, err
}
Expand Down Expand Up @@ -271,6 +280,8 @@ func onRuleUpdate(rules []*Rule) (err error) {

breakerRules = newBreakerRules
breakers = newBreakers
currentRules = rules

return nil
}

Expand Down
30 changes: 30 additions & 0 deletions core/circuitbreaker/rule_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,3 +340,33 @@ func TestRemoveCircuitBreakerGenerator(t *testing.T) {
assert.Error(t, err, "not allowed to remove the generator for default circuit breaking strategies")
})
}

func TestLoadRules(t *testing.T) {
t.Run("loadSameRules", func(t *testing.T) {
_, err := LoadRules([]*Rule{
{
Resource: "abc",
Strategy: SlowRequestRatio,
RetryTimeoutMs: 1000,
MinRequestAmount: 5,
StatIntervalMs: 1000,
MaxAllowedRtMs: 20,
Threshold: 0.1,
},
})
assert.Nil(t, err)
ok, err := LoadRules([]*Rule{
{
Resource: "abc",
Strategy: SlowRequestRatio,
RetryTimeoutMs: 1000,
MinRequestAmount: 5,
StatIntervalMs: 1000,
MaxAllowedRtMs: 20,
Threshold: 0.1,
},
})
assert.Nil(t, err)
assert.False(t, ok)
})
}
4 changes: 2 additions & 2 deletions ext/datasource/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ func CircuitBreakerRulesUpdater(data interface{}) error {
fmt.Sprintf("Fail to type assert data to []*circuitbreaker.Rule, in fact, data: %+v", data),
)
}
succ, err := cb.LoadRules(rules)
if succ && err == nil {
_, err := cb.LoadRules(rules)
if err == nil {
return nil
}
return NewError(
Expand Down

0 comments on commit 162dc70

Please sign in to comment.