Skip to content

Commit e62c312

Browse files
refactoring class-hash to be ractor-safe
mutable constants can't be shared across ractors; this changes that design to define the required variables as constants on the Resource class, which makes them reachable using ractors; the ClassHash is kept in order not to break integrations relying on its existence, but under the hood it's doing the same thing
1 parent 83b2518 commit e62c312

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

lib/resolv.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,14 @@ class Resource < Query
16991699

17001700
attr_reader :ttl
17011701

1702-
ClassHash = {} # :nodoc:
1702+
ClassHash = Module.new do
1703+
module_function
1704+
1705+
def []=(type_class_value, klass)
1706+
type_value, class_value = type_class_value
1707+
Resource.const_set(:"Type#{type_value}_Class#{class_value}", klass)
1708+
end
1709+
end
17031710

17041711
def encode_rdata(msg) # :nodoc:
17051712
raise NotImplementedError.new
@@ -1737,8 +1744,10 @@ def hash # :nodoc:
17371744
end
17381745

17391746
def self.get_class(type_value, class_value) # :nodoc:
1740-
return ClassHash[[type_value, class_value]] ||
1741-
Generic.create(type_value, class_value)
1747+
cached = const_defined?(:"Type#{type_value}_Class#{class_value}") &&
1748+
const_get(:"Type#{type_value}_Class#{class_value}")
1749+
1750+
return cached || Generic.create(type_value, class_value)
17421751
end
17431752

17441753
##
@@ -2907,4 +2916,3 @@ def DefaultResolver.replace_resolvers new_resolvers
29072916
AddressRegex = /(?:#{IPv4::Regex})|(?:#{IPv6::Regex})/
29082917

29092918
end
2910-

0 commit comments

Comments
 (0)