Method: PG::EM::ConnectionPool#initialize

Defined in:
lib/pg/em/connection_pool.rb

#initialize(options = {}) {|pg, is_async, is_reset| ... } ⇒ ConnectionPool

Creates and initializes a new connection pool.

The connection pool allocates its first connection upon initialization unless lazy: true option is given.

Pass PG::EM::Client options together with ConnectionPool options:

  • :size = 4 - the maximum number of concurrent connections

  • :lazy = false - should lazy allocate first connection

  • :connection_class = PG::EM::Client

For convenience the given block will be set as the on_connect option.

Yield Parameters:

  • pg (Client)

    connected client instance on each newly created connection

  • is_async (Boolean)

    always true in a connection pool context

  • is_reset (Boolean)

    always false unless async_autoreconnect options is true and was actually re-connecting

Raises:

  • (PG::Error)
  • (ArgumentError)

See Also:



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/pg/em/connection_pool.rb', line 94

def initialize(options = {}, &on_connect)
  @available = []
  @pending = []
  @allocated = {}
  @max_size = DEFAULT_SIZE
  @connection_class = Client

  if block_given?
    options = {on_connect: on_connect}.merge(options)
  end

  lazy = false
  @options = options.reject do |key, value|
    case key.to_sym
    when :size, :max_size
      @max_size = value.to_i
      true
    when :connection_class
      @connection_class = value
      true
    when :lazy
      lazy = value
      true
    end
  end

  raise ArgumentError, "#{self.class}.new: pool size must be >= 1" if @max_size < 1

  # allocate first connection, unless we are lazy
  hold unless lazy
end