-
Notifications
You must be signed in to change notification settings - Fork 47
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
Fix to support Ruby 3.0 Ractor #22
Merged
Merged
Changes from all 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 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
This file contains 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 |
---|---|---|
|
@@ -16,6 +16,7 @@ module URI | |
REGEXP = RFC2396_REGEXP | ||
Parser = RFC2396_Parser | ||
RFC3986_PARSER = RFC3986_Parser.new | ||
Ractor.make_shareable(RFC3986_PARSER) if defined?(Ractor) | ||
|
||
# URI::Parser.new | ||
DEFAULT_PARSER = Parser.new | ||
|
@@ -27,6 +28,7 @@ module URI | |
DEFAULT_PARSER.regexp.each_pair do |sym, str| | ||
const_set(sym, str) | ||
end | ||
Ractor.make_shareable(DEFAULT_PARSER) if defined?(Ractor) | ||
|
||
module Util # :nodoc: | ||
def make_components_hash(klass, array_hash) | ||
|
@@ -62,22 +64,38 @@ def make_components_hash(klass, array_hash) | |
|
||
include REGEXP | ||
|
||
@@schemes = {} | ||
SCHEME_LIST_MUTEX = Mutex.new | ||
private_constant :SCHEME_LIST_MUTEX | ||
|
||
# Returns a Hash of the defined schemes. | ||
# The list is lazily calculated. | ||
def self.scheme_list | ||
@@schemes | ||
return const_get(:SCHEMES) if defined?(SCHEMES) | ||
|
||
SCHEME_LIST_MUTEX.synchronize do | ||
const_set(:SCHEMES, ObjectSpace. | ||
each_object(Class). | ||
select { |klass| klass < URI::Generic }. | ||
each_with_object({}) { |klass, acc| acc[klass.name.split('::').last.upcase] = klass }. | ||
freeze) | ||
end | ||
end | ||
|
||
# Re-calculate scheme list | ||
def self.refresh_scheme_list | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is yet another hack, now the API exposes the caching to the end user, that's leaking implementation details. |
||
SCHEME_LIST_MUTEX.synchronize do | ||
remove_const(:SCHEMES) if defined?(SCHEMES) | ||
end | ||
|
||
scheme_list | ||
end | ||
|
||
# | ||
# Construct a URI instance, using the scheme to detect the appropriate class | ||
# from +URI.scheme_list+. | ||
# | ||
def self.for(scheme, *arguments, default: Generic) | ||
if scheme | ||
uri_class = @@schemes[scheme.upcase] || default | ||
else | ||
uri_class = default | ||
end | ||
uri_class = scheme_list[scheme.to_s.upcase] || default | ||
|
||
return uri_class.new(scheme, *arguments) | ||
end | ||
|
This file contains 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 |
---|---|---|
|
@@ -89,6 +89,4 @@ def set_user(v) | |
def set_password(v) | ||
end | ||
end | ||
|
||
@@schemes['FILE'] = File | ||
end |
This file contains 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 |
---|---|---|
|
@@ -262,5 +262,4 @@ def to_s | |
return str | ||
end | ||
end | ||
@@schemes['FTP'] = FTP | ||
end |
This file contains 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 |
---|---|---|
|
@@ -81,7 +81,4 @@ def request_uri | |
url.start_with?(?/.freeze) ? url : ?/ + url | ||
end | ||
end | ||
|
||
@@schemes['HTTP'] = HTTP | ||
|
||
end |
This file contains 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 |
---|---|---|
|
@@ -18,5 +18,4 @@ class HTTPS < HTTP | |
# A Default port of 443 for URI::HTTPS | ||
DEFAULT_PORT = 443 | ||
end | ||
@@schemes['HTTPS'] = HTTPS | ||
end |
This file contains 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 |
---|---|---|
|
@@ -256,6 +256,4 @@ def hierarchical? | |
false | ||
end | ||
end | ||
|
||
@@schemes['LDAP'] = LDAP | ||
end |
This file contains 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 |
---|---|---|
|
@@ -17,5 +17,4 @@ class LDAPS < LDAP | |
# A Default port of 636 for URI::LDAPS | ||
DEFAULT_PORT = 636 | ||
end | ||
@@schemes['LDAPS'] = LDAPS | ||
end |
This file contains 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 |
---|---|---|
|
@@ -288,6 +288,4 @@ def to_mailtext | |
end | ||
alias to_rfc822text to_mailtext | ||
end | ||
|
||
@@schemes['MAILTO'] = MailTo | ||
end |
This file contains 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 |
---|---|---|
|
@@ -78,7 +78,4 @@ def request_uri | |
url.start_with?(?/.freeze) ? url : ?/ + url | ||
end | ||
end | ||
|
||
@@schemes['WS'] = WS | ||
|
||
end |
This file contains 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 |
---|---|---|
|
@@ -18,5 +18,4 @@ class WSS < WS | |
# A Default port of 443 for URI::WSS | ||
DEFAULT_PORT = 443 | ||
end | ||
@@schemes['WSS'] = WSS | ||
end |
This file contains 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
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.
This seems very expensive, iterate the whole heap when previously it was just registered directly.
Could the
Class#inherited
hook be used instead at least?Or maybe use
Class#descendants
(checking if available withrespond_to?
) from https://bugs.ruby-lang.org/issues/14394?In #15 I think the way was clearly to use instance variables for this, and that has been accepted in https://bugs.ruby-lang.org/issues/17592. And the main Ractor could simply write to that instance variable. That has not been implemented yet though (cc @ko1).
Another strategy might be to haves
SCHEMES = {}
, and then once the first URI is created,Ractor.make_shareable(SCHEMES) unless SCHEMES.frozen?
. That would prevent adding new schemes after the first URI is created, but maybe it's good enough, and it feels like it fits well with the Ractor model.Adding support for Ractor by making everything slower/more hacky is IMHO absolutely not acceptable.