Class: Perpetuity::Postgres::ConnectionPool

Inherits:
Object
  • Object
show all
Defined in:
lib/perpetuity/postgres/connection_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ConnectionPool

Returns a new instance of ConnectionPool.



9
10
11
12
13
14
15
# File 'lib/perpetuity/postgres/connection_pool.rb', line 9

def initialize options={}
  @connections = Queue.new
  @size = options.delete(:pool_size) { 5 }
  @size.times do
    connections << Connection.new(options)
  end
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



7
8
9
# File 'lib/perpetuity/postgres/connection_pool.rb', line 7

def connections
  @connections
end

#sizeObject (readonly)

Returns the value of attribute size.



7
8
9
# File 'lib/perpetuity/postgres/connection_pool.rb', line 7

def size
  @size
end

Instance Method Details

#execute(sql) ⇒ Object



26
27
28
29
30
# File 'lib/perpetuity/postgres/connection_pool.rb', line 26

def execute sql
  lend_connection do |connection|
    connection.execute sql
  end
end

#lend_connectionObject



17
18
19
20
21
22
23
24
# File 'lib/perpetuity/postgres/connection_pool.rb', line 17

def lend_connection
  if block_given?
    connection = connections.pop
    yield connection
  end
ensure
  connections << connection
end

#tablesObject



32
33
34
35
36
# File 'lib/perpetuity/postgres/connection_pool.rb', line 32

def tables
  lend_connection do |connection|
    connection.tables
  end
end