From 88ff8997b51b2280707b066ee59b3c7b6070583a Mon Sep 17 00:00:00 2001 From: nadoo <287492+nadoo@users.noreply.github.com> Date: Thu, 16 Aug 2018 00:01:59 +0800 Subject: [PATCH] strategy: avoid returning nil dialer in lha.nextDialer --- README.md | 6 +++--- conf.go | 3 ++- strategy/strategy.go | 11 +++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9c8f968..fb7a2ba 100644 --- a/README.md +++ b/README.md @@ -215,9 +215,9 @@ Available forward strategies: ha: High Availability mode lha: Latency based High Availability mode -Forwarder option scheme: FORWARD_URL[#OPTIONS] - Available options for forwarders: - priority: set the priority of that forwarder, default:0 +Forwarder option scheme: FORWARD_URL#OPTIONS + priority: set the priority of that forwarder, default:0 + - Examples: socks5://1.1.1.1:1080#priority=100 vmess://[security:]uuid@host:port?alterID=num#priority=200 diff --git a/conf.go b/conf.go index 5bf9e4b..4dbc722 100644 --- a/conf.go +++ b/conf.go @@ -193,7 +193,8 @@ func usage() { fmt.Fprintf(os.Stderr, "Forwarder option scheme: FORWARD_URL#OPTIONS\n") fmt.Fprintf(os.Stderr, " priority: set the priority of that forwarder, default:0\n") - fmt.Fprintf(os.Stderr, " e.g.:\n") + fmt.Fprintf(os.Stderr, " -\n") + fmt.Fprintf(os.Stderr, " Examples:\n") fmt.Fprintf(os.Stderr, " socks5://1.1.1.1:1080#priority=100\n") fmt.Fprintf(os.Stderr, " vmess://[security:]uuid@host:port?alterID=num#priority=200\n") fmt.Fprintf(os.Stderr, "\n") diff --git a/strategy/strategy.go b/strategy/strategy.go index 73f6451..06bfd8d 100644 --- a/strategy/strategy.go +++ b/strategy/strategy.go @@ -248,24 +248,23 @@ func newLHADialer(dialers []*proxy.Forwarder, webhost string, duration int) prox func (lha *lhaDialer) nextDialer(dstAddr string) *proxy.Forwarder { var latency int64 - var d *proxy.Forwarder - for _, fwder := range lha.fwdrs { + for i, fwder := range lha.fwdrs { if fwder.Enabled() { lha.priority = fwder.Priority latency = fwder.Latency() - d = fwder + lha.idx = i break } } - for _, fwder := range lha.fwdrs { + for i, fwder := range lha.fwdrs { if fwder.Enabled() && fwder.Priority >= lha.priority && fwder.Latency() < latency { latency = fwder.Latency() - d = fwder + lha.idx = i } } - return d + return lha.fwdrs[lha.idx] } func (lha *lhaDialer) Dial(network, addr string) (net.Conn, error) {