more pretty logging and ensure query the specific upstream nameserver in async Lookup() function.
This commit is contained in:
parent
063182e0bd
commit
4b1d61a600
31
resolver.go
31
resolver.go
|
@ -23,6 +23,12 @@ func (e ResolvError) Error() string {
|
||||||
return errmsg
|
return errmsg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type RResp struct {
|
||||||
|
msg *dns.Msg
|
||||||
|
nameserver string
|
||||||
|
rtt time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
type Resolver struct {
|
type Resolver struct {
|
||||||
servers []string
|
servers []string
|
||||||
domain_server *suffixTreeNode
|
domain_server *suffixTreeNode
|
||||||
|
@ -126,7 +132,7 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
|
||||||
|
|
||||||
qname := req.Question[0].Name
|
qname := req.Question[0].Name
|
||||||
|
|
||||||
res := make(chan *dns.Msg, 1)
|
res := make(chan *RResp, 1)
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
L := func(nameserver string) {
|
L := func(nameserver string) {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
|
@ -145,11 +151,10 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
|
||||||
if r.Rcode == dns.RcodeServerFailure {
|
if r.Rcode == dns.RcodeServerFailure {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
logger.Debug("%s resolv on %s (%s) ttl: %v", UnFqdn(qname), nameserver, net, rtt)
|
|
||||||
}
|
}
|
||||||
|
re := &RResp{r, nameserver, rtt}
|
||||||
select {
|
select {
|
||||||
case res <- r:
|
case res <- re:
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -163,9 +168,9 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
|
||||||
go L(nameserver)
|
go L(nameserver)
|
||||||
// but exit early, if we have an answer
|
// but exit early, if we have an answer
|
||||||
select {
|
select {
|
||||||
case r := <-res:
|
case re := <-res:
|
||||||
// logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), nameserver, rtt)
|
logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), re.nameserver, re.rtt)
|
||||||
return r, nil
|
return re.msg, nil
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -173,9 +178,9 @@ func (r *Resolver) Lookup(net string, req *dns.Msg) (message *dns.Msg, err error
|
||||||
// wait for all the namservers to finish
|
// wait for all the namservers to finish
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
select {
|
select {
|
||||||
case r := <-res:
|
case re := <-res:
|
||||||
// logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), nameserver, rtt)
|
logger.Debug("%s resolv on %s rtt: %v", UnFqdn(qname), re.nameserver, re.rtt)
|
||||||
return r, nil
|
return re.msg, nil
|
||||||
default:
|
default:
|
||||||
return nil, ResolvError{qname, net, nameservers}
|
return nil, ResolvError{qname, net, nameservers}
|
||||||
}
|
}
|
||||||
|
@ -190,10 +195,12 @@ func (r *Resolver) Nameservers(qname string) []string {
|
||||||
|
|
||||||
ns := []string{}
|
ns := []string{}
|
||||||
if v, found := r.domain_server.search(queryKeys); found {
|
if v, found := r.domain_server.search(queryKeys); found {
|
||||||
logger.Debug("found upstream: %v", v)
|
logger.Debug("%s be found in domain server list, upstream: %v", qname, v)
|
||||||
server := v
|
server := v
|
||||||
nameserver := server + ":53"
|
nameserver := net.JoinHostPort(server, "53")
|
||||||
ns = append(ns, nameserver)
|
ns = append(ns, nameserver)
|
||||||
|
//Ensure query the specific upstream nameserver in async Lookup() function.
|
||||||
|
return ns
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, nameserver := range r.servers {
|
for _, nameserver := range r.servers {
|
||||||
|
|
Loading…
Reference in New Issue