Skip to content

Commit

Permalink
Allow restarting pool
Browse files Browse the repository at this point in the history
The implementation of shutdown from
#27 does not actually
provide for a way for the pool to re-create connections, only render the
pool unusable. This implements such a behavior. A new method is added so
as to not change the existing behavior of `shutdown`.
  • Loading branch information
amarshall committed Dec 15, 2020
1 parent 52b882f commit 1a3a6de
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/connection_pool.rb
Expand Up @@ -99,6 +99,10 @@ def shutdown(&block)
@available.shutdown(&block)
end

def restart(&block)
@available.restart(&block)
end

# Size of this connection pool
attr_reader :size

Expand Down
12 changes: 12 additions & 0 deletions lib/connection_pool/timed_stack.rb
Expand Up @@ -95,6 +95,17 @@ def shutdown(&block)
end
end

##
# Like +shutdown+, but will allow the pool to re-create connections
# afterwards.

def restart(&block)
raise ArgumentError, "restart must receive a block" unless block_given?

shutdown(&block)
@shutdown_block = nil
end

##
# Returns +true+ if there are no available connections.

Expand Down Expand Up @@ -143,6 +154,7 @@ def shutdown_connections(options = nil)
conn = fetch_connection(options)
@shutdown_block.call(conn)
end
@created = 0
end

##
Expand Down
10 changes: 10 additions & 0 deletions test/test_connection_pool_timed_stack.rb
Expand Up @@ -102,6 +102,16 @@ def test_pop_shutdown
end
end

def test_pop_restart
stack = ConnectionPool::TimedStack.new(1) { Object.new }
object = stack.pop
stack.push(object)

stack.restart {}

refute_equal object, stack.pop
end

def test_push
stack = ConnectionPool::TimedStack.new(1) { Object.new }

Expand Down

0 comments on commit 1a3a6de

Please sign in to comment.