Class: Net::HTTP::ConnectionPool
- Inherits:
-
Object
- Object
- Net::HTTP::ConnectionPool
- 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
-
#clean! ⇒ Object
Removes stale http sessions from the pool (that have exceeded the idle timeout).
-
#connection_for(host, options = {}) {|connection| ... } ⇒ nil
Requests a http session from the connection pool.
-
#empty! ⇒ Object
Closes and removes removes all sessions from the pool.
-
#initialize(options = {}) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #request(connection, *request_args, &block) ⇒ Object
-
#size ⇒ Object
Returns the number of sessions currently in the pool, not counting those currently in use.
Constructor Details
#initialize(options = {}) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
39 40 41 42 43 44 |
# File 'lib/net/http/connection_pool.rb', line 39 def initialize = {} @pool = [] @pool_mutex = Mutex.new @open_timeout = [:open_timeout] || 15 @idle_timeout = [:idle_timeout] || 60 end |
Instance Attribute Details
#idle_timeout ⇒ Integer (readonly)
47 48 49 |
# File 'lib/net/http/connection_pool.rb', line 47 def idle_timeout @idle_timeout end |
#open_timeout ⇒ 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.
110 111 112 113 114 115 116 117 |
# File 'lib/net/http/connection_pool.rb', line 110 def connection_for host, = {}, &block connection = Connection.new(self, host, ) 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 |
#size ⇒ Object
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 |