Class: ConnectionPool

Inherits:
Object show all
Defined in:
lib/assistance/connection_pool.rb

Overview

A ConnectionPool manages access to database connections by keeping multiple connections and giving threads exclusive access to each connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = 4, &block) ⇒ ConnectionPool

Constructs a new pool with a maximum size. If a block is supplied, it is used to create new connections as they are needed.

pool = ConnectionPool.new(10) {MyConnection.new(opts)}

The connection creation proc can be changed at any time by assigning a Proc to pool#connection_proc.

pool = ConnectionPool.new(10)
pool.connection_proc = proc {MyConnection.new(opts)}


27
28
29
30
31
32
33
34
35
# File 'lib/assistance/connection_pool.rb', line 27

def initialize(max_size = 4, &block)
  @max_size = max_size
  @mutex = Mutex.new
  @connection_proc = block

  @available_connections = []
  @allocated = {}
  @created_count = 0
end

Instance Attribute Details

#allocatedObject (readonly)

Returns the value of attribute allocated.



15
16
17
# File 'lib/assistance/connection_pool.rb', line 15

def allocated
  @allocated
end

#available_connectionsObject (readonly)

Returns the value of attribute available_connections.



15
16
17
# File 'lib/assistance/connection_pool.rb', line 15

def available_connections
  @available_connections
end

#connection_procObject

The proc used to create a new connection.



13
14
15
# File 'lib/assistance/connection_pool.rb', line 13

def connection_proc
  @connection_proc
end

#created_countObject (readonly)

Returns the value of attribute created_count.



15
16
17
# File 'lib/assistance/connection_pool.rb', line 15

def created_count
  @created_count
end

#max_sizeObject (readonly)

The maximum number of connections.



10
11
12
# File 'lib/assistance/connection_pool.rb', line 10

def max_size
  @max_size
end

#mutexObject (readonly)

Returns the value of attribute mutex.



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

def mutex
  @mutex
end

Instance Method Details

#disconnect(&block) ⇒ Object

Removes all connection currently available, optionally yielding each connection to the given block. This method has the effect of disconnecting from the database. Once a connection is requested using #hold, the connection pool creates new connections to the database.



74
75
76
77
78
79
80
# File 'lib/assistance/connection_pool.rb', line 74

def disconnect(&block)
  @mutex.synchronize do
    @available_connections.each {|c| block[c]} if block
    @available_connections = []
    @created_count = @allocated.size
  end
end

#holdObject

Assigns a connection to the current thread, yielding the connection to the supplied block.

pool.hold {|conn| conn.execute('DROP TABLE posts')}

Pool#hold is re-entrant, meaning it can be called recursively in the same thread without blocking.

If no connection is available, Pool#hold will block until a connection is available.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/assistance/connection_pool.rb', line 52

def hold
  t = Thread.current
  if (conn = owned_connection(t))
    return yield(conn)
  end
  while !(conn = acquire(t))
    sleep 0.001
  end
  begin
    yield conn
  ensure
    release(t)
  end
rescue Exception => e
  # if the error is not a StandardError it is converted into RuntimeError.
  raise e.is_a?(StandardError) ? e : e.message
end

#sizeObject

Returns the number of created connections.



38
39
40
# File 'lib/assistance/connection_pool.rb', line 38

def size
  @created_count
end