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,23 +363,18 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
switch strings.ToLower(config.LBStrategy) { switch strings.ToLower(config.LBStrategy) {
case "": case "":
// default // default
case "p2":
lbStrategy = LBStrategyP2
case "ph": case "ph":
lbStrategy = LBStrategyPH lbStrategy = LBStrategyPH
case "fastest": case "fastest":
case "first": case "first":
lbStrategy = LBStrategy(1) lbStrategy = LBStrategyFirst
case "random": case "random":
lbStrategy = LBStrategyRandom lbStrategy = LBStrategyRandom
default: 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.lbStrategy = lbStrategy
proxy.serversInfo.lbEstimator = config.LBEstimator proxy.serversInfo.lbEstimator = config.LBEstimator

View File

@ -126,7 +126,6 @@ keepalive = 30
## Load-balancing strategy: 'p2' (default), 'ph', 'first' or 'random' ## 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' # lb_strategy = 'p2'

View File

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