[ruby/resolv] 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

https://github.com/ruby/resolv/commit/639c01dc7f
This commit is contained in:
HoneyryderChuck 2024-10-29 17:58:27 +00:00 committed by git
parent 54a85caed4
commit e3dd766e99

View File

@ -2125,7 +2125,14 @@ class Resolv
attr_reader :ttl
ClassHash = {} # :nodoc:
ClassHash = Module.new do
module_function
def []=(type_class_value, klass)
type_value, class_value = type_class_value
Resource.const_set(:"Type#{type_value}_Class#{class_value}", klass)
end
end
def encode_rdata(msg) # :nodoc:
raise NotImplementedError.new
@ -2163,7 +2170,9 @@ class Resolv
end
def self.get_class(type_value, class_value) # :nodoc:
return ClassHash[[type_value, class_value]] ||
cache = :"Type#{type_value}_Class#{class_value}"
return (const_defined?(cache) && const_get(cache)) ||
Generic.create(type_value, class_value)
end