Class: ActiveRecord::Bogacs::DefaultPool

Inherits:
Object
  • Object
show all
Includes:
PoolSupport, MonitorMixin
Defined in:
lib/active_record/bogacs/default_pool.rb

Overview

Obtaining (checking out) a connection

Connections can be obtained and used from a connection pool in several ways:

  1. Simply use ActiveRecord::Base.connection as with Active Record 2.1 and

earlier (pre-connection-pooling). Eventually, when you’re done with the connection(s) and wish it to be returned to the pool, you call ActiveRecord::Base.clear_active_connections!.

  1. Manually check out a connection from the pool with

ActiveRecord::Base.connection_pool.checkout. You are responsible for returning this connection to the pool when finished by calling ActiveRecord::Base.connection_pool.checkin(connection).

  1. Use ActiveRecord::Base.connection_pool.with_connection(&block), which

obtains a connection, yields it as the sole argument to the block, and returns it to the pool after the block completes.

Connections in the pool are actually AbstractAdapter objects (or objects compatible with AbstractAdapter’s interface).

Options

There are several connection-pooling-related options that you can add to your database connection configuration:

  • pool: number indicating size of connection pool (default 5)

  • checkout_timeout: number of seconds to block and wait for a connection

before giving up and raising a timeout error (default 5 seconds).

  • pool_initial: number of connections to pre-initialize when the pool

is created (default 0).

  • reaping_frequency: frequency in seconds to periodically run a reaper,

which attempts to find and close “dead” connections (can occur if a caller forgets to close a connection at the end of a thread or a thread dies unexpectedly) Default is ‘nil`, which means don’t run the periodical Reaper at all (reaping will still happen occasionally).

Defined Under Namespace

Classes: Queue

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PoolSupport

#current_connection_id, #new_connection

Constructor Details

#initialize(spec) ⇒ DefaultPool

Creates a new ConnectionPool object. spec is a ConnectionSpecification object which describes database connection information (e.g. adapter, host name, username, password, etc), as well as the maximum size for this ConnectionPool.

The default ConnectionPool maximum size is 5.



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/active_record/bogacs/default_pool.rb', line 195

def initialize(spec)
  super()

  @spec = spec

  @checkout_timeout = ( spec.config[:checkout_timeout] ||
      spec.config[:wait_timeout] || 5.0 ).to_f # <= 3.2 supports wait_timeout
  @reaper = Reaper.new self, spec.config[:reaping_frequency]
  @reaping = !! @reaper.run

  # default max pool size to 5
  if spec.config[:pool]
    @size = spec.config[:pool].to_i
  else
    if defined? Rails.env && ( (! Rails.env.development? && ! Rails.env.test?) rescue nil )
      logger && logger.debug("pool: option not set, using default size: 5")
    end
    @size = 5
  end

  # The cache of reserved connections mapped to threads
  @reserved_connections = ThreadSafe::Map.new(:initial_capacity => @size)

  @connections = []
  @automatic_reconnect = true

  @available = Queue.new self

  initial_size = spec.config[:pool_initial] || 0
  initial_size = @size if initial_size == true
  initial_size = (@size * initial_size).to_i if initial_size <= 1.0
  # NOTE: warn on onitial_size > size !
  prefill_initial_connections if ( @initial_size = initial_size.to_i ) > 0

  if frequency = spec.config[:validate_frequency]
    require 'active_record/bogacs/validator' unless self.class.const_defined?(:Validator)
    @validator = Validator.new self, frequency, spec.config[:validate_timeout]
    if @validator.run && @reaping
      logger && logger.info("pool: validator configured alongside with reaper")
    end
  end
end

Instance Attribute Details

#automatic_reconnectObject

Returns the value of attribute automatic_reconnect.



185
186
187
# File 'lib/active_record/bogacs/default_pool.rb', line 185

def automatic_reconnect
  @automatic_reconnect
end

#checkout_timeoutObject

Returns the value of attribute checkout_timeout.



185
186
187
# File 'lib/active_record/bogacs/default_pool.rb', line 185

def checkout_timeout
  @checkout_timeout
end

#connectionsObject (readonly)

Returns the value of attribute connections.



186
187
188
# File 'lib/active_record/bogacs/default_pool.rb', line 186

def connections
  @connections
end

#initial_sizeObject (readonly)

Returns the value of attribute initial_size.



187
188
189
# File 'lib/active_record/bogacs/default_pool.rb', line 187

def initial_size
  @initial_size
end

#reaperObject (readonly)

Returns the value of attribute reaper.



186
187
188
# File 'lib/active_record/bogacs/default_pool.rb', line 186

def reaper
  @reaper
end

#sizeObject (readonly)

Returns the value of attribute size.



186
187
188
# File 'lib/active_record/bogacs/default_pool.rb', line 186

def size
  @size
end

#specObject (readonly)

Returns the value of attribute spec.



186
187
188
# File 'lib/active_record/bogacs/default_pool.rb', line 186

def spec
  @spec
end

#validatorObject (readonly)

Returns the value of attribute validator.



186
187
188
# File 'lib/active_record/bogacs/default_pool.rb', line 186

def validator
  @validator
end

Instance Method Details

#active_connection?true, false

Is there an open connection that is being used for the current thread?

Returns:

  • (true, false)


258
259
260
261
262
263
264
265
# File 'lib/active_record/bogacs/default_pool.rb', line 258

def active_connection?
  connection_id = current_connection_id
  if conn = @reserved_connections.fetch(connection_id, nil)
    !! conn.in_use? # synchronize { conn.in_use? }
  else
    false
  end
end

#checkin(conn, released = nil) ⇒ Object

Check-in a database connection back into the pool.

object, which was obtained earlier by calling #checkout on this pool

Parameters:

  • connection (ActiveRecord::ConnectionAdapters::AbstractAdapter)

See Also:



378
379
380
381
382
383
384
385
386
# File 'lib/active_record/bogacs/default_pool.rb', line 378

def checkin(conn, released = nil)
  synchronize do
    _run_checkin_callbacks(conn)

    release conn, conn.owner unless released

    @available.add conn
  end
end

#checkoutActiveRecord::ConnectionAdapters::AbstractAdapter

Check-out a database connection from the pool, callers are expected to call #checkin when the connection is no longer needed, so that others can use it.

This is done by either returning and leasing existing connection, or by creating a new connection and leasing it.

and the pool is at capacity (meaning the number of currently leased connections is greater than or equal to the size limit set)

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)

Raises:

  • (ActiveRecord::ConnectionTimeoutError)

    if all connections are leased



364
365
366
367
368
369
370
371
# File 'lib/active_record/bogacs/default_pool.rb', line 364

def checkout
  conn = nil
  synchronize do
    conn = acquire_connection
    conn.lease
  end
  checkout_and_verify(conn)
end

#clear_reloadable_connections!Object

Clears the cache which maps classes.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/active_record/bogacs/default_pool.rb', line 311

def clear_reloadable_connections!
  synchronize do
    @reserved_connections.clear
    @connections.each do |conn|
      checkin conn
      conn.disconnect! if conn.requires_reloading?
    end
    @connections.delete_if do |conn|
      conn.requires_reloading?
    end
    @available.clear
    @connections.each do |conn|
      @available.add conn
    end
  end
end

#clear_stale_cached_connections!Object

Return any checked-out connections back to the pool by threads that are no longer alive.



343
344
345
346
347
348
349
350
351
# File 'lib/active_record/bogacs/default_pool.rb', line 343

def clear_stale_cached_connections!
  keys = Thread.list.find_all { |t| t.alive? }.map(&:object_id)
  keys = @reserved_connections.keys - keys
  keys.each do |key|
    conn = @reserved_connections[key]
    checkin conn
    @reserved_connections.delete(key)
  end
end

#connected?true, false

Returns true if a connection has already been opened.

Returns:

  • (true, false)


293
294
295
# File 'lib/active_record/bogacs/default_pool.rb', line 293

def connected?
  @connections.size > 0 # synchronize { @connections.any? }
end

#connectionActiveRecord::ConnectionAdapters::AbstractAdapter

Retrieve the connection associated with the current thread, or call #checkout to obtain one if necessary.

#connection can be called any number of times; the connection is held in a hash keyed by the thread id.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)


245
246
247
248
249
250
251
252
253
# File 'lib/active_record/bogacs/default_pool.rb', line 245

def connection
  connection_id = current_connection_id
  unless conn = @reserved_connections.fetch(connection_id, nil)
    synchronize do
      conn = ( @reserved_connections[connection_id] ||= checkout )
    end
  end
  conn
end

#disconnect!Object

Disconnects all connections in the pool, and clears the pool.



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/active_record/bogacs/default_pool.rb', line 298

def disconnect!
  synchronize do
    @reserved_connections.clear
    @connections.each do |conn|
      checkin conn
      conn.disconnect!
    end
    @connections.clear
    @available.clear
  end
end

#loggerObject

@@logger = nil



433
# File 'lib/active_record/bogacs/default_pool.rb', line 433

def logger; ::ActiveRecord::Base.logger end

#reapObject

Recover lost connections for the pool. A lost connection can occur if a caller forgets to #checkin a connection for a given thread when its done or a thread dies unexpectedly.



406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/active_record/bogacs/default_pool.rb', line 406

def reap
  stale_connections = synchronize do
    @connections.select do |conn|
      conn.in_use? && !conn.owner.alive?
    end
  end

  stale_connections.each do |conn|
    synchronize do
      if conn.active?
        conn.reset!
        checkin conn
      else
        remove conn
      end
    end
  end
end

#reaper?Boolean

NOTE: active? and reset! are >= AR 2.3

Returns:

  • (Boolean)


426
# File 'lib/active_record/bogacs/default_pool.rb', line 426

def reaper?; (@reaper ||= nil) && @reaper.frequency end

#reaping?Boolean

Returns:

  • (Boolean)


427
# File 'lib/active_record/bogacs/default_pool.rb', line 427

def reaping?; reaper? && @reaper.running? end

#release_connection(with_id = current_connection_id) ⇒ Object

Signal that the thread is finished with the current connection. #release_connection releases the connection-thread association and returns the connection to the pool.



270
271
272
273
274
275
# File 'lib/active_record/bogacs/default_pool.rb', line 270

def release_connection(with_id = current_connection_id)
  #synchronize do
    conn = @reserved_connections.delete(with_id)
    checkin conn, true if conn
  #end
end

#remove(conn) ⇒ ActiveRecord::ConnectionAdapters::AbstractAdapter

Remove a connection from the connection pool. The returned connection will remain open and active but will no longer be managed by this pool.

Returns:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)


392
393
394
395
396
397
398
399
400
401
# File 'lib/active_record/bogacs/default_pool.rb', line 392

def remove(conn)
  synchronize do
    @connections.delete conn
    @available.delete conn

    release conn, conn.owner

    @available.add checkout_new_connection if @available.any_waiting?
  end
end

#validating?Boolean

Returns:

  • (Boolean)


430
# File 'lib/active_record/bogacs/default_pool.rb', line 430

def validating?; validator? && @validator.running? end

#validator?Boolean

Returns:

  • (Boolean)


429
# File 'lib/active_record/bogacs/default_pool.rb', line 429

def validator?; (@validator ||= nil) && @validator.frequency end

#verify_active_connections!Object

Verify active connections and remove and disconnect connections associated with stale threads.



331
332
333
334
335
336
337
338
# File 'lib/active_record/bogacs/default_pool.rb', line 331

def verify_active_connections!
  synchronize do
    clear_stale_cached_connections!
    @connections.each do |connection|
      connection.verify!
    end
  end
end

#with_connection {|ActiveRecord::ConnectionAdapters::AbstractAdapter| ... } ⇒ Object

If a connection already exists yield it to the block. If no connection exists checkout a connection, yield it to the block, and checkin the connection when finished.

Yields:

  • (ActiveRecord::ConnectionAdapters::AbstractAdapter)


282
283
284
285
286
287
288
# File 'lib/active_record/bogacs/default_pool.rb', line 282

def with_connection
  connection_id = current_connection_id
  fresh_connection = true unless active_connection?
  yield connection
ensure
  release_connection(connection_id) if fresh_connection
end