mirror of
https://github.com/nadoo/glider.git
synced 2025-02-23 09:25:41 +08:00
forwarder: set status to disabled when dialing fails MaxFailures times
This commit is contained in:
parent
e1c318990b
commit
b5b7f2998b
3
conf.go
3
conf.go
@ -45,7 +45,9 @@ func confInit() {
|
||||
flag.StringSliceUniqVar(&conf.Forward, "forward", nil, "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]")
|
||||
flag.StringVar(&conf.StrategyConfig.Strategy, "strategy", "rr", "forward strategy, default: rr")
|
||||
flag.StringVar(&conf.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80")
|
||||
// TODO: change to checkinterval
|
||||
flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)")
|
||||
flag.IntVar(&conf.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change status to disabled")
|
||||
|
||||
flag.StringSliceUniqVar(&conf.RuleFile, "rulefile", nil, "rule file path")
|
||||
flag.StringVar(&conf.RulesDir, "rules-dir", "", "rule file folder")
|
||||
@ -143,6 +145,7 @@ func NewRuleConfFromFile(ruleFile string) (*RuleConf, error) {
|
||||
f.StringSliceUniqVar(&p.Forward, "forward", nil, "forward url, format: SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS[,SCHEME://[USER|METHOD:PASSWORD@][HOST]:PORT?PARAMS]")
|
||||
f.StringVar(&p.StrategyConfig.Strategy, "strategy", "rr", "forward strategy, default: rr")
|
||||
f.StringVar(&p.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80")
|
||||
// TODO: change to checkinterval
|
||||
f.IntVar(&p.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)")
|
||||
|
||||
f.StringSliceUniqVar(&p.DNSServers, "dnsserver", nil, "remote dns server")
|
||||
|
@ -6,18 +6,17 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/nadoo/glider/common/log"
|
||||
)
|
||||
|
||||
// Forwarder is a forwarder
|
||||
type Forwarder struct {
|
||||
Dialer
|
||||
Priority int
|
||||
addr string
|
||||
disabled uint32
|
||||
failures uint32
|
||||
latency int
|
||||
Priority int
|
||||
addr string
|
||||
disabled uint32
|
||||
failures uint32
|
||||
MaxFailures uint32 //maxfailures to set to Disabled
|
||||
latency int
|
||||
}
|
||||
|
||||
// ForwarderFromURL returns a new forwarder
|
||||
@ -68,11 +67,12 @@ func (f *Forwarder) Addr() string {
|
||||
// Dial .
|
||||
func (f *Forwarder) Dial(network, addr string) (c net.Conn, err error) {
|
||||
c, err = f.Dialer.Dial(network, addr)
|
||||
|
||||
// TODO: proxy timeout, target timeout?
|
||||
if err != nil {
|
||||
atomic.AddUint32(&f.failures, 1)
|
||||
log.F("forward dial failed, %d, addr: %s", f.failures, f.addr)
|
||||
}
|
||||
|
||||
if f.Failures() >= f.MaxFailures {
|
||||
f.Disable()
|
||||
}
|
||||
|
||||
return c, err
|
||||
@ -86,6 +86,7 @@ func (f *Forwarder) Failures() uint32 {
|
||||
// Enable .
|
||||
func (f *Forwarder) Enable() {
|
||||
atomic.StoreUint32(&f.disabled, 0)
|
||||
atomic.StoreUint32(&f.failures, 0)
|
||||
}
|
||||
|
||||
// Disable .
|
||||
|
@ -17,6 +17,7 @@ type Config struct {
|
||||
Strategy string
|
||||
CheckWebSite string
|
||||
CheckInterval int
|
||||
MaxFailures int
|
||||
}
|
||||
|
||||
// NewDialer returns a new strategy dialer
|
||||
@ -28,6 +29,7 @@ func NewDialer(s []string, c *Config) proxy.Dialer {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fwdr.MaxFailures = uint32(c.MaxFailures)
|
||||
fwdrs = append(fwdrs, fwdr)
|
||||
}
|
||||
|
||||
@ -82,7 +84,6 @@ func newRRDialer(fs []*proxy.Forwarder, website string, interval int) *rrDialer
|
||||
rr.priority = rr.fwdrs[0].Priority
|
||||
|
||||
for k := range rr.fwdrs {
|
||||
log.F("k: %d, %s, priority: %d", k, rr.fwdrs[k].Addr(), rr.fwdrs[k].Priority)
|
||||
go rr.checkDialer(k)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user