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 CHANGED
@@ -1,3 +1,8 @@
1
+ 0.9.2
2
+ --------
3
+
4
+ - Fix reentrant checkout leading to early checkin.
5
+
1
6
  0.9.1
2
7
  --------
3
8
 
@@ -44,21 +44,34 @@ class ConnectionPool
44
44
  end
45
45
 
46
46
  def with
47
- yield checkout
48
- ensure
49
- checkin
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] ||= @available.timed_pop(@timeout)
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
- conn = ::Thread.current[@key]
59
- ::Thread.current[@key] = nil
60
- return unless conn
61
- @available << conn
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
 
@@ -1,3 +1,3 @@
1
1
  class ConnectionPool
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -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.1
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-04-02 00:00:00.000000000 Z
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: