Method: Sequel::ConnectionPool#initialize

Defined in:
lib/sequel_core/connection_pool.rb

#initialize(opts = {}, &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(:max_connections=>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(:max_connections=>10)
pool.connection_proc = proc {MyConnection.new(opts)}

The connection pool takes the following options:

  • :max_connections - The maximum number of connections the connection pool will open (default 4)

  • :pool_convert_exceptions - Whether to convert non-StandardError based exceptions to RuntimeError exceptions (default true)

  • :pool_sleep_time - The amount of time to sleep before attempting to acquire a connection again (default 0.001)

  • :pool_timeout - The amount of seconds to wait to acquire a connection before raising a PoolTimeoutError (default 5)

  • :servers - A hash of servers to use. Keys should be symbols. If not present, will use a single :default server. The server name symbol will be passed to the connection_proc.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sequel_core/connection_pool.rb', line 42

def initialize(opts = {}, &block)
  @max_size = opts[:max_connections] || 4
  @mutex = Mutex.new
  @connection_proc = block
  @disconnection_proc = opts[:disconnection_proc]
  @servers = [:default]
  @servers += opts[:servers].keys - @servers if opts[:servers] 
  @available_connections = Hash.new{|h,k| h[:default]}
  @allocated = Hash.new{|h,k| h[:default]}
  @created_count = Hash.new{|h,k| h[:default]}
  @servers.each do |s|
    @available_connections[s] = []
    @allocated[s] = {}
    @created_count[s] = 0
  end
  @timeout = opts[:pool_timeout] || 5
  @sleep_time = opts[:pool_sleep_time] || 0.001
  @convert_exceptions = opts.include?(:pool_convert_exceptions) ? opts[:pool_convert_exceptions] : true
end