Class: ConnectionPool
- Inherits:
-
Object
- Object
- ConnectionPool
- Defined in:
- lib/connection_pool.rb,
lib/connection_pool/version.rb
Overview
Generic connection pool class for e.g. sharing a limited number of network connections among many threads. Note: Connections are lazily created.
Example usage with block (faster):
@pool = ConnectionPool.new { Redis.new }
@pool.with do |redis|
redis.lpop('my-list') if redis.llen('my-list') > 0
end
Using optional timeout override (for that single invocation)
@pool.with(:timeout => 2.0) do |redis|
redis.lpop('my-list') if redis.llen('my-list') > 0
end
Example usage replacing an existing connection (slower):
$redis = ConnectionPool.wrap { Redis.new }
def do_work
$redis.lpop('my-list') if $redis.llen('my-list') > 0
end
Accepts the following options:
-
:size - number of connections to pool, defaults to 5
-
:timeout - amount of time to wait for a connection if none currently available, defaults to 5 seconds
Defined Under Namespace
Classes: Error, PoolShuttingDownError, TimedStack, Wrapper
Constant Summary collapse
- DEFAULTS =
{size: 5, timeout: 5}
- VERSION =
"2.1.2"
Class Method Summary collapse
Instance Method Summary collapse
- #checkin ⇒ Object
- #checkout(options = {}) ⇒ Object
-
#initialize(options = {}, &block) ⇒ ConnectionPool
constructor
A new instance of ConnectionPool.
- #shutdown(&block) ⇒ Object
- #with(options = {}) ⇒ Object
Constructor Details
#initialize(options = {}, &block) ⇒ ConnectionPool
Returns a new instance of ConnectionPool.
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/connection_pool.rb', line 43 def initialize( = {}, &block) raise ArgumentError, 'Connection pool requires a block' unless block = DEFAULTS.merge() @size = .fetch(:size) @timeout = .fetch(:timeout) @available = TimedStack.new(@size, &block) @key = :"current-#{@available.object_id}" end |
Class Method Details
Instance Method Details
#checkin ⇒ Object
87 88 89 90 91 92 |
# File 'lib/connection_pool.rb', line 87 def checkin conn = pop_connection # mutates stack, must be on its own line @available.push(conn) if stack.empty? nil end |
#checkout(options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/connection_pool.rb', line 75 def checkout( = {}) conn = if stack.empty? timeout = [:timeout] || @timeout @available.pop(timeout: timeout) else stack.last end stack.push conn conn end |
#shutdown(&block) ⇒ Object
94 95 96 |
# File 'lib/connection_pool.rb', line 94 def shutdown(&block) @available.shutdown(&block) end |
#with(options = {}) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/connection_pool.rb', line 55 def with( = {}) # Connections can become corrupted via Timeout::Error. Discard # any connection whose usage after checkout does not finish as expected. # See #67 success = false conn = checkout() begin result = yield conn success = true # means the connection wasn't interrupted result ensure if success # everything is roses, we can safely check the connection back in checkin else @available.discard!(pop_connection) end end end |