From 0ce04d0c13b27bf8b5ec75f43993bb09b79bf010 Mon Sep 17 00:00:00 2001 From: Karan Sharma Date: Tue, 17 May 2022 23:34:30 +0530 Subject: [PATCH] fix: rtt time should include tcp.Dial Ref https://github.com/mr-karan/doggo/issues/39 --- pkg/resolvers/classic.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/pkg/resolvers/classic.go b/pkg/resolvers/classic.go index 38ea93d..f72c010 100644 --- a/pkg/resolvers/classic.go +++ b/pkg/resolvers/classic.go @@ -1,6 +1,8 @@ package resolvers import ( + "time" + "github.com/miekg/dns" "github.com/sirupsen/logrus" ) @@ -65,11 +67,15 @@ func (r *ClassicResolver) Lookup(question dns.Question) (Response, error) { "ndots": r.resolverOptions.Ndots, "nameserver": r.server, }).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 { return rsp, err } - // pack questions in output. + // Pack questions in output. for _, q := range msg.Question { ques := Question{ Name: q.Name, @@ -78,13 +84,15 @@ func (r *ClassicResolver) Lookup(question dns.Question) (Response, error) { } 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) rsp.Authorities = output.Authorities rsp.Answers = output.Answers if len(output.Answers) > 0 { - // stop iterating the searchlist. + // Stop iterating the searchlist. break } }