Skip to content

Commit

Permalink
Merge pull request #2 from thorfour/LookupIPAddr
Browse files Browse the repository at this point in the history
use LookupIPAddr to support context timeout
  • Loading branch information
thorfour authored Apr 11, 2019
2 parents 561b4f3 + 0742784 commit 61244ed
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions prober/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,45 +61,45 @@ func chooseProtocol(ctx context.Context, IPProtocol string, fallbackIPProtocol b
probeDNSLookupTimeSeconds.Add(lookupTime)
}()

ipC := make(chan *net.IPAddr, 1)
errors := make(chan error, 1)
go func() {
defer close(ipC)
defer close(errors)
ip, err := net.ResolveIPAddr(IPProtocol, target)
if err != nil {
if !fallbackIPProtocol {
level.Error(logger).Log("msg", "Resolution with IP protocol failed (fallback_ip_protocol is false):", "err", err)
} else {
level.Warn(logger).Log("msg", "Resolution with IP protocol failed, attempting fallback protocol", "fallback_protocol", fallbackProtocol, "err", err)
ip, err = net.ResolveIPAddr(fallbackProtocol, target)
resolver := &net.Resolver{}
ips, err := resolver.LookupIPAddr(ctx, target)
if err != nil {
level.Error(logger).Log("msg", "Resolution with IP protocol failed", "err", err)
return nil, 0.0, err
}

// Return the IP in the requested protocol format
var fallback net.IPAddr
for _, ip := range ips {
switch IPProtocol {
case "ip4":
if ip.IP.To4() != nil {
level.Info(logger).Log("msg", "Resolved target address", "ip", ip)
probeIPProtocolGauge.Set(4)
return &ip, lookupTime, nil
}

if err != nil {
if IPProtocol == "ip6" {
probeIPProtocolGauge.Set(6)
} else {
probeIPProtocolGauge.Set(4)
}
errors <- err
return
// ip4 as fallback
fallback = ip

case "ip6":

if ip.IP.To4() == nil {
level.Info(logger).Log("msg", "Resolved target address", "ip", ip)
probeIPProtocolGauge.Set(6)
return &ip, lookupTime, nil
}
}

if ip.IP.To4() == nil {
probeIPProtocolGauge.Set(6)
} else {
probeIPProtocolGauge.Set(4)
// ip6 as fallback
fallback = ip
}
}

ipC <- ip
}()

select {
case <-ctx.Done():
return nil, lookupTime, ctx.Err()
case ip := <-ipC:
level.Info(logger).Log("msg", "Resolved target address", "ip", ip)
return ip, lookupTime, nil
// Use fallback ip protocol
if fallbackProtocol == "ip4" {
probeIPProtocolGauge.Set(4)
} else {
probeIPProtocolGauge.Set(6)
}
return &fallback, lookupTime, nil
}

0 comments on commit 61244ed

Please sign in to comment.