diff options
author | Benoit Daloze <[email protected]> | 2021-06-25 13:38:01 +0200 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2021-07-27 16:54:27 +0900 |
commit | 1cf111774f03c6d1ddba735cb8cc79483f16f699 (patch) | |
tree | b90249839fb24c0d350544eda50666e95c5d968b /lib/uri/common.rb | |
parent | 090d799c2496f4c0e1f25c9970f4015fc693ff0e (diff) |
[ruby/uri] Add proper Ractor support to URI
* Using a module to map scheme name to scheme class, which also works with Ractor.
* No constant redefinition, no ObjectSpace, still fast lookup for initial schemes.
https://github.com/ruby/uri/commit/883567fd81
Diffstat (limited to 'lib/uri/common.rb')
-rw-r--r-- | lib/uri/common.rb | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/uri/common.rb b/lib/uri/common.rb index 915c0e9519..2df0536215 100644 --- a/lib/uri/common.rb +++ b/lib/uri/common.rb @@ -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,10 +64,19 @@ module URI include REGEXP - @@schemes = {} + module Schemes + end + private_constant :Schemes + + def self.register_scheme(scheme, klass) + Schemes.const_set(scheme, klass) + end + # Returns a Hash of the defined schemes. def self.scheme_list - @@schemes + Schemes.constants.map { |name| + [name.to_s.upcase, Schemes.const_get(name)] + }.to_h end # @@ -73,11 +84,13 @@ module URI # from +URI.scheme_list+. # def self.for(scheme, *arguments, default: Generic) - if scheme - uri_class = @@schemes[scheme.upcase] || default - else - uri_class = default + const_name = scheme.to_s.upcase + + uri_class = INITIAL_SCHEMES[const_name] + if !uri_class && !const_name.empty? && Schemes.const_defined?(const_name, false) + uri_class = Schemes.const_get(const_name, false) end + uri_class ||= default return uri_class.new(scheme, *arguments) end @@ -653,6 +666,7 @@ module URI "utf-16"=>"utf-16le", "utf-16le"=>"utf-16le", } # :nodoc: + Ractor.make_shareable(WEB_ENCODINGS_) if defined?(Ractor) # :nodoc: # return encoding or nil |