Class: Philiprehberger::HttpClient::Pool
- Inherits:
-
Object
- Object
- Philiprehberger::HttpClient::Pool
- Defined in:
- lib/philiprehberger/http_client/pool.rb
Overview
Thread-safe connection pool that reuses Net::HTTP connections to the same host:port. Idle connections are automatically expired after the configured timeout.
Instance Attribute Summary collapse
-
#idle_timeout ⇒ Integer
readonly
Returns the idle timeout in seconds.
-
#size ⇒ Integer
readonly
Returns the maximum pool size.
Instance Method Summary collapse
-
#checkin(uri, connection) ⇒ void
Return a connection to the pool for reuse.
-
#checkout(uri) ⇒ Net::HTTP?
Check out a connection for the given URI’s host:port.
-
#drain ⇒ void
Close all pooled connections and clear the pool.
-
#idle_count ⇒ Integer
Return the number of idle connections across all hosts.
-
#initialize(size: 5, idle_timeout: 60) ⇒ Pool
constructor
A new instance of Pool.
Constructor Details
#initialize(size: 5, idle_timeout: 60) ⇒ Pool
Returns a new instance of Pool.
18 19 20 21 22 23 |
# File 'lib/philiprehberger/http_client/pool.rb', line 18 def initialize(size: 5, idle_timeout: 60) @size = size @idle_timeout = idle_timeout @monitor = Monitor.new @pools = {} end |
Instance Attribute Details
#idle_timeout ⇒ Integer (readonly)
Returns the idle timeout in seconds.
33 34 35 |
# File 'lib/philiprehberger/http_client/pool.rb', line 33 def idle_timeout @idle_timeout end |
#size ⇒ Integer (readonly)
Returns the maximum pool size.
28 29 30 |
# File 'lib/philiprehberger/http_client/pool.rb', line 28 def size @size end |
Instance Method Details
#checkin(uri, connection) ⇒ void
This method returns an undefined value.
Return a connection to the pool for reuse.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/philiprehberger/http_client/pool.rb', line 57 def checkin(uri, connection) key = pool_key(uri) @monitor.synchronize do entries = @pools[key] ||= [] purge_expired(entries) if entries.size < @size entries.push({ connection: connection, checked_in_at: now }) else safe_finish(connection) end end end |
#checkout(uri) ⇒ Net::HTTP?
Check out a connection for the given URI’s host:port. Returns an existing idle connection if available, or nil if none are available.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/philiprehberger/http_client/pool.rb', line 40 def checkout(uri) key = pool_key(uri) @monitor.synchronize do entries = @pools[key] || [] purge_expired(entries) @pools[key] = entries entry = entries.shift entry&.fetch(:connection) end end |
#drain ⇒ void
This method returns an undefined value.
Close all pooled connections and clear the pool.
74 75 76 77 78 79 80 81 |
# File 'lib/philiprehberger/http_client/pool.rb', line 74 def drain @monitor.synchronize do @pools.each_value do |entries| entries.each { |entry| safe_finish(entry[:connection]) } end @pools.clear end end |
#idle_count ⇒ Integer
Return the number of idle connections across all hosts.
86 87 88 89 90 |
# File 'lib/philiprehberger/http_client/pool.rb', line 86 def idle_count @monitor.synchronize do @pools.values.sum(&:size) end end |