@private
@return [Integer]
@return [Integer]
@param [Hash] options
@option options [Numeric] :#idle_timeout (60) The number of seconds a
connection is allowed to sit idle before it is closed and removed from the pool.
@option options [Numeric] :#open_timeout (15) The number of seconds to
wait when opening a http session before raising a timeout exception.
# File lib/net/http/connection_pool.rb, line 39 def initialize options = {} @pool = [] @pool_mutex = Mutex.new @open_timeout = options[:open_timeout] || 15 @idle_timeout = options[:idle_timeout] || 60 end
Removes http sessions from the pool that have passed the idle timeout
# File lib/net/http/connection_pool.rb, line 147 def clean! @pool_mutex.synchronize { _clean } end
Requests a http session from the connection pool.
pool.connection_for('domain.com') do |connection| # make connection.request(Net::HTTP::Get.new('/index.html')) connection.request(Net::HTTP::Get.new('/about.html')) end
The yielded connection object is a thin wrapper around the persistent http session object. You generally want to call {Net::HTTP::ConnectionPool::Connection#request} on the yielded object. When the block is complete the connection will be returned to the pool.
@param [String] host
@param [Hash] options
@option options [Integer] :port Which port the connection should use.
Defaults to 80, unless +:ssl+ is +true+, then it defaults to 443.
@option options [Boolean] :ssl If the connection should be made over
SSL. Defaults to +false+, unless +:port+ is 443, then it defaults to +true+.
@option options [Boolean] :ssl_verify_peer (true) If true, ssl
connections will verify peer certificates. This should only ever be set false false for debugging purposes.
@option options [String] :ssl_ca_file Full path to the SSL certificate
authority bundle file that should be used when verifying peer certificates. If you do not pass +:ssl_ca_file+ or +:ssl_ca_path+ the the system default will be used if available.
@option options [String] :ssl_ca_path Full path of the directory that
contains the unbundled SSL certificate authority files for verifying peer certificates. If you do not pass +:ssl_ca_file+ or +:ssl_ca_path+ the the system default will be used if available.
@option options [URI::HTTP,String] :proxy_uri (nil) A URI string or
URI::HTTP for a proxy reqeusts should be made through. You should not pass both +:proxy_uri+ with any of the other proxy options. :proxy_uri => 'http://user:pass@host.com:80'
@option options [String] :proxy_address
@option options [String] :proxy_port
@option options [String] :proxy_user
@option options [String] :proxy_password
@yieldparam [Connection] connection
@return [nil]
# File lib/net/http/connection_pool.rb, line 110 def connection_for host, options = {}, &block connection = Connection.new(self, host, options) if block_given? yield(connection) else connection end end
Finishes and removes removes all sessions from the pool.
If empty! is called while there are outstanding requests they may get checked back into the pool, leaving the pool in a non-empty state.
# File lib/net/http/connection_pool.rb, line 154 def empty! @pool_mutex.synchronize do @pool.each(&:finish) @pool = [] end end
# File lib/net/http/connection_pool.rb, line 119 def request connection, *request_args, &block session = nil response = nil retried = false begin session = session_for(connection) session.http_session.read_timeout = connection.read_timeout response = session.request(*request_args, &block) rescue *SOCKET_ERRORS => error # retry socket errors once unless retried retried = true retry end raise error else @pool_mutex.synchronize { @pool << session } end response end
Returns the number of sessions currently in the pool, not counting those currently in use.
# File lib/net/http/connection_pool.rb, line 142 def size @pool_mutex.synchronize { @pool.size } end