nats-io / nats-server

High-Performance server for NATS.io, the cloud and edge native messaging system.
https://nats.io
Apache License 2.0
15.86k stars 1.4k forks source link

LeafNodes remotes connect does not support dual-stack / Happy Eyeballs #4614

Open paolobarbolini opened 1 year ago

paolobarbolini commented 1 year ago

What version were you using?

nats-server: v2.10.1

What environment was the server running in?

Is this defect reproducible?

  1. Create a server listening on both IPv4 and IPv6 ports
  2. Create A and AAAA DNS records pointing at the server

Scenario 1 - Leafnode is also on a dualstack network

The leafnode uses IPv4 and IPv6 interchangeably.

Scenario 2- Leafnode runs on an IPv4 only network (may also apply to an IPv6 only network)

Depending on how the leafnode shuffles the IP addresses obtained from the DNS query it may connect via IPv4 or may instead try using IPv6 and fail with [ERR] Error trying to connect as leafnode to remote server "(domain name):7422" (attempt 1): dial tcp [(IPv6 address)]:7422: connect: network is unreachable. On second attempt the leafnode will connect over IPv4 so everything's still going to end-up working fine.

Given the capability you are leveraging, describe your expectation?

First scenario

Implement Happy Eyeballs giving preference to IPv6

Second scenario

I'm not sure how far we can get here but it may be possible to detect we're not on a dualstack network and as long as we're able to connect using one of the addresses we ignore errors from other addresses?

Given the expectation, what is the defect you are observing?

The implementation isn't dual-stack aware.

wallyqs commented 1 year ago

Go by default enables HappyEyeballs in TCP connections and will fallback under 300ms, (although we currently do not make it possible to customize the fallback delay): https://github.com/nats-io/nats-server/blob/main/server/util.go#L260-L266

paolobarbolini commented 1 year ago

Go may support it but the current implementation gives it just one random IP per connect attempt so there's nothing Happy Eyeballs can do

https://github.com/nats-io/nats-server/blob/4165f869d2368c123f88ff0597914766883d5385/server/leafnode.go#L519-L534