Class: Puppet::Network::HTTP::Pool Private
- Defined in:
- lib/puppet/network/http/pool.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A pool for persistent Net::HTTP
connections. Connections are stored in the pool indexed by their Site. Connections are borrowed from the pool, yielded to the caller, and released back into the pool. If a connection is expired, it will be closed either when a connection to that site is requested, or when the pool is closed. The pool can store multiple connections to the same site, and will be reused in MRU order.
Instance Attribute Summary collapse
- #factory ⇒ Object readonly private
- #keepalive_timeout ⇒ Object readonly private
Instance Method Summary collapse
-
#active_sessions(site) ⇒ Object
private
Returns an Array of sessions whose connections are not expired.
-
#borrow(site, verifier) ⇒ Object
private
Borrow and take ownership of a persistent connection.
- #close ⇒ Object private
-
#close_connection(site, http) ⇒ Object
private
Safely close a persistent connection.
-
#initialize(keepalive_timeout) ⇒ Pool
constructor
private
A new instance of Pool.
- #pool ⇒ Object private
-
#release(site, verifier, http) ⇒ Object
private
Release a connection back into the pool.
-
#setsockopts(netio) ⇒ Object
private
Set useful socket option(s) which lack from default settings in Net:HTTP.
- #with_connection(site, verifier, &block) ⇒ Object private
Methods inherited from BasePool
Constructor Details
#initialize(keepalive_timeout) ⇒ Pool
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Pool.
14 15 16 17 18 |
# File 'lib/puppet/network/http/pool.rb', line 14 def initialize(keepalive_timeout) @pool = {} @factory = Puppet::Network::HTTP::Factory.new @keepalive_timeout = keepalive_timeout end |
Instance Attribute Details
#factory ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
12 13 14 |
# File 'lib/puppet/network/http/pool.rb', line 12 def factory @factory end |
#keepalive_timeout ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
12 13 14 |
# File 'lib/puppet/network/http/pool.rb', line 12 def keepalive_timeout @keepalive_timeout end |
Instance Method Details
#active_sessions(site) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns an Array of sessions whose connections are not expired.
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/puppet/network/http/pool.rb', line 124 def active_sessions(site) now = Time.now sessions = @pool[site] || [] sessions.select do |session| if session.expired?(now) close_connection(site, session.connection) false else true end end end |
#borrow(site, verifier) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Borrow and take ownership of a persistent connection. If a new connection is created, it will be started prior to being returned.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/puppet/network/http/pool.rb', line 74 def borrow(site, verifier) @pool[site] = active_sessions(site) index = @pool[site].index do |session| (verifier.nil? && session.verifier.nil?) || (!verifier.nil? && verifier.reusable?(session.verifier)) end session = index ? @pool[site].delete_at(index) : nil if session @pool.delete(site) if @pool[site].empty? Puppet.debug("Using cached connection for #{site}") session.connection else http = @factory.create_connection(site) start(site, verifier, http) setsockopts(http.instance_variable_get(:@socket)) http end end |
#close ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
42 43 44 45 46 47 48 49 |
# File 'lib/puppet/network/http/pool.rb', line 42 def close @pool.each_pair do |site, sessions| sessions.each do |session| close_connection(site, session.connection) end end @pool.clear end |
#close_connection(site, http) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Safely close a persistent connection. Don’t try to close a connection that’s already closed.
60 61 62 63 64 65 66 67 68 |
# File 'lib/puppet/network/http/pool.rb', line 60 def close_connection(site, http) return false unless http.started? Puppet.debug("Closing connection for #{site}") http.finish true rescue => detail Puppet.log_exception(detail, _("Failed to close connection for %{site}: %{detail}") % { site: site, detail: detail }) nil end |
#pool ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
52 53 54 |
# File 'lib/puppet/network/http/pool.rb', line 52 def pool @pool end |
#release(site, verifier, http) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Release a connection back into the pool.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/puppet/network/http/pool.rb', line 108 def release(site, verifier, http) expiration = Time.now + @keepalive_timeout session = Puppet::Network::HTTP::Session.new(http, verifier, expiration) Puppet.debug("Caching connection for #{site}") sessions = @pool[site] if sessions sessions.unshift(session) else @pool[site] = [session] end end |
#setsockopts(netio) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Set useful socket option(s) which lack from default settings in Net:HTTP
98 99 100 101 102 103 |
# File 'lib/puppet/network/http/pool.rb', line 98 def setsockopts(netio) return unless netio socket = netio.io socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) end |
#with_connection(site, verifier, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/puppet/network/http/pool.rb', line 20 def with_connection(site, verifier, &block) reuse = true http = borrow(site, verifier) begin if http.use_ssl? && http.verify_mode != OpenSSL::SSL::VERIFY_PEER reuse = false end yield http rescue => detail reuse = false raise detail ensure if reuse && http.started? release(site, verifier, http) else close_connection(site, http) end end end |