shared/network: implement Happy Eyeballs via Go's built-in dialer #18234
Why go from 10s to 30s?
The dial timeout has been increased from 10s (previous per-address timeout) to 30s. This changes failure latency for cases where only one address is attempted (e.g. single A record, direct IP, or tcp4/tcp6), potentially making the client hang noticeably longer on unreachable targets. Consider keeping the previous 10s default here, or deriving the timeout from ctx.Deadline (and otherwise leaving Timeout unset) so callers control the connect deadline.
RFC3493Dialer now wraps any DialContext error with the "Cannot connect to" prefix. This includes non-connectivity errors like malformed addresses (e.g. "missing port in address"), which will then be misclassified by IsConnectionError (it only checks for that prefix) and may incorrectly trigger retry/continue logic in callers. Consider validating/parsing the address first (at least for tcp/tcp4/tcp6) and returning the raw error for invalid input, or otherwise ensuring only genuine connection failures get the prefix.
Pull request overview
This PR updates LXD’s shared network dial helper (RFC3493Dialer) to rely on Go’s net.Dialer.DialContext with the original hostname so Go’s built-in Happy Eyeballs behavior can select between IPv6/IPv4 more effectively (rather than manually resolving and trying addresses sequentially).
Changes:
LookupHost + sequential per-IP dialing with a single DialContext call using the original address string.TCP_USER_TIMEOUT setup on successful TCP connections.connectErrorPrefix wrapper.yeah I set this back to 10s now, thanks for catching
I improved the error message situation, similar to how it was before
my bad, thanks for noticing!
Approved these changes.
Thanks for approving @simondeziel
As far as I can see the CI failures are pre-existing, and have nothing to do with this PR; the documentation one hits a timeout and the other errors with 'failed updating instance types' which is because it connects to images.lxd.canonical.com which maps to 127.0.0.1 in CI and gets 'connection refused', and these happen also on other PRs
Thanks for approving @simondeziel
Thanks for the PR!
As far as I can see the CI failures are pre-existing, and have nothing to do with this PR; the documentation one hits a timeout and the other errors with 'failed updating instance types' which is because it connects to images.lxd.canonical.com which maps to 127.0.0.1 in CI and gets 'connection refused', and these happen also on other PRs
Yep, I confirm, not related to the changes at hand :)
Thanks for this.
One thing I was immediately concerned about is whether the loop of the addresses returned from net.LookupHost was originally intended to support hosts with multiple A records (where LXD would attempt to connect to each one, rather than only trying the first one returned by the DNS resolver), and not just a host with both A and AAAA records.
However I found https://github.com/canonical/lxd/commit/9f7a754783bc2ef4391ee2435db37588cdc8444e that originally introduced this and it does seem that it was intended for trying IPv6 connectivity too.
It could still be the case however that introducing this change may break some users who can come (perhaps inadvertently in the case of misconfigured DNS) to depend on this behaviour.
But I think its worth merging anyway as this gives more predictable, modern and standard behaviour.
simondeziel
mbeijen
Replace the manual DNS-resolve + sequential-address loop in RFC3493Dialer with a direct call to net.Dialer.DialContext using the original hostname. Go's dialer has implemented RFC 8305 Happy Eyeballs since Go 1.12: it races IPv6 and IPv4 concurrently with a ~250 ms fallback, eliminating the 10 s per-address stall that occurred when IPv6 was present but unreachable.
TCP keepalive config and TCP_USER_TIMEOUT are preserved. Context propagation to DNS resolution is also fixed as a side-effect.
I ran into this because I was trying to get lxd working on a host with a not-working IPv6. Instead of falling back to ipv4, it ran into timeouts.
The Happy Eyeballs path is a better user experience and also much less code.
Checklist