Adding node caching, so that node sources are not spammed during file serving and such
This commit is contained in:
Родитель
a9539548d9
Коммит
1527f4a615
|
@ -10,6 +10,7 @@ class Puppet::Network::Handler::Node < Puppet::Network::Handler
|
|||
# A simplistic class for managing the node information itself.
|
||||
class SimpleNode
|
||||
attr_accessor :name, :classes, :parameters, :environment, :source, :ipaddress, :names
|
||||
attr_reader :time
|
||||
|
||||
def initialize(name, options = {})
|
||||
@name = name
|
||||
|
@ -31,6 +32,8 @@ class Puppet::Network::Handler::Node < Puppet::Network::Handler
|
|||
@environment = env
|
||||
end
|
||||
end
|
||||
|
||||
@time = Time.now
|
||||
end
|
||||
|
||||
# Merge the node facts with parameters from the node source.
|
||||
|
@ -168,6 +171,9 @@ class Puppet::Network::Handler::Node < Puppet::Network::Handler
|
|||
extend(mod)
|
||||
|
||||
super
|
||||
|
||||
# We cache node info for speed
|
||||
@node_cache = {}
|
||||
end
|
||||
|
||||
# Try to retrieve a given node's parameters.
|
||||
|
@ -181,6 +187,23 @@ class Puppet::Network::Handler::Node < Puppet::Network::Handler
|
|||
|
||||
private
|
||||
|
||||
# Store the node to make things a bit faster.
|
||||
def cache(node)
|
||||
@node_cache[node.name] = node
|
||||
end
|
||||
|
||||
# If the node is cached, return it.
|
||||
def cached?(name)
|
||||
# Don't use cache when the filetimeout is set to 0
|
||||
return false if [0, "0"].include?(Puppet[:filetimeout])
|
||||
|
||||
if node = @node_cache[name] and Time.now - node.time < Puppet[:filetimeout]
|
||||
return node
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
# Create/cache a fact handler.
|
||||
def fact_handler
|
||||
unless defined?(@fact_handler)
|
||||
|
|
|
@ -66,7 +66,7 @@ module NodeTesting
|
|||
end
|
||||
end
|
||||
|
||||
class TestNodeInterface < Test::Unit::TestCase
|
||||
class TestNodeHandler < Test::Unit::TestCase
|
||||
include NodeTesting
|
||||
|
||||
def setup
|
||||
|
@ -318,6 +318,28 @@ class TestNodeInterface < Test::Unit::TestCase
|
|||
end
|
||||
assert_equal(%w{yay foo}, result, "Did not get classes back")
|
||||
end
|
||||
|
||||
# We reuse the filetimeout for the node caching timeout.
|
||||
def test_node_caching
|
||||
handler = Node.new
|
||||
|
||||
node = Object.new
|
||||
node.metaclass.instance_eval do
|
||||
attr_accessor :time, :name
|
||||
end
|
||||
node.time = Time.now
|
||||
node.name = "yay"
|
||||
|
||||
# Make sure caching works normally
|
||||
assert_nothing_raised("Could not cache node") do
|
||||
handler.send(:cache, node)
|
||||
end
|
||||
assert_equal(node.object_id, handler.send(:cached?, "yay").object_id, "Did not get node back from the cache")
|
||||
|
||||
# Now set the node's time to be a long time ago
|
||||
node.time = Time.now - 50000
|
||||
assert(! handler.send(:cached?, "yay"), "Timed-out node was returned from cache")
|
||||
end
|
||||
end
|
||||
|
||||
class TestSimpleNode < Test::Unit::TestCase
|
||||
|
@ -332,6 +354,7 @@ class TestSimpleNode < Test::Unit::TestCase
|
|||
assert_equal("testing", node.name, "Did not set name correctly")
|
||||
assert_equal({}, node.parameters, "Node parameters did not default correctly")
|
||||
assert_equal([], node.classes, "Node classes did not default correctly")
|
||||
assert_instance_of(Time, node.time, "Did not set the creation time")
|
||||
|
||||
# Now test it with values for both
|
||||
params = {"a" => "b"}
|
||||
|
|
Загрузка…
Ссылка в новой задаче