Class: Net::HTTP::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/net/http/connection_pool.rb,
lib/net/http/connection_pool/connection.rb,
lib/net/http/connection_pool/session.rb

Overview

A wrapper around Net::HTTP that provides a manged pool of persistant HTTP connections.

pool = Net::HTTP::ConnectionPool.new
connection = pool.connection_for('domain.com')
connection.request(Net::HTTP::Get.new('/')) do |resp|
  # Connection#request yields Net::HTTPResponse objects
  puts resp.body
end

Defined Under Namespace

Classes: Connection, Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :http_idle_timeout (Numeric) — default: 60

    The number of seconds a connection is allowed to sit idle before it is closed and removed from the pool.

  • :http_open_timeout (Numeric) — default: 15

    The number of seconds to wait when opening a http session before raising a timeout exception.

  • :http_wire_trace (Boolean) — default: false

    When true, HTTP debug output will be sent to the :logger.

  • :logger (Logger) — default: Logger.new($stdout)

    Where debug out is sent (wire traces).



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/net/http/connection_pool.rb', line 47

def initialize options = {}
  @pool_mutex = Mutex.new
  @pool = []
  @open_timeout = options[:http_open_timeout] || 15
  @idle_timeout = options[:http_idle_timeout] || 60
  @http_wire_trace = !!options[:http_wire_trace]
  if logger = options[:logger]
    @logger = logger
  elsif http_wire_trace?
    @logger = Logger.new($stdout)
  end
end

Instance Attribute Details

#http_wire_traceBoolean (readonly) Also known as: http_wire_trace?, log_wire_trace?, log_wire_trace

Returns true when HTTP debug output (wire traces) will be logged.

Returns:

  • (Boolean)

    Returns true when HTTP debug output (wire traces) will be logged.



68
69
70
# File 'lib/net/http/connection_pool.rb', line 68

def http_wire_trace
  @http_wire_trace
end

#idle_timeoutInteger (readonly)

Returns:

  • (Integer)


61
62
63
# File 'lib/net/http/connection_pool.rb', line 61

def idle_timeout
  @idle_timeout
end

#loggerLogger (readonly)

Returns Where debug output is sent.

Returns:

  • (Logger)

    Where debug output is sent.



75
76
77
# File 'lib/net/http/connection_pool.rb', line 75

def logger
  @logger
end

#open_timeoutInteger

Returns:

  • (Integer)


64
65
66
# File 'lib/net/http/connection_pool.rb', line 64

def open_timeout
  @open_timeout
end

Instance Method Details

#clean!Object

Removes stale http sessions from the pool (that have exceeded the idle timeout).



152
153
154
# File 'lib/net/http/connection_pool.rb', line 152

def clean!
  @pool_mutex.synchronize { _clean }
end

#connection_for(host, options = {}) {|connection| ... } ⇒ Connection

Requests a connection object from the connection pool.

connection = pool.connection_for('domain.com')
connection.request(Net::HTTP::Get.new('/index.html')) {|resp|}
connection.request(Net::HTTP::Get.new('/about.html')) {|resp|}

# same thing in block form
pool.connection_for('domain.com') do |connection|
  connection.request(Net::HTTP::Get.new('/index.html')) {|resp|}
  connection.request(Net::HTTP::Get.new('/about.html')) {|resp|}
end

Because the pool manages HTTP sessions you do not have to worry about closing a connection or returning a connection to the pool.

Parameters:

  • host (String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :port (Integer)

    Which port the connection should use. Defaults to 80, unless :ssl is true, then it defaults to 443.

  • :ssl (Boolean)

    If the connection should be made over SSL. Defaults to false, unless :port is 443, then it defaults to true.

  • :ssl_verify_peer (Boolean) — default: true

    If true, ssl connections should verify peer certificates. This should only ever be set false false for debugging purposes.

  • :ssl_ca_file (String)

    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.

  • :ssl_ca_path (String)

    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.

  • :proxy_uri (URI::HTTP, String) — default: nil

    A URI string or URI::HTTP object to use as a proxy. You should not provide :proxy_uri with any other proxy options.

    :proxy_uri => 'http://user:[email protected]:80'
    
  • :proxy_address (String)
  • :proxy_port (String)
  • :proxy_user (String)
  • :proxy_password (String)

Yields:

  • (connection)

Yield Parameters:

Returns:



138
139
140
141
142
# File 'lib/net/http/connection_pool.rb', line 138

def connection_for host, options = {}, &block
  connection = Connection.new(self, host, options)
  yield(connection) if block_given?
  connection
end

#empty!Object

Closes 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.



159
160
161
162
163
164
# File 'lib/net/http/connection_pool.rb', line 159

def empty!
  @pool_mutex.synchronize do
    @pool.each(&:finish)
    @pool = []
  end
end

#request(connection, *args, &block) ⇒ nil

Makes a single HTTP request. See Net::HTTP::ConnectionPool::Connection#request for more information on making an HTTP request.

Returns:

  • (nil)


170
171
172
173
174
175
# File 'lib/net/http/connection_pool.rb', line 170

def request connection, *args, &block
  session_for(connection) do |session|
    session.read_timeout = connection.read_timeout
    session.request(*args, &block)
  end
end

#sizeObject

Returns the number of sessions currently in the pool, not counting those currently in use.



146
147
148
# File 'lib/net/http/connection_pool.rb', line 146

def size
  @pool_mutex.synchronize { @pool.size }
end