Skip to content

Conversation

@magurotuna
Copy link
Member

@magurotuna magurotuna commented Dec 27, 2025

Implements the Happy Eyeballs v2 (RFC 8305) for TCP and TLS connections, enabling faster connection establishment on dual-stack networks by racing IPv6 and IPv4 addresses with staggered parallel attempts.

Closes #25661

Changes

  • Add ext/net/happy_eyeballs.rs with the core algorithm implementation
  • Update Deno.connect and Deno.connectTls to use Happy Eyeballs when multiple addresses are resolved
  • Add two new options to Deno.ConnectOptions and Deno.ConnectTlsOptions:
    • autoSelectFamily: Enable/disable Happy Eyeballs (default: true)
    • autoSelectFamilyAttemptDelay: Delay between connection attempts in ms (default: 250)

How it works

  1. DNS resolution returns multiple addresses (potentially both IPv6 and IPv4)
  2. Addresses are interleaved: IPv6 first, then IPv4, alternating (per RFC 8305 Section 4)
  3. First connection attempt starts immediately
  4. New attempts start at configurable intervals (default: 250ms) while previous attempts continue in parallel
  5. First successful connection wins; all other pending attempts are cancelled

This avoids the common problem where a broken IPv6 path causes long delays before falling back to IPv4.

@magurotuna magurotuna changed the title feat(net): implement Happy Eyeballs for Deno.connect and Deno.connectTls feat(ext/net): implement Happy Eyeballs for Deno.connect and Deno.connectTls Dec 27, 2025
@magurotuna magurotuna marked this pull request as ready for review December 27, 2025 17:29
@magurotuna
Copy link
Member Author

Node compat layer have already had its own Happy Eyeballs logic implemented in JS. So initially I thought it would make sense to implement it in Rust so that both Deno.connect() and Node's net.connect() can leave the Happy Eyeballs logic to the Rust side, which would allow us to remove it from JS. However, Node's implementation has several points where events are emitted such as connectionAttempt or connectionAttemptFailed, making it hard to perform the Happy Eyeballs algorithm entirely in Rust, as there's no mechanism to emit JS events from Rust.
https://nodejs.org/api/net.html#event-connectionattemptfailed

@Hajime-san
Copy link
Contributor

there's no mechanism to emit JS events from Rust.

Has this refactor made it available?

@bartlomieju
Copy link
Member

there's no mechanism to emit JS events from Rust.

Has this refactor made it available?

Yes, but we benched it and at the moment it's 10x slower than emitting events from JS.

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.

Implement Happy Eyeballs for TCP connections

3 participants