Skip to content

Commit

Permalink
Merge branch 'feature/score' @Release
Browse files Browse the repository at this point in the history
  • Loading branch information
wweir committed Feb 15, 2019
2 parents 4fff2a1 + 18ee9ca commit 4883e24
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
52 changes: 34 additions & 18 deletions dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4883e24

Please sign in to comment.