Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarazzutti committed Aug 16, 2023
1 parent 95c8ba8 commit c64fb2c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 42 deletions.
4 changes: 3 additions & 1 deletion app/pinger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ func (webClientBuilderMock *webClientBuilderMock) URL() string {
return "https://www.google.com"
}

func (webClientBuilderMock *webClientBuilderMock) SetURL(_ *url.URL) {}
func (webClientBuilderMock *webClientBuilderMock) SetURL(url *url.URL) {
// not needed
}

func (webClientBuilderMock *webClientBuilderMock) NewInstance() WebClient {
return &webClientMock{}
Expand Down
1 change: 0 additions & 1 deletion app/pinglogger.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func newVerboseLogger(config *Config, stdout io.Writer, pinger Pinger) logger {
}

func (logger *verboseLogger) onMeasure(measure *HTTPMeasure) {

if strings.HasPrefix(measure.Proto, "HTTP/3") {
measure.MeasuresCollection.Set(stats.QUIC, measure.MeasuresCollection.Get(stats.TLS))
measure.MeasuresCollection.Set(stats.TLS, stats.MeasureNotValid)
Expand Down
69 changes: 35 additions & 34 deletions app/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ import (
)

type resolver struct {
config *Config
cache map[string]*net.IPAddr
config *Config
cache map[string]*net.IPAddr
dnsResolver *dnsr.Resolver
}

func newResolver(config *Config) *resolver {
return &resolver{
config: config,
cache: make(map[string]*net.IPAddr),
config: config,
cache: make(map[string]*net.IPAddr),
dnsResolver: dnsr.NewResolver(dnsr.WithCache(1024)),
}
}

Expand Down Expand Up @@ -78,18 +80,18 @@ func resolveWithSpecificServerQtype(qtype uint16, server string, host string) ([
}

func resolveWithSpecificServer(network, server string, host string) ([]*net.IP, error) {

type resolveAnswer struct {
ip []*net.IP
err error
qtype uint16
}

if network == "ip4" {
return resolveWithSpecificServerQtype(dns.TypeA, server, host)
} else if network == "ip6" {
return resolveWithSpecificServerQtype(dns.TypeAAAA, server, host)
} else {
var ipv6Address []*net.IP = nil
var ipv4Address []*net.IP = nil

answersChan := make(chan *resolveAnswer)

Expand All @@ -98,27 +100,31 @@ func resolveWithSpecificServer(network, server string, host string) ([]*net.IP,

answersChan <- &resolveAnswer{out, err, qtype}
}
go ret(dns.TypeA)

go ret(dns.TypeAAAA)
go ret(dns.TypeA)

for i := 0; i < 2; i++ {
if answer := <-answersChan; answer.err == nil && len(answer.ip) > 0 {

if answer.qtype == dns.TypeA {
if answer.qtype == dns.TypeAAAA {
return answer.ip, nil
}

ipv6Address = append(ipv6Address, answer.ip...)
ipv4Address = append(ipv4Address, answer.ip...)
}

}

if ipv6Address == nil {
return nil, &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
if ipv4Address == nil {
return nil, noSuchHostError(host)
}
return ipv6Address, nil
return ipv4Address, nil
}
}

func noSuchHostError(host string) error {
return &net.DNSError{Err: "no such host", Name: host, IsNotFound: true}
}

func (resolver *resolver) resolve(addr string) (*net.IPAddr, error) {
Expand Down Expand Up @@ -148,7 +154,7 @@ func (resolver *resolver) actualResolve(addr string) (*net.IPAddr, error) {
}
}
if ip == nil {
return nil, &net.DNSError{Err: "no such host", Name: addr, IsNotFound: true}
return nil, noSuchHostError(addr)
}
return &net.IPAddr{IP: ip}, nil
} else if resolver.config.DNSServer != "" {
Expand All @@ -158,7 +164,7 @@ func (resolver *resolver) actualResolve(addr string) (*net.IPAddr, error) {
}

if len(ip) == 0 {
return nil, &net.DNSError{Err: "no such host", Name: addr, IsNotFound: true}
return nil, noSuchHostError(addr)
}

return &net.IPAddr{IP: *ip[0]}, nil
Expand All @@ -167,7 +173,7 @@ func (resolver *resolver) actualResolve(addr string) (*net.IPAddr, error) {
}
}

func (*resolver) fullResolveFromRoot(network, host string) (*string, error) {
func (resolver *resolver) fullResolveFromRoot(network, host string) (*string, error) {
var qtypes []string

if network == "ip" {
Expand All @@ -180,30 +186,25 @@ func (*resolver) fullResolveFromRoot(network, host string) (*string, error) {
qtypes = []string{}
}

r := dnsr.NewResolver(dnsr.WithCache(1024))
requestCount := 0
return resolver.resolveRecu(host, qtypes)
}

var resolveRecu func(r *dnsr.Resolver, host string) (*string, error)
func (resolver *resolver) resolveRecu(host string, qtypes []string) (*string, error) {

resolveRecu = func(r *dnsr.Resolver, host string) (*string, error) {
requestCount++
cnames := make(map[string]struct{})
for _, qtype := range qtypes {
for _, rr := range r.Resolve(host, qtype) {
if rr.Type == qtype {
return &rr.Value, nil
} else if rr.Type == "CNAME" {
cnames[rr.Value] = struct{}{}
}
cnames := make(map[string]struct{})
for _, qtype := range qtypes {
for _, rr := range resolver.dnsResolver.Resolve(host, qtype) {
if rr.Type == qtype {
return &rr.Value, nil
} else if rr.Type == "CNAME" {
cnames[rr.Value] = struct{}{}
}
}
}

for cname := range cnames {
return resolveRecu(r, cname)
}

return nil, fmt.Errorf("no host found: %s", host)
for cname := range cnames {
return resolver.resolveRecu(cname, qtypes)
}

return resolveRecu(r, host)
return nil, fmt.Errorf("no host found: %s", host)
}
15 changes: 9 additions & 6 deletions app/webclientimpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {

if err != nil {
return &HTTPMeasure{
IsFailure: true,
FailureCause: err.Error(),
IsFailure: true,
FailureCause: err.Error(),
MeasuresCollection: timerRegistry.Measure(),
}
}

Expand Down Expand Up @@ -365,8 +366,9 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {

if err != nil {
return &HTTPMeasure{
IsFailure: true,
FailureCause: err.Error(),
IsFailure: true,
FailureCause: err.Error(),
MeasuresCollection: timerRegistry.Measure(),
}
}
return webClient.DoMeasure(followRedirect)
Expand All @@ -378,8 +380,9 @@ func (webClient *webClientImpl) DoMeasure(followRedirect bool) *HTTPMeasure {
s, err := io.Copy(io.Discard, res.Body)
if err != nil {
return &HTTPMeasure{
IsFailure: true,
FailureCause: "I/O error while reading payload",
IsFailure: true,
FailureCause: "I/O error while reading payload",
MeasuresCollection: timerRegistry.Measure(),
}
}

Expand Down

0 comments on commit c64fb2c

Please sign in to comment.