class Net::HTTP::ConnectionPool
@private
def _clean
def _clean now = Time.now @pool.delete_if do |idle_session| if idle_session.last_used_at.nil? or now - idle_session.last_used_at > idle_timeout then idle_session.finish true end end end
def clean!
Removes stale http sessions from the pool (that have exceeded
def clean! @pool_mutex.synchronize { _clean } end
def connection_for host, options = {}, &block
-
(Connection)
-
Other tags:
- Yieldparam: connection -
Other tags:
- Yield: -
Options Hash:
(**options)
-
:proxy_password
(String
) -- -
:proxy_user
(String
) -- -
:proxy_port
(String
) -- -
:proxy_address
(String
) -- -
:proxy_uri
(URI::HTTP, String
) -- A URI string or -
:ssl_ca_path
(String
) -- Full path of the directory that -
:ssl_ca_file
(String
) -- Full path to the SSL certificate -
:ssl_verify_peer
(Boolean
) -- If true, ssl -
:ssl
(Boolean
) -- If the connection should be made over -
:port
(Integer
) -- Which port the connection should use.
Parameters:
-
options
(Hash
) -- -
host
(String
) --
def connection_for host, options = {}, &block connection = Connection.new(self, host, options) yield(connection) if block_given? connection end
def empty!
If empty! is called while there are outstanding requests they may
Closes and removes removes all sessions from the pool.
def empty! @pool_mutex.synchronize do @pool.each(&:finish) @pool = [] end end
def initialize options = {}
(**options)
-
:logger
(Logger
) -- Where debug out -
:http_wire_trace
(Boolean
) -- When +true+, HTTP -
:http_open_timeout
(Numeric
) -- The number of seconds to -
:http_idle_timeout
(Numeric
) -- The number of seconds a
Parameters:
-
options
(Hash
) --
def initialize options = {} @pool = [] @pool_mutex = Mutex.new @open_timeout = options[:http_open_timeout] || 15 @idle_timeout = options[:http_idle_timeout] || 60 @log_wire_trace = !!options[:http_wire_trace] @logger = options[:logger] || Logger.new($stdout) end
def request connection, *request_args, &block
- Private: -
def request connection, *request_args, &block session = nil response = nil retried = false begin session = session_for(connection, retried) session.http_session.read_timeout = connection.read_timeout response = session.request(*request_args, &block) rescue Exception => error # close the http session to prevent the connection from being # left open and risk the other side sending data session.finish if session # retry socket errors once on a new session if SOCKET_ERRORS.include?(error.class) and !retried retried = true retry end raise error else @pool_mutex.synchronize { @pool << session } end response end
def session_for connection, force_new = false
def session_for connection, force_new = false session = nil unless force_new @pool_mutex.synchronize do _clean session = @pool.find{|idle_session| idle_session.key == connection.key } @pool.delete(session) if session end end if session.nil? logger = log_wire_trace? ? self.logger : nil session = Session.for(connection, open_timeout, logger) end session end
def size
Returns the number of sessions currently in the pool, not counting those
def size @pool_mutex.synchronize { @pool.size } end