Happy Eyeballs: Structured Concurrency's Hello World


When explaining structured concurrency to newbies, but as well when the different ways to implement it Happy Eyeballs protocol (RFC 8305) has roven to be very instructional. At the moment there’s at least Trio and libdill version out there.

The nice thing is that it exhibits all the aspects of structured concurrency (cancelation, error handling, different kind of async tasks etc.) which still remining relatively simple.

Another nice feature is that it comes with “easy” and “hard” mode. When proving your initial implementation of structured concurrency you can start with with a stripped down version which does does the DNS query synchronously, then does actual connecting in asynchronous manner. When successful you can switch to the hard mode, where the DNS queries are done asynchronously, as described in the RFC.

This post is not meant to propose anything specific. It just to point out that in Happy Eyeballs we have a kind of common gauge, an example problem we can use to compare different ideas: How easy it is to implement Happy Eyeballs with each proposed solution?

1 Like

Happy Eyeballs is indeed good algorithm to try to implement.

The javadoc for the FiberScope API in Project Loom includes a number of code samples, one of which is described as:

The method is called with an array of socket addresses and returns a SocketChannel connected to one of the addresses. The connection attempts are staggered. A fiber is scheduled to connect to the first socket address and if the connection is not established within a certain time then another fiber is scheduled to try to connect to the next socket address. The staggered attempts continue until a connection is established, none of the connections succeed, or the deadline is reached. In the event that several connections are established then all but one are closed so that the method does not leak resources.”

This may be another interesting example to explore.


Yes, that sounds very much like the simple version of Happy Eyeballs. Can you link to the sample? I would like to put the three implementations side by side and see whether anything interesting could be learned from that.