Skip to content
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

Merged
merged 8 commits into from
Feb 1, 2024

Conversation

ycombinator
Copy link
Contributor

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.

@@ -28,7 +31,7 @@ type Host interface {
Memory() (*HostMemoryInfo, error)

// FQDN returns the fully-qualified domain name of the host, lowercased.
FQDN() (string, error)
Copy link
Member

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.

Copy link
Contributor Author

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())
Copy link
Member

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
}

Copy link
Contributor Author

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.

Copy link
Member

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.

@ycombinator ycombinator merged commit 489579d into elastic:main Feb 1, 2024
23 checks passed
@ycombinator ycombinator deleted the fqdn-ctx branch February 1, 2024 01:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants