Skip to content

Commit d21b614

Browse files
authored
[DOC] Improve Socket::tcp with Happy Eyeballs Version 2
With the introduction of Happy Eyeballs Version 2 to `Socket::tcp`, the following areas have been mainly enhanced: - How the value specified for `connect_timeout` works - How Socket.tcp operates with Happy Eyeballs Version 2 A description for the new option `fast_fallback` has been added in ruby#11813.
1 parent fe17697 commit d21b614

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

ext/socket/lib/socket.rb

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,15 +629,22 @@ class << self
629629
#
630630
# creates a new socket object connected to host:port using TCP/IP.
631631
#
632+
# Starting from Ruby 3.4, this method operates according to the
633+
# Happy Eyeballs Version 2 ({RFC 8305}[https://datatracker.ietf.org/doc/html/rfc8305])
634+
# algorithm by default.
635+
#
636+
# To make it behave the same as in Ruby 3.3 and earlier,
637+
# explicitly specify the option +fast_fallback:false+.
638+
#
632639
# If local_host:local_port is given,
633640
# the socket is bound to it.
634641
#
635642
# The optional last argument _opts_ is options represented by a hash.
636643
# _opts_ may have following options:
637644
#
638-
# [:resolv_timeout] specify the timeout of hostname resolution in seconds.
639-
# [:connect_timeout] specify the timeout of conncetion in seconds.
640-
# [:fast_fallback] enable Happy Eyeballs Version 2 ({RFC 8305}[https://datatracker.ietf.org/doc/html/rfc8305]) algorithm (Enabled by default).
645+
# [:resolv_timeout] Specifies the timeout in seconds from when the hostname resolution starts.
646+
# [:connect_timeout] This method sequentially attempts connecting to all candidate destination addresses.<br>The +connect_timeout+ specifies the timeout in seconds from the start of the connection attempt to the last candidate.<br>By default, all connection attempts continue until the timeout occurs.<br>When +fast_fallback:false+ is explicitly specified,<br>a timeout is set for each connection attempt and any connection attempt that exceeds its timeout will be canceled.
647+
# [:fast_fallback] Enables the Happy Eyeballs Version 2 algorithm (enabled by default).
641648
#
642649
# If a block is given, the block is called with the socket.
643650
# The value of the block is returned.
@@ -650,6 +657,24 @@ class << self
650657
# sock.close_write
651658
# puts sock.read
652659
# }
660+
#
661+
# === Happy Eyeballs Version 2
662+
# Happy Eyeballs Version 2 ({RFC 8305}[https://datatracker.ietf.org/doc/html/rfc8305])
663+
# is an algorithm designed to improve client socket connectivity.<br>
664+
# It aims for more reliable and efficient connections by performing hostname resolution
665+
# and connection attempts in parallel, instead of serially.
666+
#
667+
# Starting from Ruby 3.4, this method operates as follows with this algorithm:
668+
#
669+
# 1. Start resolving both IPv6 and IPv4 addresses concurrently.
670+
# 2. Start connecting to the one of the addresses that are obtained first.<br>If IPv4 addresses are obtained first,
671+
# the method waits 50 ms for IPv6 name resolution to prioritize IPv6 connections.
672+
# 3. After starting a connection attempt, wait 250 ms for the connection to be established.<br>
673+
# If no connection is established within this time, a new connection is started every 250 ms<br>
674+
# until a connection is established or there are no more candidate addresses.<br>
675+
# (Although RFC 8305 strictly specifies sorting addresses,<br>
676+
# this method only alternates between IPv6 / IPv4 addresses due to the performance concerns)
677+
# 4. Once a connection is established, all remaining connection attempts are canceled.
653678
def self.tcp(host, port, local_host = nil, local_port = nil, connect_timeout: nil, resolv_timeout: nil, fast_fallback: tcp_fast_fallback, &) # :yield: socket
654679
sock = if fast_fallback && !(host && ip_address?(host))
655680
tcp_with_fast_fallback(host, port, local_host, local_port, connect_timeout:, resolv_timeout:)

0 commit comments

Comments
 (0)