Skip to content

Commit

Permalink
Change in Cache implementation for Loadbalance (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
thandayuthapani authored and tianxiaoliang committed Sep 7, 2018
1 parent 9adb1d4 commit 32140e4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion control/archaius/panel.go
Expand Up @@ -16,7 +16,7 @@ type Panel struct {
}

func newPanel(options control.Options) control.Panel {
SaveToLBCache(config.GetLoadBalancing())
SaveToLBCache(config.GetLoadBalancing(), "", true)
SaveToCBCache(config.GetHystrixConfig(), "", true)
return &Panel{}
}
Expand Down
6 changes: 5 additions & 1 deletion control/archaius/transfer.go
Expand Up @@ -14,12 +14,16 @@ import (
)

//SaveToLBCache save configs
func SaveToLBCache(raw *model.LoadBalancing) {
func SaveToLBCache(raw *model.LoadBalancing, key string, isAnyService bool) {
lager.Logger.Debug("Loading lb config from archaius into cache")
saveDefaultLB(raw)
for k, v := range raw.AnyService {
saveEachLB(k, v)
}
if !isAnyService {
stringSlice := strings.Split(key, ".")
saveEachLB(stringSlice[2], raw.AnyService[stringSlice[2]])
}

}
func saveDefaultLB(raw *model.LoadBalancing) {
Expand Down
4 changes: 2 additions & 2 deletions control/archaius/transfer_test.go
Expand Up @@ -27,13 +27,13 @@ func TestSaveToLBCache(t *testing.T) {
},
},
},
})
}, "", true)
c, _ := archaius.LBConfigCache.Get("test")
assert.Equal(t, loadbalancer.StrategyRoundRobin, c.(control.LoadBalancingConfig).Strategy)
}
func TestSaveDefaultToLBCache(t *testing.T) {
lager.Initialize("", "INFO", "", "size", true, 1, 10, 7)
archaius.SaveToLBCache(&model.LoadBalancing{})
archaius.SaveToLBCache(&model.LoadBalancing{}, "", true)
c, _ := archaius.LBConfigCache.Get("test")
assert.Equal(t, loadbalancer.StrategyRoundRobin, c.(control.LoadBalancingConfig).Strategy)
}
Expand Down
7 changes: 5 additions & 2 deletions eventlistener/loadbalance_event_listener.go
Expand Up @@ -6,12 +6,14 @@ import (
"github.com/go-chassis/go-archaius/core"
"github.com/go-chassis/go-chassis/control/archaius"
"github.com/go-chassis/go-chassis/core/config"
"regexp"
)

// constants for loadbalancer strategy name, and timeout
const (
//LoadBalanceKey is variable of type string that matches load balancing events
LoadBalanceKey = "^cse\\.loadbalance\\."
LoadBalanceKey = "^cse\\.loadbalance\\."
regex4normalloadbalance = "^cse\\.loadbalance\\.(strategy|SessionStickinessRule|retryEnabled|retryOnNext|retryOnSame|backoff)"
)

//LoadbalanceEventListener is a struct
Expand All @@ -25,5 +27,6 @@ func (e *LoadbalanceEventListener) Event(event *core.Event) {
if err := config.ReadLBFromArchaius(); err != nil {
lager.Logger.Error("can not unmarshal new lb config: " + err.Error())
}
archaius.SaveToLBCache(config.GetLoadBalancing())
regNormal := regexp.MustCompile(regex4normalloadbalance)
archaius.SaveToLBCache(config.GetLoadBalancing(), event.Key, regNormal.MatchString(event.Key))
}

0 comments on commit 32140e4

Please sign in to comment.