ruby/ext/socket
Misaki Shioi b3baa11ee9
Improve Socket.tcp (#11187)
[Feature #20646]Improve Socket.tcp

This is a proposed improvement to `Socket.tcp`, which has implemented Happy Eyeballs version 2 (RFC8305) in PR9374.

1. Background
I implemented Happy Eyeballs version 2 (HEv2) for Socket.tcp in PR9374, but several issues have been identified:

- `IO.select` waits for name resolution or connection establishment in v46w, but it does not consider the case where both events occur simultaneously when it returns a value.
  - In this case, Socket.tcp can only capture one event and needs to execute an unnecessary loop to capture the other one, calling `IO.select` one extra time.
- `IO.select` waits for both IPv6/IPv4 name resolution (in start), but when it returns a value, it doesn't consider the case where name resolution for both address families is complete.
  - In this case, `Socket.tcp` can only obtain the addresses of one address family and needs to execute an unnecessary loop obtain the other addresses, calling `IO.select` one extra time.
- The consideration for `connect_timeout` was insufficient. After initiating one or more connections, it raises a 'user specified timeout' after the `connect_timeout` period even if there were addresses that have been resolved and have not yet tried to connect.
- It does not retry with another address in case of a connection failure.
- It executes unnecessary state transitions even when an IP address is passed as the `host` argument.
- The regex for IP addresses did not correctly specify the start and end.

2. Proposal & Outcome
To overcome the aforementioned issues, this PR introduces the following changes:

- Previously, each loop iteration represented a single state transition. This has been changed to execute all processes that meet the execution conditions within a single loop iteration.
  - This prevents unnecessary repeated loops and calling `IO.select`
- Introduced logic to determine the timeout value set for `IO.select`. During the Resolution Delay and Connection Attempt Delay, the user-specified timeout is ignored. Otherwise, the timeout value is set to the larger of `resolv_timeout` and `connect_timeout`.
  - This ensures that the `connect_timeout` is only detected after attempting to connect to all resolved addresses.
- Retry with another address in case of a connection failure.
  - This prevents unnecessary repeated loops upon connection failure.
- Call `tcp_without_fast_fallback` when an IP address is passed as the host argument.
  - This prevents unnecessary state transitions when an IP address is passed.
- Fixed regex for IP addresses.

Additionally, the code has been reduced by over 100 lines, and redundancy has been minimized, which is expected to improve readability.

3. Performance
No significant performance changes were observed in the happy case before and after the improvement.
However, improvements in state transition deficiencies are expected to enhance performance in edge cases.

```ruby
require 'socket'
require 'benchmark'

Benchmark.bmbm do |x|
  x.report('fast_fallback: true') do
    30.times { Socket.tcp("www.ruby-lang.org", 80) }
  end

  x.report('fast_fallback: false') do # Ruby3.3時点と同じ
    30.times { Socket.tcp("www.ruby-lang.org", 80, fast_fallback: false) }
  end
end
```

Before:

```
~/s/build ❯❯❯ ../install/bin/ruby ../ruby/test.rb

                           user     system      total        real
fast_fallback: true    0.021315   0.040723   0.062038 (  0.504866)
fast_fallback: false   0.007553   0.026248   0.033801 (  0.533211)
```

After:

```
~/s/build ❯❯❯ ../install/bin/ruby ../ruby/test.rb

                           user     system      total        real
fast_fallback: true    0.023081   0.040525   0.063606 (  0.406219)
fast_fallback: false   0.007302   0.025515   0.032817 (  0.418680)
```
2024-07-30 12:58:31 +09:00
..
lib Improve Socket.tcp (#11187) 2024-07-30 12:58:31 +09:00
.document
addrinfo.h Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
ancdata.c BasicSocket#recv* return nil rather than an empty packet 2023-08-30 10:07:18 +02:00
basicsocket.c Try nil as default for 'default timeout'. (#6509) 2022-10-08 14:02:34 +13:00
constants.c Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
depend ruby tool/update-deps --fix 2024-04-27 21:55:28 +09:00
extconf.rb Revert "Set AI_ADDRCONFIG when making getaddrinfo(3) calls for outgoing conns" 2024-02-01 11:09:54 +11:00
getaddrinfo.c Don't check for null pointer in calls to free 2023-06-30 09:13:31 -04:00
getnameinfo.c Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
ifaddr.c Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
init.c Stop using rb_str_locktmp_ensure publicly 2024-02-23 14:08:29 -05:00
ipsocket.c Revert "Set AI_ADDRCONFIG when making getaddrinfo(3) calls for outgoing conns" 2024-02-01 11:09:54 +11:00
mkconstants.rb Drop support for old ERB 2024-03-03 00:55:45 +09:00
option.c Add support for sockaddr_un on Windows. (#6513) 2022-11-17 14:50:25 -08:00
raddrinfo.c Fix dangling else 2024-06-23 09:42:25 +09:00
rubysocket.h Revise 9ec342e07d 2024-02-26 13:12:05 +09:00
socket.c Rename rsock_raise_socket_error to rsock_raise_resolution_error 2023-11-30 13:27:19 +09:00
sockport.h
sockssocket.c Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
tcpserver.c Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
tcpsocket.c Expand tabs [ci skip] 2022-07-21 09:42:04 -07:00
udpsocket.c Partially revert "Set AI_ADDRCONFIG when making getaddrinfo(3) calls" 2023-12-12 20:05:21 +11:00
unixserver.c Add support for sockaddr_un on Windows. (#6513) 2022-11-17 14:50:25 -08:00
unixsocket.c fix typo in comments / autodoc 2023-01-16 08:00:03 +09:00