Class ConnectionPool
In: lib/connection_pool/monotonic_time.rb
lib/connection_pool/version.rb
lib/connection_pool.rb
Parent: Object

Generic connection pool class for e.g. sharing a limited number of network connections among many threads. Note: Connections are lazily created.

Example usage with block (faster):

   @pool = ConnectionPool.new { Redis.new }

   @pool.with do |redis|
     redis.lpop('my-list') if redis.llen('my-list') > 0
   end

Using optional timeout override (for that single invocation)

   @pool.with(timeout: 2.0) do |redis|
     redis.lpop('my-list') if redis.llen('my-list') > 0
   end

Example usage replacing an existing connection (slower):

   $redis = ConnectionPool.wrap { Redis.new }

   def do_work
     $redis.lpop('my-list') if $redis.llen('my-list') > 0
   end

Accepts the following options:

  • :size - number of connections to pool, defaults to 5
  • :timeout - amount of time to wait for a connection if none currently available, defaults to 5 seconds

Methods

available   checkin   checkout   get_time   get_time   get_time   monotonic_time   new   new   shutdown   size   with   with   wrap  

Classes and Modules

Class ConnectionPool::Error
Class ConnectionPool::PoolShuttingDownError
Class ConnectionPool::TimedStack
Class ConnectionPool::Wrapper

Constants

GLOBAL_MONOTONIC_CLOCK = class_definition.new   Clock that cannot be set and represents monotonic time since some unspecified starting point.

@!visibility private

VERSION = "2.2.2"
DEFAULTS = {size: 5, timeout: 5}

Public Class methods

Returns the current time a tracked by the application monotonic clock.

@return [Float] The current monotonic time when `since` not given else

  the elapsed monotonic time between `since` and the current time

@!visibility private

Public Instance methods

Number of pool entries available for checkout at this instant.

@!visibility private

@!visibility private

@!visibility private

Size of this connection pool

[Validate]