Class: RailsAi::Performance::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_ai/performance.rb

Overview

Connection pooling for HTTP clients

Instance Method Summary collapse

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_connectionObject



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