reactphp / socket

Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP.
https://reactphp.org/socket/
MIT License
1.21k stars 157 forks source link

Immediately try next connection when one attempt fails and fix possible race conditions (happy eyeballs) #232

Closed clue closed 4 years ago

clue commented 4 years ago

This changeset ensures a new connection attempt will be started immediately when one connection attempt fails. This is quite common when connecting on IPv4-only systems where the first IPv6 connection will fail and the next connection attempt should not have to wait 100ms.

The 100ms delay between connection attempts now only triggers when the connection attempt is still pending. Afaict this is not in violation of RFC 8305 and seems to be in line with how other implementations handle this specific situation (https://daniel.haxx.se/blog/2020/03/16/curl-ootw-happy-eyeballs-timeout/, https://www.isc.org/blogs/2011-01-19-multi-homed-server-over-tcp/, https://github.com/curl/curl/issues/2281 and others).

This can easily be reproduced by connecting to localhost:1 when nothing is listening on this port. Prior to this patch, this would take around ~160ms on my system, after applying this patch it takes around ~50ms. Likewise, connection setup is improved on IPv4-only systems (such as Docker in its default configuration) when the destination is reachable over both IPv6 and IPv4.

Additionally, this changeset fixes two subtle bugs when the connector rejects immediately and when DNS resolves with the same IP address multiple times.

Builds on top of #231, #230, #224 and #225