gene_pool 1.4.0 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +2 -0
  3. data/History.md +4 -0
  4. data/lib/gene_pool.rb +28 -14
  5. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc1bdaf7ba625db87160e8afdf2ed0dc1769c036
4
- data.tar.gz: af8f23899faaea5e5c681807ffaab6ea29651a3b
3
+ metadata.gz: 65f8bc24d422c5746e94dc1e295908b58da9220f
4
+ data.tar.gz: 9784d1f48c019d0e713b220150354973bf80b11c
5
5
  SHA512:
6
- metadata.gz: 4a861f3844f435b77944cb4699335066278facec8c71bfa581c16bd480ffcd9d34192bbbce6fe6095de474779a66bc59838a5f2a10d62aae54cd1b53418f8220
7
- data.tar.gz: 012a79fb29dc53ebf5ae09a94913e45df5ee1fd810c797671f9a090bb7cd50928b38c06e620d3eb56c61cf4b2481f9d18576ebe88e7f5d683c593ffb3e0c8fca
6
+ metadata.gz: 94e48da354cfadc86e17640d18a926525f748659d9e5ed2b1571ad0be4a2f1be552901aa6d4d585c2c7f211687f784b54fa3b8299788820577985a150e4c1d46
7
+ data.tar.gz: 767e6165d9704693e62d4b70ae0f8aa8765fd1163f726e6cdede4f3a22afcb8030600a7c0097afc5122b37fb6397f9679d9f5eb8291029aaf54babf64c79a27b
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gemspec
4
+
3
5
  platforms :rbx do
4
6
  gem 'rubysl', '~> 2.0'
5
7
  gem 'rubysl-test-unit'
data/History.md CHANGED
@@ -1,6 +1,10 @@
1
1
  GenePool Changelog
2
2
  =====================
3
3
 
4
+ 1.4.1 / 2014-04-29
5
+ - Close connection outside of the mutex.
6
+ - When iterating over the connections, don't include connections still in the process of being created (set to the reserved_placeholder).
7
+
4
8
  1.4.0 / 2014-04-18
5
9
 
6
10
  - Use Monitor instead of Mutex for locking to prevent recursive lock issues.
@@ -1,11 +1,13 @@
1
1
  require 'logger'
2
2
  require 'thread'
3
+ require 'thread_safe'
3
4
  require 'monitor'
4
5
 
5
6
  # Generic connection pool class
6
7
  class GenePool
7
8
 
8
- attr_accessor :name, :pool_size, :warn_timeout, :logger, :timeout_class
9
+ attr_accessor :name, :warn_timeout, :logger, :timeout_class
10
+ attr_reader :pool_size
9
11
 
10
12
  # Creates a gene_pool. The passed block will be used to initialize a single instance of
11
13
  # the item being pooled (i.e., socket connection or whatever)
@@ -40,7 +42,7 @@ class GenePool
40
42
  @checked_out = []
41
43
  # Map the original connections object_id within the with_connection method to the final connection.
42
44
  # This could change if the connection is renew'ed.
43
- @with_map = {}
45
+ @with_map = ThreadSafe::Hash.new
44
46
 
45
47
  setup_mutex
46
48
  end
@@ -130,16 +132,12 @@ class GenePool
130
132
  # Note that with_connection_auto_remove automatically does this
131
133
  def with_connection
132
134
  connection = checkout
133
- @mutex.synchronize do
134
- @with_map[connection.object_id] = connection
135
- end
135
+ @with_map[connection.object_id] = connection
136
136
  begin
137
137
  yield connection
138
138
  ensure
139
- @mutex.synchronize do
140
- # Update connection for any renew's that have occurred
141
- connection = @with_map.delete(connection.object_id)
142
- end
139
+ # Update connection for any renew's that have occurred
140
+ connection = @with_map.delete(connection.object_id)
143
141
  checkin(connection) if connection
144
142
  end
145
143
  end
@@ -149,7 +147,7 @@ class GenePool
149
147
  with_connection do |connection|
150
148
  begin
151
149
  yield connection
152
- rescue Exception => e
150
+ rescue Exception
153
151
  remove(connection)
154
152
  raise
155
153
  end
@@ -204,10 +202,8 @@ class GenePool
204
202
  @mutex.synchronize do
205
203
  index = @checked_out.index(old_connection)
206
204
  raise Error.new("Can't reassign non-checked out connection for #{@name}") unless index
207
- close_connection(old_connection)
208
205
  @checked_out[index] = new_connection
209
206
  @connections[@connections.index(old_connection)] = new_connection
210
-
211
207
  # If this is part of a with_connection block, then track our new connection
212
208
  if @with_map.respond_to?(:key)
213
209
  with_key = @with_map.key(old_connection)
@@ -218,7 +214,10 @@ class GenePool
218
214
 
219
215
  @with_map[with_key] = new_connection if with_key
220
216
  end
221
- @logger.debug {"#{@name}: Renewed connection old=#{old_connection.object_id} new=#{new_connection}(#{new_connection.object_id})"}
217
+ # Since connection has been removed, it can be closed outside the mutex
218
+ close_connection(old_connection)
219
+
220
+ @logger.debug {"#{@name}: Renewed connection old=#{old_connection.inspect} new=#{new_connection.inspect}"}
222
221
  return new_connection
223
222
  end
224
223
 
@@ -226,11 +225,26 @@ class GenePool
226
225
  # This should probably only ever be used to allow interrupt of a connection that is checked out?
227
226
  def each
228
227
  @mutex.synchronize do
229
- @connections.each { |connection| yield connection }
228
+ # Don't include the ones in a reserved_placeholder state because that object is meaningless
229
+ @connections.each { |connection| yield connection unless connection.kind_of?(Thread) }
230
230
  end
231
231
  end
232
232
 
233
+ # Return a copy of all the current connections
234
+ def connections
235
+ connections = @mutex.synchronize { connections = @connections.dup }
236
+ connections.delete_if { |c| c.kind_of?(Thread) }
237
+ connections.freeze
238
+ connections
239
+ end
240
+
241
+ # Close all connections and wait for active connections to complete
242
+ #
243
+ # Parameters:
244
+ # timeout:
245
+ # Maximum time to wait for connections to close before returning
233
246
  def close(timeout=10)
247
+ # Prevent any new connections from being handed out
234
248
  self.pool_size = 0
235
249
  start_time = Time.now
236
250
  while (Time.now - start_time) < timeout
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gene_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.0
4
+ version: 1.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Pardee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-18 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2014-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thread_safe
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ requirement: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ prerelease: false
26
+ type: :runtime
13
27
  description: Generic pooling library for creating a connection pool
14
28
  email:
15
29
  - bradpardee@gmail.com