summaryrefslogtreecommitdiff
path: root/lib/uri/common.rb
diff options
context:
space:
mode:
authorkvokka <[email protected]>2021-05-29 11:29:39 +0100
committerHiroshi SHIBATA <[email protected]>2021-07-27 16:54:26 +0900
commita288c21a5d46418e75c0f03eb12ff0782e51568d (patch)
treee45873ce85b2ff7c85dadec5a137bcd96f1d88f9 /lib/uri/common.rb
parent82191da2a28c8da0f1049ca6d814c9da992c39a1 (diff)
[ruby/uri] Fix to support Ruby 3.0 Ractor
https://github.com/ruby/uri/commit/1faa4fdc16
Diffstat (limited to 'lib/uri/common.rb')
-rw-r--r--lib/uri/common.rb32
1 files changed, 25 insertions, 7 deletions
diff --git a/lib/uri/common.rb b/lib/uri/common.rb
index 915c0e9519..ab3234cb37 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,30 @@ module URI
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
+ SCHEME_LIST_MUTEX.synchronize do
+ remove_const(:SCHEMES) if defined?(SCHEMES)
+ end
+
+ scheme_list
end
#
@@ -73,11 +95,7 @@ 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
- end
+ uri_class = scheme_list[scheme.to_s.upcase] || default
return uri_class.new(scheme, *arguments)
end