fix: rtt time should include tcp.Dial

Ref https://github.com/mr-karan/doggo/issues/39
pull/41/head
Karan Sharma 2022-05-17 23:34:30 +05:30
parent 8d705245a1
commit 0ce04d0c13
1 changed files with 12 additions and 4 deletions

View File

@ -1,6 +1,8 @@
package resolvers package resolvers
import ( import (
"time"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -65,11 +67,15 @@ func (r *ClassicResolver) Lookup(question dns.Question) (Response, error) {
"ndots": r.resolverOptions.Ndots, "ndots": r.resolverOptions.Ndots,
"nameserver": r.server, "nameserver": r.server,
}).Debug("Attempting to resolve") }).Debug("Attempting to resolve")
in, rtt, err := r.client.Exchange(&msg, r.server)
// Since the library doesn't include tcp.Dial time,
// it's better to not rely on `rtt` provided here and calculate it ourselves.
now := time.Now()
in, _, err := r.client.Exchange(&msg, r.server)
if err != nil { if err != nil {
return rsp, err return rsp, err
} }
// pack questions in output. // Pack questions in output.
for _, q := range msg.Question { for _, q := range msg.Question {
ques := Question{ ques := Question{
Name: q.Name, Name: q.Name,
@ -78,13 +84,15 @@ func (r *ClassicResolver) Lookup(question dns.Question) (Response, error) {
} }
rsp.Questions = append(rsp.Questions, ques) rsp.Questions = append(rsp.Questions, ques)
} }
// get the authorities and answers. rtt := time.Since(now)
// Get the authorities and answers.
output := parseMessage(in, rtt, r.server) output := parseMessage(in, rtt, r.server)
rsp.Authorities = output.Authorities rsp.Authorities = output.Authorities
rsp.Answers = output.Answers rsp.Answers = output.Answers
if len(output.Answers) > 0 { if len(output.Answers) > 0 {
// stop iterating the searchlist. // Stop iterating the searchlist.
break break
} }
} }