2011-05-14 18:36:17 -04: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.
2011-09-19 13:29:31 -04:00
2011-05-14 18:36:17 -04:00
Install
------------
gem install connection_pool
2012-07-05 11:30:48 -04:00
Notes
------------
- Connections are eager created when the pool is created.
- 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.
2011-05-14 18:36:17 -04:00
Usage
------------
Create a pool of objects to share amongst the fibers or threads in your Ruby application:
2012-07-05 09:42:30 -04:00
``` ruby
@memcached = ConnectionPool.new(:size => 5, :timeout => 5) { Dalli::Client.new }
```
2011-05-14 18:36:17 -04:00
Then use the pool in your application:
2012-07-05 09:42:30 -04:00
``` ruby
2012-12-18 21:33:59 -05:00
@memcached .with do |dalli|
2012-07-05 09:42:30 -04:00
dalli.get('some-count')
end
```
2011-05-14 18:36:17 -04:00
2012-12-18 21:33:59 -05:00
If all the objects in the connection pool are in use, `with` will block
2012-11-02 14:21:57 -04:00
until one becomes available. If no object is available within `:timeout` seconds,
2012-12-18 21:33:59 -05:00
`with` will raise a `Timeout::Error` .
2012-11-02 14:21:57 -04:00
2013-09-24 12:37:58 -04:00
Optionally, you can specify a timeout override using the with-block semantics:
``` ruby
@memcached .with(:timeout => 2.0) do |dalli|
dalli.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.
2012-03-14 12:26:39 -04:00
You can use `ConnectionPool::Wrapper` to wrap a single global connection, making
it easier to port your connection code over time:
2012-07-05 09:42:30 -04:00
``` ruby
$redis = ConnectionPool::Wrapper.new(:size => 5, :timeout => 3) { Redis.connect }
$redis.sadd('foo', 1)
$redis.smembers('foo')
```
2012-03-14 12:26:39 -04:00
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
2012-12-18 21:33:59 -05:00
performance sensitive code to use `with` as soon as possible.
2012-03-14 12:26:39 -04:00
2012-07-05 09:42:30 -04:00
``` ruby
2012-12-18 21:33:59 -05:00
$redis.with do |conn|
2012-07-05 09:42:30 -04:00
conn.sadd('foo', 1)
conn.smembers('foo')
end
```
2012-03-14 12:26:39 -04:00
Once you've ported your entire system to use `with` , you can simply
remove ::Wrapper and use a simple, fast ConnectionPool.
2011-05-14 18:36:17 -04:00
Author
--------------
2011-09-19 13:29:31 -04:00
Mike Perham, [@mperham ](https://twitter.com/mperham ), < http: // mikeperham . com >