Skip to content

Commit

Permalink
refactoring class-hash to be ractor-safe
Browse files Browse the repository at this point in the history
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
  • Loading branch information
HoneyryderChuck committed Oct 29, 2024
1 parent 83b2518 commit e62c312
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/resolv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,14 @@ class Resource < Query

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
Expand Down Expand Up @@ -1737,8 +1744,10 @@ def hash # :nodoc:
end

def self.get_class(type_value, class_value) # :nodoc:
return ClassHash[[type_value, class_value]] ||
Generic.create(type_value, class_value)
cached = const_defined?(:"Type#{type_value}_Class#{class_value}") &&
const_get(:"Type#{type_value}_Class#{class_value}")

return cached || Generic.create(type_value, class_value)
end

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

end

0 comments on commit e62c312

Please sign in to comment.