class FTW::Pool

A simple thread-safe resource pool.

Resources in this pool must respond to 'available?'. For best results, your resources should just 'include FTW::Poolable'

The primary use case was as a way to pool FTW::Connection instances.

Public Class Methods

new() click to toggle source
# File lib/ftw/pool.rb, line 11
def initialize
  # Pool is a hash of arrays.
  @pool = Hash.new { |h,k| h[k] = Array.new }
  @lock = Mutex.new
end

Public Instance Methods

add(identifier, object) click to toggle source

Add an object to the pool with a given identifier. For example:

pool.add("www.google.com:80", connection1)
pool.add("www.google.com:80", connection2)
pool.add("github.com:443", connection3)
# File lib/ftw/pool.rb, line 22
def add(identifier, object)
  @lock.synchronize do
    @pool[identifier] << object
  end
  return object
end
each(&block) click to toggle source

Iterate over all pool members.

This holds the pool lock during this method, so you should not call 'fetch' or 'add'.

# File lib/ftw/pool.rb, line 61
def each(&block)
  @lock.synchronize do
    @pool.each do |identifier, object|
      block.call(identifier, object)
    end
  end
end
fetch(identifier, &default_block) click to toggle source

Fetch a resource from this pool. If no available resources are found, the 'default_block' is invoked and expected to return a new resource to add to the pool that satisfies the fetch..

Example:

pool.fetch("github.com:443") do 
  conn = FTW::Connection.new("github.com:443")
  conn.secure
  conn
end
# File lib/ftw/pool.rb, line 41
def fetch(identifier, &default_block)
  @lock.synchronize do
    @pool[identifier].delete_if { |o| o.available? && !o.connected? }
    object = @pool[identifier].find { |o| o.available? }
    return object if !object.nil?
  end
  # Otherwise put the return value of default_block in the
  # pool and return it, but don't put nil values in the pool.
  obj = default_block.call
  if obj.nil?
    return nil
  else
    return add(identifier, obj)
  end
end