Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
1049057f69 | |||
0622e00f64 | |||
c0f98d4a25 |
@ -172,43 +172,40 @@ func (m measurement) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) probeUpstreamLatency() {
|
func (s *Server) probeUpstreamLatency() {
|
||||||
if !s.once {
|
upstreams := s.upstreams()
|
||||||
s.once = true
|
results := make([]measurement, len(upstreams))
|
||||||
upstreams := s.upstreams()
|
var wg sync.WaitGroup
|
||||||
results := make([]measurement, len(upstreams))
|
for idx, u := range upstreams {
|
||||||
var wg sync.WaitGroup
|
wg.Add(1)
|
||||||
for idx, u := range upstreams {
|
go func(idx int, u string) {
|
||||||
wg.Add(1)
|
defer wg.Done()
|
||||||
go func(idx int, u string) {
|
// resolve a most-definitely cached record
|
||||||
defer wg.Done()
|
m := new(dns.Msg)
|
||||||
// resolve a most-definitely cached record
|
m.SetQuestion("google.ch.", dns.TypeA)
|
||||||
m := new(dns.Msg)
|
start := time.Now()
|
||||||
m.SetQuestion("google.ch.", dns.TypeA)
|
_, _, err := s.client.Exchange(m, u)
|
||||||
start := time.Now()
|
rtt := time.Since(start)
|
||||||
_, _, err := s.client.Exchange(m, u)
|
if err != nil {
|
||||||
rtt := time.Since(start)
|
// including unresponsive upstreams in results makes the update
|
||||||
if err != nil {
|
// code simpler:
|
||||||
// including unresponsive upstreams in results makes the update
|
results[idx] = measurement{u, time.Duration(math.MaxInt64)}
|
||||||
// code simpler:
|
return
|
||||||
results[idx] = measurement{u, time.Duration(math.MaxInt64)}
|
}
|
||||||
return
|
results[idx] = measurement{u, rtt}
|
||||||
}
|
}(idx, u)
|
||||||
results[idx] = measurement{u, rtt}
|
|
||||||
}(idx, u)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
// Re-order by resolving latency:
|
|
||||||
sort.Slice(results, func(i, j int) bool {
|
|
||||||
return results[i].rtt < results[j].rtt
|
|
||||||
})
|
|
||||||
log.Printf("probe results: %v %v", s.once, results)
|
|
||||||
for idx, result := range results {
|
|
||||||
upstreams[idx] = result.upstream
|
|
||||||
}
|
|
||||||
s.upstreamMu.Lock()
|
|
||||||
defer s.upstreamMu.Unlock()
|
|
||||||
s.upstream = upstreams
|
|
||||||
}
|
}
|
||||||
|
wg.Wait()
|
||||||
|
// Re-order by resolving latency:
|
||||||
|
sort.Slice(results, func(i, j int) bool {
|
||||||
|
return results[i].rtt < results[j].rtt
|
||||||
|
})
|
||||||
|
log.Printf("probe results: %v", results)
|
||||||
|
for idx, result := range results {
|
||||||
|
upstreams[idx] = result.upstream
|
||||||
|
}
|
||||||
|
s.upstreamMu.Lock()
|
||||||
|
defer s.upstreamMu.Unlock()
|
||||||
|
s.upstream = upstreams
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) hostByName(n lcHostname) (string, bool) {
|
func (s *Server) hostByName(n lcHostname) (string, bool) {
|
||||||
|
Reference in New Issue
Block a user