diff options
| author | Daisuke Aritomo <osyoyu@osyoyu.com> | 2025-07-17 00:09:47 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-11-10 02:40:46 +0000 |
| commit | f710e6bb54a1e2cfe808222bc8d70d8f68ab5dc9 (patch) | |
| tree | e265246cc436a3cc54d04f7ae693df7a211f24e1 /include/ruby/ractor.h | |
| parent | 8fa29a75abf265c20cdeb9779bf25c1b3eafcf26 (diff) | |
[ruby/net-http] Replace Timeout.timeout with TCPSocket.open(open_timeout:) when available
This patch replaces the implementation of #open_timeout from Timeout.timeout from the builtin timeout in TCPSocket.open, which was introduced in Ruby 3.5 (https://bugs.ruby-lang.org/issues/21347).
The builtin timeout in TCPSocket.open is better in several ways than Timeout.timeout. It does not rely on a separate Ruby Thread for monitoring Timeout (which is what the timeout library internally does).
Furthermore, it is compatible with Ractors, as opposed to Timeout.timeout (it internally uses Thread::Mutex which can not be used in non-main Ractors).
This change allows the following code to work.
require 'net/http'
Ractor.new {
uri = URI('http://example.com/')
http = Net::HTTP.new(uri.host, uri.port)
http.open_timeout = 1
http.get(uri.path)
}.value
In Ruby <3.5 environments where `TCPSocket.open` does not have the `open_timeout` option, I have kept the behavior unchanged. net/http will use `Timeout.timeout { TCPSocket.open }`.
https://github.com/ruby/net-http/commit/728eb8fc42
Diffstat (limited to 'include/ruby/ractor.h')
0 files changed, 0 insertions, 0 deletions
