Class: RailsAi::Performance::ConnectionPool
- Inherits:
-
Object
- Object
- RailsAi::Performance::ConnectionPool
- Defined in:
- lib/rails_ai/performance.rb
Overview
Connection pooling for HTTP clients
Instance Method Summary collapse
-
#initialize(size: 10) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #with_connection ⇒ Object
Constructor Details
#initialize(size: 10) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
7 8 9 10 |
# File 'lib/rails_ai/performance.rb', line 7 def initialize(size: 10) @pool = Concurrent::Array.new(size) { create_connection } @semaphore = Concurrent::Semaphore.new(size) end |
Instance Method Details
#with_connection ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/rails_ai/performance.rb', line 12 def with_connection @semaphore.acquire connection = @pool.pop yield(connection) ensure @pool.push(connection) if connection @semaphore.release end |