Skip to content

Commit 55e9717

Browse files
committed
make connection pool fork aware
1 parent 1691152 commit 55e9717

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

lib/resque/connection_pool.rb

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ def initialize(url, size, timeout = nil)
88
@url = url
99
@size = size
1010
@timeout = timeout
11-
@conns = {}
11+
@conns = Hash.new { |h,k|
12+
h[Process.pid] = {}
13+
}
1214
end
1315

1416
def checkout
@@ -18,20 +20,20 @@ def checkout
1820
Timeout.timeout(@timeout) do
1921
@cv.wait_while { checked_out_conns.length >= @size }
2022
end
21-
if @conns.size < @size
23+
if conns.size < @size
2224
conn = Resque.create_connection(@url)
2325
else
24-
conn = @conns.find {|k, v| !v }.first
26+
conn = conns.find {|k, v| !v }.first
2527
end
26-
@conns[conn] = true
28+
conns[conn] = true
2729
end
2830

2931
conn
3032
end
3133

3234
def checkin(conn)
3335
@lock.synchronize do
34-
@conns[conn] = false
36+
conns[conn] = false
3537
end
3638
end
3739

@@ -44,7 +46,11 @@ def with_connection
4446

4547
private
4648
def checked_out_conns
47-
@conns.find_all {|k, v| v }.map(&:first)
49+
conns.find_all {|k, v| v }.map(&:first)
50+
end
51+
52+
def conns
53+
@conns[Process.pid]
4854
end
4955
end
5056
end

test/connection_pool_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,14 @@ module Resque
5757
end
5858
assert_equal c, cp.checkout
5959
end
60+
61+
it 'is fork aware' do
62+
cp = ConnectionPool.new(REDIS_URL, 1)
63+
conn = cp.checkout
64+
65+
Process.waitpid fork {
66+
assert cp.checkout
67+
}
68+
end
6069
end
6170
end

0 commit comments

Comments
 (0)