From 18ee9ca9939b8a22352ba2994f941661a47caa70 Mon Sep 17 00:00:00 2001 From: wweir Date: Fri, 15 Feb 2019 22:47:51 +0800 Subject: [PATCH] Refactor intelligent detect to score logic --- conf/conf.go | 2 +- dns/dns.go | 52 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/conf/conf.go b/conf/conf.go index c0b6566..5bd0f7c 100644 --- a/conf/conf.go +++ b/conf/conf.go @@ -114,7 +114,7 @@ func watchConfigFile() { } } - glog.Infof("watch %s event: %v", Conf.ConfigFile, event) + glog.V(1).Infof("watch %s event: %v", Conf.ConfigFile, event) for i := range OnRefreash { if err := OnRefreash[i](); err != nil { glog.Errorln(err) diff --git a/dns/dns.go b/dns/dns.go index e70298c..6d8ab3d 100644 --- a/dns/dns.go +++ b/dns/dns.go @@ -17,13 +17,14 @@ const colon = byte(':') func StartDNS(dnsServer, listenIP string) { ip := net.ParseIP(listenIP) - suggest := &intelliSuggest{listenIP, 2 * time.Second} + + suggest := &intelliSuggest{listenIP, time.Second} mem.DefaultCache = mem.New(time.Hour) - var dhcpCh chan struct{} + + dhcpCh := make(chan struct{}) if dnsServer != "" { dnsServer = net.JoinHostPort(dnsServer, "53") } else { - dhcpCh = make(chan struct{}) go func() { for { <-dhcpCh @@ -36,6 +37,7 @@ func StartDNS(dnsServer, listenIP string) { glog.Infoln("set dns server to", host) } }() + dhcpCh <- struct{}{} } dns.HandleFunc(".", func(w dns.ResponseWriter, r *dns.Msg) { @@ -108,29 +110,43 @@ func (i *intelliSuggest) GetOne(domain interface{}) (iface interface{}, e error) // give local dial a hand, make it not so easy to be added into suggestions util.HTTPPing(addr, addr, util.Http, i.timeout/50) - pings := []struct { - viaAddr string - port util.Port - }{ - {addr, util.Http}, - {addr, util.Https}, - {i.listenIP, util.Http}, - {i.listenIP, util.Https}, - } - var finish = new(uint32) + var ( + pings = []struct { + viaAddr string + port util.Port + }{ + {addr, util.Http}, + {addr, util.Https}, + {i.listenIP, util.Http}, + {i.listenIP, util.Https}, + } + half = int32(len(pings) / 2) + score = new(int32) + ) for idx := range pings { go func(idx int) { err := util.HTTPPing(pings[idx].viaAddr, addr, pings[idx].port, i.timeout) if err != nil { - if atomic.LoadUint32(finish) == 0 { // fails before first succ - glog.V(1).Infof("PING %s via %s fail: %s", addr, pings[idx].viaAddr, err) + // local ping fail + if pings[idx].viaAddr == addr { + atomic.AddInt32(score, 1) } - } else if atomic.CompareAndSwapUint32(finish, 0, 1) { // first succ + } else { + // remote ping succ if pings[idx].viaAddr == i.listenIP { - conf.AddSuggest(addr) - glog.Infof("added suggest domain: %s\t via: %s", addr, pings[idx].viaAddr) + atomic.AddInt32(score, 1) + + // local ping took longger than remote + } else if atomic.LoadInt32(score) >= half { + atomic.AddInt32(score, 1) } } + + if atomic.LoadInt32(score) > half { + atomic.StoreInt32(score, 0) // avoid redo add action + conf.AddSuggest(addr) + glog.Infof("added suggest domain: %s", addr) + } }(idx) } return