connection_pool 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Changes.md +5 -0
- data/lib/connection_pool.rb +21 -8
- data/lib/connection_pool/version.rb +1 -1
- data/test/test_connection_pool.rb +36 -0
- metadata +2 -3
data/Changes.md
CHANGED
data/lib/connection_pool.rb
CHANGED
@@ -44,21 +44,34 @@ class ConnectionPool
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def with
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
conn = checkout
|
48
|
+
begin
|
49
|
+
yield conn
|
50
|
+
ensure
|
51
|
+
checkin
|
52
|
+
end
|
50
53
|
end
|
51
54
|
alias_method :with_connection, :with
|
52
55
|
|
53
56
|
def checkout
|
54
|
-
::Thread.current[@key] ||=
|
57
|
+
stack = ::Thread.current[@key] ||= []
|
58
|
+
|
59
|
+
if stack.empty?
|
60
|
+
conn = @available.timed_pop(@timeout)
|
61
|
+
else
|
62
|
+
conn = stack.last
|
63
|
+
end
|
64
|
+
|
65
|
+
stack.push conn
|
66
|
+
conn
|
55
67
|
end
|
56
68
|
|
57
69
|
def checkin
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
70
|
+
stack = ::Thread.current[@key]
|
71
|
+
conn = stack.pop
|
72
|
+
if stack.empty?
|
73
|
+
@available << conn
|
74
|
+
end
|
62
75
|
nil
|
63
76
|
end
|
64
77
|
|
@@ -86,4 +86,40 @@ class TestConnectionPool < MiniTest::Unit::TestCase
|
|
86
86
|
end
|
87
87
|
sleep 0.5
|
88
88
|
end
|
89
|
+
|
90
|
+
class Recorder
|
91
|
+
def initialize
|
92
|
+
@calls = []
|
93
|
+
end
|
94
|
+
|
95
|
+
attr_reader :calls
|
96
|
+
|
97
|
+
def do_work(label)
|
98
|
+
@calls << label
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_nested_checkout
|
103
|
+
recorder = Recorder.new
|
104
|
+
pool = ConnectionPool.new(:size => 1) { recorder }
|
105
|
+
pool.with do |r_outer|
|
106
|
+
@other = Thread.new do |t|
|
107
|
+
pool.with do |r_other|
|
108
|
+
r_other.do_work('other')
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
pool.with do |r_inner|
|
113
|
+
r_inner.do_work('inner')
|
114
|
+
end
|
115
|
+
|
116
|
+
sleep 0.1
|
117
|
+
|
118
|
+
r_outer.do_work('outer')
|
119
|
+
end
|
120
|
+
|
121
|
+
@other.join
|
122
|
+
|
123
|
+
assert_equal ['inner', 'outer', 'other'], recorder.calls
|
124
|
+
end
|
89
125
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: connection_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-06-30 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Generic connection pool for Ruby
|
15
15
|
email:
|
@@ -57,4 +57,3 @@ summary: Generic connection pool for Ruby
|
|
57
57
|
test_files:
|
58
58
|
- test/helper.rb
|
59
59
|
- test/test_connection_pool.rb
|
60
|
-
has_rdoc:
|