Commit 6ae215dd authored by Ruth Linehan's avatar Ruth Linehan

(PE-18347) Memoize arity call in multi_arity_get method

Memoize the call to get the arity of the `get` method in the http client
class, since this call takes more time than just a regular method call.

Also update to use a case/when block rather than if/else.
parent ad0905ab
......@@ -235,14 +235,15 @@ module Puppet::Util::Puppetdb
# `parameters()` returns the parameters of a method as an array of
# tuples, with a tuple for each parameter, e.g. `[[:req :a], [:opt
# :b]]`. Counting this array gives the arity.
arity = http_instance.class.instance_method(:get).parameters.count
if arity == 2
@@arity ||= http_instance.class.instance_method(:get).parameters.count
case @@arity
when 2
http_instance.get(path, headers)
elsif arity == 3
when 3
http_instance.get(path, headers, options)
raise Puppet::Error,
"Http client `get` method expected to have arity 2 or 3 but was arity #{arity}"
"Http client `get` method expected to have arity 2 or 3 but was arity #{@@arity}"
......@@ -385,6 +385,10 @@ describe Puppet::Util::Puppetdb::Http do
describe "#multi_arity_get" do
before (:each) do
Puppet::Util::Puppetdb::Http.class_variable_set :@@arity, nil
let(:path) { "/path" }
let(:headers) { {"header" => "foo"} }
let(:options) { {"options" => "abc"} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment