Generic (eager and lazy load) connection pooling for Ruby based on Mike Perham's own connection pool.
This implementation extends the existing one allowing the user to create connections using lazy loading pattern.
MongoDB has its own connection pool. ActiveRecord has its own connection pool. This is a generic connection pool that can be used with anything, e.g. Redis, Dalli and other Ruby network clients.
gem install connection_pool
- Connections are eager/lazy depending on :loading param on ConnectionPool's initializer (default :eager).
- There is no provision for repairing or checking the health of a connection; connections should be self-repairing. This is true of the dalli and redis clients.
Create a pool of objects to share amongst the fibers or threads in your Ruby application:
@memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new }Then use the pool in your application:
@memcached.with do |dalli|
dalli.get('some-count')
endIf all the objects in the connection pool are in use, with will block
until one becomes available. If no object is available within :timeout seconds,
with will raise a Timeout::Error.
You can use ConnectionPool::Wrapper to wrap a single global connection, making
it easier to port your connection code over time:
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3, :loading => :lazy) { Redis.connect }
$redis.sadd('foo', 1)
$redis.smembers('foo')The Wrapper uses method_missing to checkout a connection, run the
requested method and then immediately check the connection back into the
pool. It's not high-performance so you'll want to port your
performance sensitive code to use with as soon as possible.
$redis.with do |conn|
conn.sadd('foo', 1)
conn.smembers('foo')
endOnce you've ported your entire system to use with, you can simply
remove ::Wrapper and use a simple, fast ConnectionPool.