2014-03-13 23:31:25 -04:00
|
|
|
connection\_pool
|
2014-03-13 21:44:54 -04:00
|
|
|
=================
|
2021-04-12 10:43:54 -04:00
|
|
|
[![Build Status](https://github.com/mperham/connection_pool/actions/workflows/ci.yml/badge.svg)](https://github.com/mperham/connection_pool/actions/workflows/ci.yml)
|
2011-05-14 18:36:17 -04:00
|
|
|
|
|
|
|
Generic connection pooling for Ruby.
|
|
|
|
|
2014-03-13 21:44:54 -04:00
|
|
|
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-05-14 18:36:17 -04:00
|
|
|
|
2011-09-19 13:29:31 -04:00
|
|
|
|
2011-05-14 18:36:17 -04:00
|
|
|
Usage
|
2014-03-13 21:44:54 -04:00
|
|
|
-----
|
2011-05-14 18:36:17 -04:00
|
|
|
|
2014-03-13 21:44:54 -04:00
|
|
|
Create a pool of objects to share amongst the fibers or threads in your Ruby
|
|
|
|
application:
|
2011-05-14 18:36:17 -04:00
|
|
|
|
2012-07-05 09:42:30 -04:00
|
|
|
``` ruby
|
2014-03-13 21:48:45 -04:00
|
|
|
$memcached = ConnectionPool.new(size: 5, timeout: 5) { Dalli::Client.new }
|
2012-07-05 09:42:30 -04:00
|
|
|
```
|
2011-05-14 18:36:17 -04:00
|
|
|
|
|
|
|
Then use the pool in your application:
|
|
|
|
|
2012-07-05 09:42:30 -04:00
|
|
|
``` ruby
|
2014-03-13 21:48:45 -04:00
|
|
|
$memcached.with do |conn|
|
|
|
|
conn.get('some-count')
|
2012-07-05 09:42:30 -04:00
|
|
|
end
|
|
|
|
```
|
2011-05-14 18:36:17 -04:00
|
|
|
|
2021-04-12 07:43:35 -04:00
|
|
|
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,
|
2021-10-14 00:34:11 -04:00
|
|
|
`with` will raise a `ConnectionPool::TimeoutError` (a subclass of `Timeout::Error`).
|
2021-04-12 07:43:35 -04:00
|
|
|
|
2020-12-03 17:33:40 -05:00
|
|
|
You can also use `ConnectionPool#then` to support _both_ a
|
|
|
|
connection pool and a raw client (requires Ruby 2.5+).
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
# Compatible with a raw Redis::Client, and ConnectionPool Redis
|
|
|
|
$redis.then { |r| r.set 'foo' 'bar' }
|
|
|
|
```
|
|
|
|
|
2013-09-24 12:37:58 -04:00
|
|
|
Optionally, you can specify a timeout override using the with-block semantics:
|
|
|
|
|
|
|
|
``` ruby
|
2014-03-13 21:48:45 -04:00
|
|
|
$memcached.with(timeout: 2.0) do |conn|
|
|
|
|
conn.get('some-count')
|
2013-09-24 12:37:58 -04:00
|
|
|
end
|
|
|
|
```
|
|
|
|
|
2014-03-13 21:44:54 -04:00
|
|
|
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.
|
2013-09-24 12:37:58 -04:00
|
|
|
|
2015-07-27 14:08:50 -04:00
|
|
|
## Migrating to a Connection Pool
|
|
|
|
|
2014-03-13 21:44:54 -04:00
|
|
|
You can use `ConnectionPool::Wrapper` to wrap a single global connection,
|
2015-07-27 14:08:50 -04:00
|
|
|
making it easier to migrate existing connection code over time:
|
2012-03-14 12:26:39 -04:00
|
|
|
|
2012-07-05 09:42:30 -04:00
|
|
|
``` ruby
|
2020-05-11 06:16:50 -04:00
|
|
|
$redis = ConnectionPool::Wrapper.new(size: 5, timeout: 3) { Redis.new }
|
2012-07-05 09:42:30 -04:00
|
|
|
$redis.sadd('foo', 1)
|
|
|
|
$redis.smembers('foo')
|
|
|
|
```
|
2012-03-14 12:26:39 -04:00
|
|
|
|
2014-03-13 21:44:54 -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 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
|
|
|
|
2014-03-13 21:44:54 -04:00
|
|
|
Once you've ported your entire system to use `with`, you can simply remove
|
|
|
|
`Wrapper` and use the simpler and faster `ConnectionPool`.
|
2011-05-14 18:36:17 -04:00
|
|
|
|
2015-07-27 14:08:50 -04:00
|
|
|
|
|
|
|
## Shutdown
|
|
|
|
|
2014-05-28 12:14:00 -04:00
|
|
|
You can shut down a ConnectionPool instance once it should no longer be used.
|
|
|
|
Further checkout attempts will immediately raise an error but existing checkouts
|
|
|
|
will work.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
cp = ConnectionPool.new { Redis.new }
|
2015-08-05 15:03:35 -04:00
|
|
|
cp.shutdown { |conn| conn.quit }
|
2014-05-28 12:14:00 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
Shutting down a connection pool will block until all connections are checked in and closed.
|
2015-07-27 14:08:50 -04:00
|
|
|
**Note that shutting down is completely optional**; Ruby's garbage collector will reclaim
|
2014-05-28 12:14:00 -04:00
|
|
|
unreferenced pools under normal circumstances.
|
|
|
|
|
2021-01-28 13:29:18 -05:00
|
|
|
## Reload
|
|
|
|
|
|
|
|
You can reload a ConnectionPool instance in the case it is desired to close all
|
|
|
|
connections to the pool and, unlike `shutdown`, afterwards recreate connections
|
|
|
|
so the pool may continue to be used. Reloading may be useful after forking the
|
|
|
|
process.
|
|
|
|
|
|
|
|
```ruby
|
|
|
|
cp = ConnectionPool.new { Redis.new }
|
|
|
|
cp.reload { |conn| conn.quit }
|
|
|
|
cp.with { |conn| conn.get('some-count') }
|
|
|
|
```
|
|
|
|
|
|
|
|
Like `shutdown`, this will block until all connections are checked in and
|
|
|
|
closed.
|
|
|
|
|
2018-06-04 16:05:22 -04:00
|
|
|
## Current State
|
2018-06-04 15:41:58 -04:00
|
|
|
|
2018-06-04 16:05:22 -04:00
|
|
|
There are several methods that return information about a pool.
|
2018-06-04 15:41:58 -04:00
|
|
|
|
|
|
|
```ruby
|
|
|
|
cp = ConnectionPool.new(size: 10) { Redis.new }
|
|
|
|
cp.size # => 10
|
|
|
|
cp.available # => 10
|
|
|
|
|
|
|
|
cp.with do |conn|
|
|
|
|
cp.size # => 10
|
|
|
|
cp.available # => 9
|
|
|
|
end
|
|
|
|
```
|
2014-03-13 21:46:06 -04:00
|
|
|
|
|
|
|
Notes
|
|
|
|
-----
|
|
|
|
|
2014-03-13 23:31:25 -04:00
|
|
|
- Connections are lazily created as needed.
|
2014-03-13 21:46:06 -04:00
|
|
|
- There is no provision for repairing or checking the health of a connection;
|
2021-04-12 07:43:35 -04:00
|
|
|
connections should be self-repairing. This is true of the Dalli and Redis
|
2014-03-13 21:46:06 -04:00
|
|
|
clients.
|
2015-05-07 12:21:42 -04:00
|
|
|
- **WARNING**: Don't ever use `Timeout.timeout` in your Ruby code or you will see
|
2021-04-12 07:43:35 -04:00
|
|
|
occasional silent corruption and mysterious errors. The Timeout API is unsafe
|
|
|
|
and cannot be used correctly, ever. Use proper socket timeout options as
|
2015-05-07 12:21:42 -04:00
|
|
|
exposed by Net::HTTP, Redis, Dalli, etc.
|
|
|
|
|
2014-03-13 21:46:06 -04:00
|
|
|
|
2011-05-14 18:36:17 -04:00
|
|
|
Author
|
2014-03-13 21:44:54 -04:00
|
|
|
------
|
2011-05-14 18:36:17 -04:00
|
|
|
|
2021-04-12 07:43:35 -04:00
|
|
|
Mike Perham, [@getajobmike](https://twitter.com/getajobmike), <https://www.mikeperham.com>
|