Use an interface for load-balancing strategies

This commit is contained in:
Frank Denis 2020-03-20 17:37:34 +01:00
parent a1f3b34390
commit b57cc19d70
2 changed files with 33 additions and 26 deletions

View File

@ -358,20 +358,19 @@ func ConfigLoad(proxy *Proxy, flags *ConfigFlags) error {
if len(config.ListenAddresses) == 0 && len(config.LocalDoH.ListenAddresses) == 0 { if len(config.ListenAddresses) == 0 && len(config.LocalDoH.ListenAddresses) == 0 {
dlog.Debug("No local IP/port configured") dlog.Debug("No local IP/port configured")
} }
lbStrategy := LBStrategy(DefaultLBStrategy)
lbStrategy := DefaultLBStrategy
switch strings.ToLower(config.LBStrategy) { switch strings.ToLower(config.LBStrategy) {
case "": case "":
// default // default
case "p2": case "p2":
lbStrategy = LBStrategyP2 lbStrategy = LBStrategyP2{}
case "ph": case "ph":
lbStrategy = LBStrategyPH lbStrategy = LBStrategyPH{}
case "fastest": case "fastest":
case "first": case "first":
lbStrategy = LBStrategyFirst lbStrategy = LBStrategyFirst{}
case "random": case "random":
lbStrategy = LBStrategyRandom lbStrategy = LBStrategyRandom{}
default: default:
dlog.Warnf("Unknown load balancing strategy: [%s]", config.LBStrategy) dlog.Warnf("Unknown load balancing strategy: [%s]", config.LBStrategy)
} }

View File

@ -62,17 +62,35 @@ type ServerInfo struct {
DOHClientCreds DOHClientCreds DOHClientCreds DOHClientCreds
} }
type LBStrategy int type LBStrategy interface {
getCandidate(serversCount int) int
}
const ( type LBStrategyP2 struct{}
LBStrategyNone = LBStrategy(iota)
LBStrategyP2
LBStrategyPH
LBStrategyFirst
LBStrategyRandom
)
const DefaultLBStrategy = LBStrategyP2 func (LBStrategyP2) getCandidate(serversCount int) int {
return rand.Intn(Min(serversCount, 2))
}
type LBStrategyPH struct{}
func (LBStrategyPH) getCandidate(serversCount int) int {
return rand.Intn(Max(Min(serversCount, 2), serversCount/2))
}
type LBStrategyFirst struct{}
func (LBStrategyFirst) getCandidate(int) int {
return 0
}
type LBStrategyRandom struct{}
func (LBStrategyRandom) getCandidate(serversCount int) int {
return rand.Intn(serversCount)
}
var DefaultLBStrategy = LBStrategyP2{}
type ServersInfo struct { type ServersInfo struct {
sync.RWMutex sync.RWMutex
@ -209,17 +227,7 @@ func (serversInfo *ServersInfo) getOne() *ServerInfo {
if serversInfo.lbEstimator { if serversInfo.lbEstimator {
serversInfo.estimatorUpdate() serversInfo.estimatorUpdate()
} }
var candidate int candidate := serversInfo.lbStrategy.getCandidate(serversCount)
switch serversInfo.lbStrategy {
case LBStrategyFirst:
candidate = 0
case LBStrategyPH:
candidate = rand.Intn(Max(Min(serversCount, 2), serversCount/2))
case LBStrategyRandom:
candidate = rand.Intn(serversCount)
default:
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()))
serversInfo.Unlock() serversInfo.Unlock()