summaryrefslogtreecommitdiff
path: root/ext/socket/tcpsocket.c
diff options
context:
space:
mode:
authorMisaki Shioi <[email protected]>2024-12-14 15:51:19 +0900
committerGitHub <[email protected]>2024-12-14 15:51:19 +0900
commit9f924e2f13992241c447190a9eb139bf46dcb8d9 (patch)
treeac6acd2fa5b2fe57d895266a762999343c3ae160 /ext/socket/tcpsocket.c
parent77016a7b4341d861b83e222c18333204b63f480b (diff)
Improve APIs for Globally Enabling/Disabling fast_fallback in Socket (#12257)
This change includes the following updates: - Added an environment variable `RUBY_TCP_NO_FAST_FALLBACK` to control enabling/disabling fast_fallback - Updated documentation and man pages - Revised the implementation of Socket.tcp_fast_fallback= and Socket.tcp_fast_fallback, which previously performed dynamic name resolution of constants and variables. As a result, the following performance improvements were achieved: (Case of 1000 executions of `TCPSocket.new` to the local host) Rehearsal ----------------------------------------- before 0.031462 0.147946 0.179408 ( 0.249279) after 0.031164 0.146839 0.178003 ( 0.346935) -------------------------------- total: 0.178003sec user system total real before 0.027584 0.138712 0.166296 ( 0.233356) after 0.025953 0.127608 0.153561 ( 0.237971)
Notes
Notes: Merged-By: shioimm <[email protected]>
Diffstat (limited to 'ext/socket/tcpsocket.c')
-rw-r--r--ext/socket/tcpsocket.c31
1 files changed, 8 insertions, 23 deletions
diff --git a/ext/socket/tcpsocket.c b/ext/socket/tcpsocket.c
index e091d75e5d..93618d2d90 100644
--- a/ext/socket/tcpsocket.c
+++ b/ext/socket/tcpsocket.c
@@ -22,33 +22,20 @@
* Happy Eyeballs Version 2 ({RFC 8305}[https://datatracker.ietf.org/doc/html/rfc8305])
* algorithm by default, except on Windows.
*
+ * For details on Happy Eyeballs Version 2,
+ * see {Socket.tcp_fast_fallback=}[rdoc-ref:Socket#tcp_fast_fallback=].
+ *
* To make it behave the same as in Ruby 3.3 and earlier,
- * explicitly specify the option +fast_fallback:false+.
+ * explicitly specify the option fast_fallback:false.
+ * Or, setting Socket.tcp_fast_fallback=false will disable
+ * Happy Eyeballs Version 2 not only for this method but for all Socket globally.
*
- * Happy Eyeballs Version 2 is not provided on Windows,
+ * When using TCPSocket.new on Windows, Happy Eyeballs Version 2 is not provided,
* and it behaves the same as in Ruby 3.3 and earlier.
*
* [:resolv_timeout] Specifies the timeout in seconds from when the hostname resolution starts.
* [: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.
* [:fast_fallback] Enables the Happy Eyeballs Version 2 algorithm (enabled by default).
- *
- * === Happy Eyeballs Version 2
- * Happy Eyeballs Version 2 ({RFC 8305}[https://datatracker.ietf.org/doc/html/rfc8305])
- * is an algorithm designed to improve client socket connectivity.<br>
- * It aims for more reliable and efficient connections by performing hostname resolution
- * and connection attempts in parallel, instead of serially.
- *
- * Starting from Ruby 3.4, this method operates as follows with this algorithm except on Windows:
- *
- * 1. Start resolving both IPv6 and IPv4 addresses concurrently.
- * 2. Start connecting to the one of the addresses that are obtained first.<br>If IPv4 addresses are obtained first,
- * the method waits 50 ms for IPv6 name resolution to prioritize IPv6 connections.
- * 3. After starting a connection attempt, wait 250 ms for the connection to be established.<br>
- * If no connection is established within this time, a new connection is started every 250 ms<br>
- * until a connection is established or there are no more candidate addresses.<br>
- * (Although RFC 8305 strictly specifies sorting addresses,<br>
- * this method only alternates between IPv6 / IPv4 addresses due to the performance concerns)
- * 4. Once a connection is established, all remaining connection attempts are canceled.
*/
static VALUE
tcp_init(int argc, VALUE *argv, VALUE sock)
@@ -82,9 +69,7 @@ tcp_init(int argc, VALUE *argv, VALUE sock)
}
if (fast_fallback == Qnil) {
- VALUE socket_class = rb_const_get(rb_cObject, rb_intern("Socket"));
- ID var_id = rb_intern("@tcp_fast_fallback");
- fast_fallback = rb_ivar_get(socket_class, var_id);
+ fast_fallback = rb_ivar_get(rb_cSocket, tcp_fast_fallback);
if (fast_fallback == Qnil) fast_fallback = Qtrue;
}