From 0337e9821588b50424fdf2491873c4d8402723a0 Mon Sep 17 00:00:00 2001 From: Mac_Zhou Date: Tue, 5 Mar 2019 13:42:46 +0800 Subject: [PATCH] strategy: add features latencythreshold --- conf.go | 1 + strategy/strategy.go | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/conf.go b/conf.go index df1ebc1..e914844 100644 --- a/conf.go +++ b/conf.go @@ -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") // TODO: change to checkinterval 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.StringVar(&conf.StrategyConfig.IntFace, "interface", "", "source ip or source interface") diff --git a/strategy/strategy.go b/strategy/strategy.go index 2583b27..d87361a 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -25,6 +25,7 @@ type Config struct { Strategy string CheckWebSite string CheckInterval int + LatencyThreshold int MaxFailures int IntFace string } @@ -229,11 +230,18 @@ func (d *Dialer) check(i int) { f.Disable() 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) { - f.Enable() retry = 2 readTime := time.Since(startTime) - f.SetLatency(int64(readTime)) - log.F("[check] %s(%d) -> %s, ENABLED. connect time: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, readTime.String()) + readLatency := int64(readTime) / 1e6 + 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 { f.Disable() log.F("[check] %s(%d) -> %s, DISABLED. server response: %s", f.Addr(), f.Priority(), d.config.CheckWebSite, buf)