Skip to content

Commit 639c01d

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 afb57f4 commit 639c01d

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

lib/resolv.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2125,7 +2125,14 @@ class Resource < Query
21252125

21262126
attr_reader :ttl
21272127

2128-
ClassHash = {} # :nodoc:
2128+
ClassHash = Module.new do
2129+
module_function
2130+
2131+
def []=(type_class_value, klass)
2132+
type_value, class_value = type_class_value
2133+
Resource.const_set(:"Type#{type_value}_Class#{class_value}", klass)
2134+
end
2135+
end
21292136

21302137
def encode_rdata(msg) # :nodoc:
21312138
raise NotImplementedError.new
@@ -2163,7 +2170,9 @@ def hash # :nodoc:
21632170
end
21642171

21652172
def self.get_class(type_value, class_value) # :nodoc:
2166-
return ClassHash[[type_value, class_value]] ||
2173+
cache = :"Type#{type_value}_Class#{class_value}"
2174+
2175+
return (const_defined?(cache) && const_get(cache)) ||
21672176
Generic.create(type_value, class_value)
21682177
end
21692178

0 commit comments

Comments
 (0)