strategy: add features latencythreshold

This commit is contained in:
Mac_Zhou 2019-03-05 13:42:46 +08:00
parent d5a9d680a9
commit 0337e98215
2 changed files with 12 additions and 3 deletions

View File

@ -41,6 +41,7 @@ func confInit() {
flag.StringVar(&conf.StrategyConfig.CheckWebSite, "checkwebsite", "www.apple.com", "proxy check HTTP(NOT HTTPS) website address, format: HOST[:PORT], default port: 80") 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 // TODO: change to checkinterval
flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)") flag.IntVar(&conf.StrategyConfig.CheckInterval, "checkduration", 30, "proxy check interval(seconds)")
flag.IntVar(&conf.StrategyConfig.LatencyThreshold, "latencythreshold", 10000, "proxy check latency time(millisecond)")
flag.IntVar(&conf.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled") flag.IntVar(&conf.StrategyConfig.MaxFailures, "maxfailures", 3, "max failures to change forwarder status to disabled")
flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface") flag.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface")

View File

@ -25,6 +25,7 @@ type Config struct {
Strategy string Strategy string
CheckWebSite string CheckWebSite string
CheckInterval int CheckInterval int
LatencyThreshold int
MaxFailures int MaxFailures int
IntFace string IntFace string
} }
@ -229,11 +230,18 @@ func (d *Dialer) check(i int) {
f.Disable() f.Disable()
log.F("[check] %s(%d) -> %s, DISABLED. error in read: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, err) log.F("[check] %s(%d) -> %s, DISABLED. error in read: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, err)
} else if bytes.Equal([]byte("HTTP"), buf) { } else if bytes.Equal([]byte("HTTP"), buf) {
f.Enable()
retry = 2 retry = 2
readTime := time.Since(startTime) readTime := time.Since(startTime)
f.SetLatency(int64(readTime)) readLatency := int64(readTime) / 1e6
log.F("[check] %s(%d) -> %s, ENABLED. connect time: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, readTime.String()) LatencyThreshold := int64(d.config.LatencyThreshold)
if readLatency > LatencyThreshold {
f.Disable()
log.F("[check] %s(%d) -> %s, DISABLED. connect time: %s > latencythreshold: %dms", f.Addr(), f.Priority(), d.config.CheckWebSite, readTime.String(), LatencyThreshold)
} else {
f.Enable()
f.SetLatency(int64(readTime))
log.F("[check] %s(%d) -> %s, ENABLED. connect time: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, readTime.String())
}
} else { } else {
f.Disable() f.Disable()
log.F("[check] %s(%d) -> %s, DISABLED. server response: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, buf) log.F("[check] %s(%d) -> %s, DISABLED. server response: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, buf)