fetch_connect() tried the addresses of a host one after the other, each
with the full fetchTimeout. A host with an IPv6 address that is routed
nowhere, which is what a network with IPv6 configured but not working
looks like, cost the whole timeout (or the kernel's 75 second SYN
timeout without one) before IPv4 was even attempted, on every single
connection.
Implement the connection part of RFC 8305 ("Happy Eyeballs"): start the
next attempt 250 ms after the previous one if it has not answered yet,
or at once if it failed, and use the first connection to complete. The
addresses are ordered so that address families alternate, because
getaddrinfo() lists all the addresses of the preferred family first and
several unreachable IPv6 addresses would otherwise delay IPv4 by 250 ms
each. fetchTimeout now bounds connecting to the host as a whole rather
than each address, so the worst case is one timeout, not one per
address. A failed attempt is diagnosed with SO_ERROR instead of being
reported as refused.
With several attempts in flight, a socket() failure now skips that
address instead of ending the connection, unless the process is out of
descriptors (EMFILE or ENFILE). Then the address waits until no attempt
is left, and every attempt has a fetchTimeout of its own, as before this
change, so that a process with a single free descriptor still reaches
each address. A failure to bind to FETCH_BIND_ADDRESS remains
fatal. When fetchRestartCalls is clear, a signal that interrupts the
wait now ends the connection, as it does for reads and writes, instead
of moving on to the next address.
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")