glider/strategy/strategy.go

67 lines
1.5 KiB
Go
Raw Normal View History

2018-08-11 11:46:10 +08:00
package strategy
2017-07-13 21:55:41 +08:00
import (
"github.com/nadoo/glider/common/log"
"github.com/nadoo/glider/proxy"
)
2018-08-23 00:01:31 +08:00
// Checker is an interface of forwarder checker
type Checker interface {
Check()
}
2018-08-11 11:46:10 +08:00
// Config of strategy
type Config struct {
Strategy string
CheckWebSite string
CheckInterval int
MaxFailures int
IntFace string
}
2018-08-11 11:46:10 +08:00
// NewDialer returns a new strategy dialer
func NewDialer(s []string, c *Config) proxy.Dialer {
var fwdrs []*proxy.Forwarder
for _, chain := range s {
fwdr, err := proxy.ForwarderFromURL(chain, c.IntFace)
if err != nil {
log.Fatal(err)
}
2018-08-23 00:01:31 +08:00
fwdr.SetMaxFailures(uint32(c.MaxFailures))
fwdrs = append(fwdrs, fwdr)
}
if len(fwdrs) == 0 {
d, err := proxy.NewDirect(c.IntFace)
if err != nil {
log.Fatal(err)
}
return d
2018-01-13 20:08:49 +08:00
}
if len(fwdrs) == 1 {
return fwdrs[0]
2018-01-13 20:08:49 +08:00
}
var dialer proxy.Dialer
switch c.Strategy {
2018-01-13 20:08:49 +08:00
case "rr":
dialer = newRRDialer(fwdrs, c.CheckWebSite, c.CheckInterval)
log.F("forward to remote servers in round robin mode.")
2018-01-13 20:08:49 +08:00
case "ha":
dialer = newHADialer(fwdrs, c.CheckWebSite, c.CheckInterval)
log.F("forward to remote servers in high availability mode.")
case "lha":
dialer = newLHADialer(fwdrs, c.CheckWebSite, c.CheckInterval)
log.F("forward to remote servers in latency based high availability mode.")
case "dh":
dialer = newDHDialer(fwdrs, c.CheckWebSite, c.CheckInterval)
log.F("forward to remote servers in destination hashing mode.")
2018-01-13 20:08:49 +08:00
default:
log.F("not supported forward mode '%s', just use the first forward server.", c.Strategy)
dialer = fwdrs[0]
}
return dialer
}