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
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
-
#http_wire_trace ⇒ Boolean
(also: #http_wire_trace?, #log_wire_trace?, #log_wire_trace)
readonly
Returns
true
when HTTP debug output (wire traces) will be logged. - #idle_timeout ⇒ Integer readonly
-
#logger ⇒ Logger
readonly
Where debug output is sent.
- #open_timeout ⇒ Integer
Instance Method Summary collapse
-
#clean! ⇒ Object
Removes stale http sessions from the pool (that have exceeded the idle timeout).
-
#connection_for(host, options = {}) {|connection| ... } ⇒ Connection
Requests a connection object 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, *args, &block) ⇒ nil
Makes a single HTTP request.
-
#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.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/net/http/connection_pool.rb', line 47 def initialize = {} @pool_mutex = Mutex.new @pool = [] @open_timeout = [:http_open_timeout] || 15 @idle_timeout = [:http_idle_timeout] || 60 @http_wire_trace = !![:http_wire_trace] if logger = [:logger] @logger = logger elsif http_wire_trace? @logger = Logger.new($stdout) end end |
Instance Attribute Details
#http_wire_trace ⇒ Boolean (readonly) Also known as: http_wire_trace?, log_wire_trace?, log_wire_trace
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_timeout ⇒ Integer (readonly)
61 62 63 |
# File 'lib/net/http/connection_pool.rb', line 61 def idle_timeout @idle_timeout end |
#logger ⇒ Logger (readonly)
Returns Where debug output is sent.
75 76 77 |
# File 'lib/net/http/connection_pool.rb', line 75 def logger @logger end |
#open_timeout ⇒ 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.
138 139 140 141 142 |
# File 'lib/net/http/connection_pool.rb', line 138 def connection_for host, = {}, &block connection = Connection.new(self, host, ) 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.
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 |
#size ⇒ Object
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 |