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
Damian Janowski 84a9a32314 Make sure Wrapper proxies Kernel methods.
Proves that #54 is not an issue.
2014-03-13 23:02:21 -03:00
lib Require proper files. 2014-03-13 22:56:16 -03:00
test Make sure Wrapper proxies Kernel methods. 2014-03-13 23:02:21 -03:00
.gitignore Initial pass at a generic connection pool 2011-05-14 12:29:51 -07:00
.travis.yml Fix travis configuration 2014-02-14 16:53:15 -08:00
Changes.md Update version to 2.0.0 due to laziness 2014-03-13 16:54:03 -07:00
connection_pool.gemspec Add Damian to owners 2013-08-14 20:26:18 -07:00
Gemfile Gemfile cleanup. 2013-08-14 23:33:55 -03:00
LICENSE Add project info, tests 2011-05-14 15:36:17 -07:00
Rakefile Don't require Bundler to run tests. 2012-12-18 13:35:50 -03:00
README.md Prefer 1.9+ syntax in README + normalize examples. 2014-03-13 22:48:45 -03: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.

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

If 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.

Optionally, you can specify a timeout override using the with-block semantics:

$memcached.with(timeout: 2.0) do |conn|
  conn.get('some-count')
end

This will only modify the resource-get timeout for this particular invocation. This is useful if you want to fail-fast on certain non critical sections when a resource is not available, or conversely if you are comfortable blocking longer on a particular resource. This is not implemented in the below ConnectionPool::Wrapper class.

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

$redis.with 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 the simpler and faster ConnectionPool.

Notes

  • Connections are lazily created as they are needed.
  • 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.

Install

$ gem install connection_pool

Author

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