1
0
Fork 0
mirror of https://github.com/mperham/connection_pool synced 2023-03-27 23:22:21 -04:00
Generic connection pooling for Ruby
Find a file
chatgris f258f18c7b Added some colors to README.
Signed-off-by: chatgris <jboyer@af83.com>
2012-07-05 15:42:30 +02:00
lib Doc, bump version 2012-06-29 20:22:42 -07:00
test Fix nested checkouts 2012-06-18 16:15:50 +03:00
.gitignore Initial pass at a generic connection pool 2011-05-14 12:29:51 -07:00
Changes.md Doc, bump version 2012-06-29 20:22:42 -07:00
connection_pool.gemspec Initial pass at a generic connection pool 2011-05-14 12:29:51 -07:00
Gemfile Suggestions from @brixen 2011-09-25 19:23:35 -07:00
LICENSE Add project info, tests 2011-05-14 15:36:17 -07:00
Rakefile Add project info, tests 2011-05-14 15:36:17 -07:00
README.md Added some colors to README. 2012-07-05 15:42:30 +02:00

connection_pool

Generic connection pooling for Ruby.

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.

Install

gem install connection_pool

Usage

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_connection do |dalli|
  dalli.get('some-count')
end

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) { 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_connection as soon as possible.

$redis.with_connection do |conn|
  conn.sadd('foo', 1)
  conn.smembers('foo')
end

Once you've ported your entire system to use with, you can simply remove ::Wrapper and use a simple, fast ConnectionPool.

Author

Mike Perham, @mperham, http://mikeperham.com