Support power-of-<arbitrary number>

This commit is contained in:
Frank Denis 2020-03-20 17:48:54 +01:00
parent b57cc19d70
commit 34d83f027f
2 changed files with 16 additions and 2 deletions

View File

@ -359,7 +359,7 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
dlog.Debug("No local IP/port configured")
}
lbStrategy := LBStrategy(DefaultLBStrategy)
switch strings.ToLower(config.LBStrategy) {
switch lbStrategyStr := strings.ToLower(config.LBStrategy); lbStrategyStr {
case "":
// default
case "p2":
@ -372,7 +372,15 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
case "random":
lbStrategy = LBStrategyRandom{}
default:
dlog.Warnf("Unknown load balancing strategy: [%s]", config.LBStrategy)
if strings.HasPrefix(lbStrategyStr, "p") {
n, err := strconv.ParseInt(strings.TrimPrefix(lbStrategyStr, "p"), 10, 32)
if err != nil || n <= 0 {
dlog.Fatalf("Invalid load balancing strategy: [%s]", config.LBStrategy)
}
lbStrategy = LBStrategyPN{n: int(n)}
} else {
dlog.Warnf("Unknown load balancing strategy: [%s]", config.LBStrategy)
}
}
proxy.serversInfo.lbStrategy = lbStrategy
proxy.serversInfo.lbEstimator = config.LBEstimator

View File

@ -72,6 +72,12 @@ func (LBStrategyP2) getCandidate(serversCount int) int {
return rand.Intn(Min(serversCount, 2))
}
type LBStrategyPN struct{ n int }
func (s LBStrategyPN) getCandidate(serversCount int) int {
return rand.Intn(Min(serversCount, s.n))
}
type LBStrategyPH struct{}
func (LBStrategyPH) getCandidate(serversCount int) int {