-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow FQDN lookup functions to take context #199
Conversation
@@ -28,7 +31,7 @@ type Host interface { | |||
Memory() (*HostMemoryInfo, error) | |||
|
|||
// FQDN returns the fully-qualified domain name of the host, lowercased. | |||
FQDN() (string, error) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a breaking change to the API. I suggest adding an FQDNWithContext(context.Context)
unless we are prepared to rollout a github.com/elastic/go-sysinfo/v2
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8ef547d.
if err != nil { | ||
errs = fmt.Errorf("%s: failed looking up IP: %w", errs, err) | ||
} | ||
|
||
for _, ip := range ips { | ||
names, err := net.LookupAddr(ip.String()) | ||
names, err := net.DefaultResolver.LookupAddr(ctx, ip.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of ips
here changed from []net.IP
to []net.IPAddr
which doesn't have the same implementation of String()
, since net.IPAddr embeds IP
and extends it with a zone.
If you want the exact same conversion as before you want to call ip.IP.String()
I think.
func (a *IPAddr) String() string {
if a == nil {
return "<nil>"
}
ip := ipEmptyString(a.IP)
if a.Zone != "" {
return ip + "%" + a.Zone
}
return ip
}
The original call to net.LookupIP
stripped off the IPv6 zone: https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/net/lookup.go;l=195
// LookupIP looks up host using the local resolver.
// It returns a slice of that host's IPv4 and IPv6 addresses.
func LookupIP(host string) ([]IP, error) {
addrs, err := DefaultResolver.LookupIPAddr(context.Background(), host)
if err != nil {
return nil, err
}
ips := make([]IP, len(addrs))
for i, ia := range addrs {
ips[i] = ia.IP
}
return ips, nil
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type of
ips
here changed from[]net.IP
to[]net.IPAddr
...
Are you sure? Before, ips
was initialized as:
ips, err := net.LookupIP(hostname)
Now, it's initialized as:
ips, err := net.DefaultResolver.LookupIP(ctx, "ip", hostname)
In both cases, the type of ips
is the same: []IP
.
net.LookupIP
: https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/net/lookup.go;l=193-205net.DefaultResolver.LookupIP
: https://cs.opensource.google/go/go/+/refs/tags/go1.21.6:src/net/lookup.go;l=213-234
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah you are right for some reason I ended up focusing on LookupIPAddr as if it were called early, I think this was because net.LookupIP
calls LookupIPAddr but func (r *Resolver) LookupIP
does something else.
I don't have enough brainpower left for the day or network knowledge to know if that is significant, I was trying to quickly check if this worked the same as it did before.
This PR changes the FQDN lookup functions to take a
context.Context
, thus allowing callers of these functions to pass a context with a timeout to prevent indefinitely waiting for DNS resolution.