Skip to content

Commit dbefd22

Browse files
committed
connection pool will reuse conns first before creating a new one
1 parent 069c3c1 commit dbefd22

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/resque/connection_pool.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ def checkout
2020
Timeout.timeout(@timeout) do
2121
@cv.wait_while { checked_out_conns.length >= @size }
2222
end
23-
if conns.size < @size
23+
available_conns = conns.find {|k, v| !v }
24+
if conns.size < @size && available_conns.nil?
2425
conn = Resque.create_connection(@url)
2526
else
26-
conn = conns.find {|k, v| !v }.first
27+
conn = available_conns.first
2728
end
2829
conns[conn] = true
2930
end

test/connection_pool_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,5 +113,13 @@ def method_missing(meth, *args, &block)
113113
refute_equal "fatal", e.class.to_s, e.message
114114
end
115115
end
116+
117+
it "reuse existing connections first if available" do
118+
cp = ConnectionPool.new(REDIS_URL, 2)
119+
conn = nil
120+
cp.with_connection {|c| conn = c }
121+
122+
assert_equal conn, cp.checkout, "Does not use the same connection"
123+
end
116124
end
117125
end

0 commit comments

Comments
 (0)