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

Defined Under Namespace

Classes: Connection, Session

Constant Summary collapse

SOCKET_ERRORS =
[
  EOFError, 
  IOError, 
  Errno::ECONNABORTED, 
  Errno::ECONNRESET, 
  Errno::EPIPE, 
  Errno::EINVAL 
]

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):

  • :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.

  • :open_timeout (Numeric) — default: 15

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


39
40
41
42
43
44
# 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

Instance Attribute Details

#idle_timeoutInteger (readonly)

Returns:

  • (Integer)

47
48
49
# File 'lib/net/http/connection_pool.rb', line 47

def idle_timeout
  @idle_timeout
end

#open_timeoutInteger

Returns:

  • (Integer)

50
51
52
# File 'lib/net/http/connection_pool.rb', line 50

def open_timeout
  @open_timeout
end

Instance Method Details

#clean!Object

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


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

def clean!
  @pool_mutex.synchronize { _clean }
end

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

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.

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 will 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 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:[email protected]:80'
    
  • :proxy_address (String)
  • :proxy_port (String)
  • :proxy_user (String)
  • :proxy_password (String)

Yield Parameters:

Returns:

  • (nil)

110
111
112
113
114
115
116
117
# 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

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


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

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

#request(connection, *request_args, &block) ⇒ Object


119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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, 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

#sizeObject

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


155
156
157
# File 'lib/net/http/connection_pool.rb', line 155

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