Skip to content

Commit

Permalink
Merge pull request #9158 from joshcooper/threadsafe_initialization
Browse files Browse the repository at this point in the history
Make cache and values fully thread-safe
  • Loading branch information
joshcooper committed Nov 21, 2023
2 parents 13ce54d + f890191 commit 4e6aa1f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions lib/puppet/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'forwardable'
require 'fileutils'
require 'concurrent'
require_relative 'concurrent/lock'

# The class for handling configuration files.
class Puppet::Settings
Expand Down Expand Up @@ -146,8 +147,21 @@ def initialize
@configuration_file = nil

# And keep a per-environment cache
@cache = Concurrent::Hash.new { |hash, key| hash[key] = Concurrent::Hash.new }
@values = Concurrent::Hash.new { |hash, key| hash[key] = Concurrent::Hash.new }
# We can't use Concurrent::Map because we want to preserve insertion order
@cache_lock = Puppet::Concurrent::Lock.new
@cache = Concurrent::Hash.new do |hash, key|
@cache_lock.synchronize do
break hash[key] if hash.key?(key)
hash[key] = Concurrent::Hash.new
end
end
@values_lock = Puppet::Concurrent::Lock.new
@values = Concurrent::Hash.new do |hash, key|
@values_lock.synchronize do
break hash[key] if hash.key?(key)
hash[key] = Concurrent::Hash.new
end
end

# The list of sections we've used.
@used = []
Expand Down

0 comments on commit 4e6aa1f

Please sign in to comment.