Ruby Common Object Pool (common-pool)

What is common-pool?

Ruby version of first-in-first-out (FIFO) object pooling library that checks for invalid and expired idle objects, inspired by Apache Commons Pool.

It supports the following configuration parameters:

  • min_idle
  • max_idle
  • max_idle_time
  • max_active
  • timeout
  • validation_timeout
  • idle_check_no_per_run
  • idle_check_interval

Overwrite CommonPool::DataSource to create object to be returned to the pool. For more information, please refer to the Rdoc documentation.

Links

Installation

$ gem install common-pool

Example

require 'common_pool'    
  
# Extend data source object
class RandomNumberDataSource < CommonPool::PoolDataSource
  # Overwrite to return object to be stored in the pool.
  def create_object
    rand(1000)
  end
    
  # Overwrite to check if idle object in the pool is still valid.
  def valid?(object)
    true
  end
end
  
# Create a new object pool
object_pool = ObjectPool.new(RandomNumberDataSource.new)
  
# Borrow object from the pool
object = object_pool.borrow_object

# Return object to the pool
object_pool.return_object(object)
  
# Or invalidate object and remove it from the pool
object_pool.invalidate_object(object)
  
# Create object pool with idle objects eviction thread
object_pool = ObjectPool.new(RandomNumberDataSource.new) do |config|
  config.min_idle = 5
  config.max_idle = 10
  
  # max 10 idle objects checked per run
  config.idle_check_no_per_run = 10

  # check idle objects every 10 minutes
  config.idle_check_interval = 10 * 60
end

# Return a hash of pool instance status variables, i.e.
# active and idle lists size, and configuration options
object_pool.status_info

2 Comments . Comments Feed . Trackback URI
Mon, 1 Sep 08 04:03 pm . dynnamitt wrote:

Do know if Rails/Merb would support this?
Is it possible in these frameworks to “keep” the pool between requests somehow?

Tue, 2 Sep 08 09:28 am . Herryanto Siatono wrote:

Yeah, but to use one pool across multiple requests, I’m afraid that you’ll have to wrap it in a background process, such as DrbServer.

Add Your Comment



(optional)