diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2016-08-09 11:08:34 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2016-08-09 11:08:34 -0700 |
commit | 27cfcdb4cc6fdd7a52e3edb570297709f2bfe715 (patch) | |
tree | ee5893f2f0b04ed2615953d1ced9567e361f09a2 | |
parent | c5cf649b253ffb35684576e207f7c52b205f6360 (diff) | |
download | chef-27cfcdb4cc6fdd7a52e3edb570297709f2bfe715.tar.gz |
make keepalives entirely optional
turn on only for the cookbook synchronizer
-rw-r--r-- | lib/chef/cookbook/synchronizer.rb | 2 | ||||
-rw-r--r-- | lib/chef/http.rb | 53 | ||||
-rw-r--r-- | lib/chef/http/basic_client.rb | 8 |
3 files changed, 40 insertions, 23 deletions
diff --git a/lib/chef/cookbook/synchronizer.rb b/lib/chef/cookbook/synchronizer.rb index 60be422c66..01f6155bf3 100644 --- a/lib/chef/cookbook/synchronizer.rb +++ b/lib/chef/cookbook/synchronizer.rb @@ -291,7 +291,7 @@ class Chef end def server_api - Thread.current[:server_api] ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url]) + Thread.current[:server_api] ||= Chef::ServerAPI.new(Chef::Config[:chef_server_url], keepalives: true) end end diff --git a/lib/chef/http.rb b/lib/chef/http.rb index 0406a7a975..d7150a50dd 100644 --- a/lib/chef/http.rb +++ b/lib/chef/http.rb @@ -77,6 +77,9 @@ class Chef attr_reader :middlewares + # [Boolean] if we're doing keepalives or not + attr_reader :keepalives + # Create a HTTP client object. The supplied +url+ is used as the base for # all subsequent requests. For example, when initialized with a base url # http://localhost:4000, a call to +get+ with 'nodes' will make an @@ -87,6 +90,7 @@ class Chef @sign_on_redirect = true @redirects_followed = 0 @redirect_limit = 10 + @keepalives = options[:keepalives] || false @options = options @middlewares = [] @@ -227,30 +231,37 @@ class Chef end def http_client(base_url = nil) - # the per-host per-port cache here gets peristent connections correct in the - # face of redirects to different servers - @http_client ||= {} - @http_client[base_url.host] ||= {} - @http_client[base_url.host][base_url.port] ||= - begin - base_url ||= url - if chef_zero_uri?(base_url) - # PERFORMANCE CRITICAL: *MUST* lazy require here otherwise we load up webrick - # via chef-zero and that hits DNS (at *require* time) which may timeout, - # when for most knife/chef-client work we never need/want this loaded. - - unless defined?(SocketlessChefZeroClient) - require "chef/http/socketless_chef_zero_client" - end + base_url ||= url + client = build_http_client(base_url) + if keepalives && !base_url.nil? + # only reuse the http_client if we want keepalives and have a base_url + @http_client ||= {} + # the per-host per-port cache here gets peristent connections correct when + # redirecting to different servers + @http_client[base_url.host] ||= {} + @http_client[base_url.host][base_url.port] ||= client + else + client + end + end - SocketlessChefZeroClient.new(base_url) - else - BasicClient.new(base_url, :ssl_policy => Chef::HTTP::APISSLPolicy) - end + private + + def build_http_client(base_url) + if chef_zero_uri?(base_url) + # PERFORMANCE CRITICAL: *MUST* lazy require here otherwise we load up webrick + # via chef-zero and that hits DNS (at *require* time) which may timeout, + # when for most knife/chef-client work we never need/want this loaded. + + unless defined?(SocketlessChefZeroClient) + require "chef/http/socketless_chef_zero_client" end - end - protected + SocketlessChefZeroClient.new(base_url) + else + BasicClient.new(base_url, ssl_policy: Chef::HTTP::APISSLPolicy, keepalives: keepalives) + end + end def create_url(path) return path if path.is_a?(URI) diff --git a/lib/chef/http/basic_client.rb b/lib/chef/http/basic_client.rb index 38386bddbb..921a28cad0 100644 --- a/lib/chef/http/basic_client.rb +++ b/lib/chef/http/basic_client.rb @@ -34,6 +34,7 @@ class Chef attr_reader :url attr_reader :http_client attr_reader :ssl_policy + attr_reader :keepalives # Instantiate a BasicClient. # === Arguments: @@ -43,6 +44,7 @@ class Chef def initialize(url, opts = {}) @url = url @ssl_policy = opts[:ssl_policy] || DefaultSSLPolicy + @keepalives = opts[:keepalives] || false @http_client = build_http_client end @@ -114,7 +116,11 @@ class Chef http_client.read_timeout = config[:rest_timeout] http_client.open_timeout = config[:rest_timeout] - http_client.start + if keepalives + http_client.start + else + http_client + end end def config |