Revert "Implement pN load balancing strategy (#1188)"

This reverts commit 014a75c0ec.
This commit is contained in:
Frank Denis 2020-03-20 17:55:33 +01:00
parent 014a75c0ec
commit 4c402a6012
3 changed files with 16 additions and 17 deletions

View File

@ -363,22 +363,17 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
switch strings.ToLower(config.LBStrategy) {
case "":
// default
case "p2":
lbStrategy = LBStrategyP2
case "ph":
lbStrategy = LBStrategyPH
case "fastest":
case "first":
lbStrategy = LBStrategy(1)
lbStrategy = LBStrategyFirst
case "random":
lbStrategy = LBStrategyRandom
default:
n, err := strconv.ParseUint(strings.TrimPrefix(config.LBStrategy, "p"), 10, 64)
if err == nil && n >= 1 {
lbStrategy = LBStrategy(n)
} else {
dlog.Warnf("Unknown load balancing strategy: [%s]", config.LBStrategy)
}
dlog.Warnf("Unknown load balancing strategy: [%s]", config.LBStrategy)
}
proxy.serversInfo.lbStrategy = lbStrategy
proxy.serversInfo.lbEstimator = config.LBEstimator

View File

@ -126,7 +126,6 @@ keepalive = 30
## Load-balancing strategy: 'p2' (default), 'ph', 'first' or 'random'
## Use pN where N is a positive number to choose between N first fastest servers.
# lb_strategy = 'p2'

View File

@ -65,11 +65,14 @@ type ServerInfo struct {
type LBStrategy int
const (
LBStrategyRandom = LBStrategy(0)
LBStrategyPH = LBStrategy(-1)
LBStrategyNone = LBStrategy(iota)
LBStrategyP2
LBStrategyPH
LBStrategyFirst
LBStrategyRandom
)
const DefaultLBStrategy = LBStrategy(2)
const DefaultLBStrategy = LBStrategyP2
type ServersInfo struct {
sync.RWMutex
@ -208,12 +211,14 @@ func (serversInfo *ServersInfo) getOne() *ServerInfo {
}
var candidate int
switch serversInfo.lbStrategy {
case -1: // First half
case LBStrategyFirst:
candidate = 0
case LBStrategyPH:
candidate = rand.Intn(Max(Min(serversCount, 2), serversCount/2))
case 0: // Random
case LBStrategyRandom:
candidate = rand.Intn(serversCount)
default: // First N
candidate = rand.Intn(int(serversInfo.lbStrategy)) % serversCount
default:
candidate = rand.Intn(Min(serversCount, 2))
}
serverInfo := serversInfo.inner[candidate]
dlog.Debugf("Using candidate [%s] RTT: %d", (*serverInfo).Name, int((*serverInfo).rtt.Value()))