-
Notifications
You must be signed in to change notification settings - Fork 79
Replace Timeout.timeout with TCPSocket.open(open_timeout:) when available #224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
osyoyu
wants to merge
3
commits into
ruby:master
Choose a base branch
from
osyoyu:tcpsocket-open-timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+31
−8
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1654,14 +1654,22 @@ def connect | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| debug "opening connection to #{conn_addr}:#{conn_port}..." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s = Timeout.timeout(@open_timeout, Net::OpenTimeout) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rescue => e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise e, "Failed to open TCP connection to " + | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "#{conn_addr}:#{conn_port} (#{e.message})" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s = begin | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Use built-in timeout in TCPSocket.open if available | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port, open_timeout: @open_timeout) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| rescue ArgumentError => e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| raise if !(e.message.include?('unknown keyword: :open_timeout') || e.message.include?('wrong number of arguments (given 5, expected 2..4)')) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Fallback to Timeout.timeout if TCPSocket.open does not support open_timeout | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Timeout.timeout(@open_timeout, Net::OpenTimeout) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| s = begin | |
| # Use built-in timeout in TCPSocket.open if available | |
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port, open_timeout: @open_timeout) | |
| rescue ArgumentError => e | |
| raise if !(e.message.include?('unknown keyword: :open_timeout') || e.message.include?('wrong number of arguments (given 5, expected 2..4)')) | |
| # Fallback to Timeout.timeout if TCPSocket.open does not support open_timeout | |
| Timeout.timeout(@open_timeout, Net::OpenTimeout) { | |
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port) | |
| } | |
| end | |
| s = begin | |
| # Determine once whether TCPSocket.open supports open_timeout keyword argument | |
| case @tcpsocket_open_timeout_supported | |
| when nil | |
| begin | |
| # Try using open_timeout keyword argument | |
| sock = TCPSocket.open(conn_addr, conn_port, @local_host, @local_port, open_timeout: @open_timeout) | |
| @tcpsocket_open_timeout_supported = true | |
| sock | |
| rescue ArgumentError => e | |
| if e.message.include?('unknown keyword: :open_timeout') || e.message.include?('wrong number of arguments (given 5, expected 2..4)') | |
| # TCPSocket.open does not support open_timeout keyword argument | |
| @tcpsocket_open_timeout_supported = false | |
| # Fallback to Timeout.timeout | |
| Timeout.timeout(@open_timeout, Net::OpenTimeout) { | |
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port) | |
| } | |
| else | |
| raise | |
| end | |
| end | |
| when true | |
| # Use open_timeout keyword argument (known to be supported) | |
| # assume Ruby >= 3.5 | |
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port, open_timeout: @open_timeout) | |
| when false | |
| # Use Timeout.timeout (known that open_timeout is not supported) | |
| # assume Ruby < 3.5 | |
| Timeout.timeout(@open_timeout, Net::OpenTimeout) { | |
| TCPSocket.open(conn_addr, conn_port, @local_host, @local_port) | |
| } | |
| end | |
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't come up with a better way to detect the absence of
open_timeout:option. Is this a acceptable solution?TCPSocket.open is basically
(**args)from the perspective of Ruby, so Method#parameters wasn't an option:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's fine, given this is only to keep things working for very old rubies, right, and the error message is not going to change for those rubies anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually it's not only very old Rubies, but even 3.4 raises on a
TCPSocket.opencall withopen_timeout. It is true that the situation is different in Ruby 2.x, where keyword arguments were not a argument of its own kind (workaround in 09bf573).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I thought your question was specifically for the Ruby 2.x workaround, since you need to parse a very generic error message, but I see how the detection for Ruby 3.4 is also brittle since it also involves parsing the error message, even if more specific. Unfortunately, I don't know of a better way, but I'd say this way is acceptable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Considering that every single new HTTP request is going to go through this raise/rescue flow on ruby < 3.5, and that exceptions are kind of expensive, personally I think a better option is just to have test for
RUBY_VERSION.to_f < 3.5directly to see if we use the oldTimeout.timeout. I get that testing version numbers directly is a bit distasteful, but when there's actual performance issues and brittleness issues on the line... I'd say its meritedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've took @eregon's idea. Pushed in 06d982f. (thanks: @niku)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Regarding 06d982f:
Since it's going to affect all instances of this class, can you make it a class variable, i.e.
@@tcpsocket_supports_open_timeout? My code for example makes tons of new instances of this class, and it would be great if they didn't all have to individually figure out that they couldn't use open_timeout.We wouldn't need any synchronization for a
@@tcpsocket_supports_open_timeoutI believe: the worst that would happen is a different thread might read an old value and try to useopen_timeoutagain unnecessarily. Writing a boolean value should be atomic on all Rubies as well since it's going to be just a byte, and can never be in a state where it's partially written... right @eregon?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah Christ. Ractors. Yeah nevermind my suggestion above won't work any more, since it would make it so instances of Net::HTTP wouldn't work inside anything but the main Ractor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that’s exactly right. Using class variables would unfortunately kill Ractors.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case I'd still argue that simply testing
RUBY_VERSION.to_f >= 3.5is the more straightforward and efficient way to do it - to address @eregon's arguments: Regarding alternative Rubies, I'd say its their responsibility to implement Ruby 3.5 functionality before advertising compatibility with it in RUBY_VERSION (unless there's some reason whyopen_timeoutspecifically would be difficult to implemented in Java?). And in the unlikely case thatopen_timeoutis removed in a future Ruby version, well then an update to this library would be necessary, which is to be expected when behavior is deprecated/removed, and isn't something we usually guard against no? Things would get pretty messy if we did that all over the place.Just my two cents, and yeah an exception raised on first use of every Net::HTTP instance probably isn't a performance hit we need to be debating a ton. I'll just be happy either way if this hopefully gets merged, and i appreciate the work!! 🙏