gene_pool 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/{History.md → CHANGELOG.md} +5 -0
- data/Gemfile +4 -15
- data/README.md +49 -45
- data/lib/gene_pool.rb +2 -2
- data/test/gene_pool_test.rb +32 -37
- metadata +27 -25
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ed271080c3002e5df818bb015aa625dde4241778
|
4
|
+
data.tar.gz: 4a48d03387d5f76edb87eec64acb1b7d5dbe9d63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 37d9bc6cf14105a9cecf3f82bcdb44debaba13009edde4258a486fc2336284e9443a07bec236266b6fe5e96f4d20bdd3b8c1209593e0cc73cacdf142fb4d3321
|
7
|
+
data.tar.gz: e2517b7333784fbaf97279a908fbc9b3b755b71a08e12474c8e499113391fade9608a1156d706d26ae9d643012fe2b07dc44c23deff10acf22bb92b655d514d2
|
data/{History.md → CHANGELOG.md}
RENAMED
@@ -1,6 +1,11 @@
|
|
1
1
|
GenePool Changelog
|
2
2
|
=====================
|
3
3
|
|
4
|
+
1.5.0 / 2019-02-11
|
5
|
+
- Replace deprecated `thread_safe` gem with `concurrent-ruby`.
|
6
|
+
- Update tests with latest mini-test spec.
|
7
|
+
- Update Readme.
|
8
|
+
|
4
9
|
1.4.1 / 2014-04-29
|
5
10
|
- Close connection outside of the mutex.
|
6
11
|
- When iterating over the connections, don't include connections still in the process of being created (set to the reserved_placeholder).
|
data/Gemfile
CHANGED
@@ -1,18 +1,7 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gemspec
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
group :development do
|
11
|
-
gem 'rake'
|
12
|
-
gem 'rdoc'
|
13
|
-
end
|
14
|
-
|
15
|
-
group :test do
|
16
|
-
gem 'minitest', "~> 4.0"
|
17
|
-
gem 'shoulda'
|
18
|
-
end
|
5
|
+
gem 'rake'
|
6
|
+
gem 'awesome_print'
|
7
|
+
gem 'minitest'
|
data/README.md
CHANGED
@@ -2,61 +2,65 @@
|
|
2
2
|
|
3
3
|
* http://github.com/bpardee/gene_pool
|
4
4
|
|
5
|
-
##
|
5
|
+
## Description:
|
6
6
|
|
7
|
-
|
7
|
+
Highly performant Ruby connection pooling library.
|
8
8
|
|
9
|
-
##
|
9
|
+
## Features:
|
10
10
|
|
11
11
|
* Thread-safe
|
12
12
|
* Pure ruby
|
13
13
|
|
14
|
-
##
|
14
|
+
## Installation:
|
15
15
|
|
16
16
|
gem install gene_pool
|
17
17
|
|
18
|
-
##
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
# Equivalent to send_message above
|
46
|
-
def send_message_auto_remove
|
47
|
-
# On exception, close and reopen socket and perform retry
|
48
|
-
@@gene_pool.with_connection_auto_remove do |socket|
|
49
|
-
# use socket here,
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def send_message_auto_retry
|
54
|
-
# On exception, close and reopen socket and perform retry
|
55
|
-
@@gene_pool.with_connection_auto_retry do |socket|
|
56
|
-
# use socket here,
|
57
|
-
end
|
58
|
-
end
|
18
|
+
## Example Usage:
|
19
|
+
|
20
|
+
~~~ruby
|
21
|
+
class MyClient
|
22
|
+
# Print a logger warning if it requires more than 0.25 seconds to acquire a connection.
|
23
|
+
# Close and reopen the connection if it hasn't been used for 10 seconds.
|
24
|
+
# Raise Timeout::Error if waiting for a connection more than 3 seconds.
|
25
|
+
@gene_pool = GenePool.new(
|
26
|
+
name: 'MyClient',
|
27
|
+
pool_size: 10,
|
28
|
+
timeout: 3,
|
29
|
+
warn_timeout: 0.25,
|
30
|
+
idle_timeout: 10,
|
31
|
+
logger: Rails.logger,
|
32
|
+
close_proc: :close) do
|
33
|
+
|
34
|
+
TCPSocket.new('myserver', 4321)
|
35
|
+
end
|
36
|
+
|
37
|
+
def send_message
|
38
|
+
@gene_pool.with_connection do |socket|
|
39
|
+
begin
|
40
|
+
# use socket here
|
41
|
+
rescue Exception => e
|
42
|
+
# If the socket gets closed, remove it from the pool
|
43
|
+
@gene_pool.remove(socket)
|
59
44
|
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# Equivalent to send_message above
|
49
|
+
def send_message_auto_remove
|
50
|
+
# On exception, close and remeove socket from pool
|
51
|
+
@gene_pool.with_connection_auto_remove do |socket|
|
52
|
+
# use socket here,
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def send_message_auto_retry
|
57
|
+
# On exception, close and reopen socket and perform retry
|
58
|
+
@gene_pool.with_connection_auto_retry do |socket|
|
59
|
+
# use socket here,
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
~~~
|
60
64
|
|
61
65
|
## Copyright
|
62
66
|
|
data/lib/gene_pool.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'logger'
|
2
2
|
require 'thread'
|
3
|
-
require '
|
3
|
+
require 'concurrent-ruby'
|
4
4
|
require 'monitor'
|
5
5
|
|
6
6
|
# Generic connection pool class
|
@@ -42,7 +42,7 @@ class GenePool
|
|
42
42
|
@checked_out = []
|
43
43
|
# Map the original connections object_id within the with_connection method to the final connection.
|
44
44
|
# This could change if the connection is renew'ed.
|
45
|
-
@with_map =
|
45
|
+
@with_map = Concurrent::Hash.new
|
46
46
|
|
47
47
|
setup_mutex
|
48
48
|
end
|
data/test/gene_pool_test.rb
CHANGED
@@ -1,14 +1,11 @@
|
|
1
|
-
|
2
|
-
require 'test/unit'
|
3
|
-
require 'shoulda'
|
4
|
-
require 'gene_pool'
|
1
|
+
require_relative 'test_helper.rb'
|
5
2
|
require 'stringio'
|
6
3
|
require 'logger'
|
7
4
|
require 'timeout'
|
8
5
|
|
9
6
|
# Increase visibility
|
10
7
|
class GenePool
|
11
|
-
attr_reader :
|
8
|
+
attr_reader :checked_out, :with_map
|
12
9
|
end
|
13
10
|
|
14
11
|
class MyTimeoutError < RuntimeError; end
|
@@ -51,15 +48,13 @@ class DummyConnection
|
|
51
48
|
end
|
52
49
|
end
|
53
50
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
context 'on default setup' do
|
58
|
-
setup do
|
51
|
+
class GenePoolTest < Minitest::Test
|
52
|
+
describe 'on default setup' do
|
53
|
+
before do
|
59
54
|
@gene_pool = GenePool.new { Object.new }
|
60
55
|
end
|
61
56
|
|
62
|
-
|
57
|
+
it 'have default options set' do
|
63
58
|
assert_equal 'GenePool', @gene_pool.name
|
64
59
|
assert_equal 1, @gene_pool.pool_size
|
65
60
|
assert_equal 5.0, @gene_pool.warn_timeout
|
@@ -67,8 +62,8 @@ class GenePoolTest < Test::Unit::TestCase
|
|
67
62
|
end
|
68
63
|
end
|
69
64
|
|
70
|
-
|
71
|
-
|
65
|
+
describe GenePool do
|
66
|
+
before do
|
72
67
|
#@stringio = StringIO.new
|
73
68
|
#@logger = Logger.new($stdout)
|
74
69
|
#@logger = Logger.new(@stringio)
|
@@ -88,14 +83,14 @@ class GenePoolTest < Test::Unit::TestCase
|
|
88
83
|
end
|
89
84
|
end
|
90
85
|
|
91
|
-
|
86
|
+
it 'have options set' do
|
92
87
|
assert_equal 'TestGenePool', @gene_pool.name
|
93
88
|
assert_equal 10, @gene_pool.pool_size
|
94
89
|
assert_equal 2.0, @gene_pool.warn_timeout
|
95
90
|
#assert_same @logger, @gene_pool.logger
|
96
91
|
end
|
97
92
|
|
98
|
-
|
93
|
+
it 'create 1 connection' do
|
99
94
|
(1..3).each do |i|
|
100
95
|
@gene_pool.with_connection do |conn|
|
101
96
|
assert_equal conn.count, 1
|
@@ -108,7 +103,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
108
103
|
end
|
109
104
|
end
|
110
105
|
|
111
|
-
|
106
|
+
it 'create 2 connections' do
|
112
107
|
conn1 = @gene_pool.checkout
|
113
108
|
(1..3).each do |i|
|
114
109
|
@gene_pool.with_connection do |conn2|
|
@@ -127,7 +122,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
127
122
|
assert_equal 0, @gene_pool.checked_out.size
|
128
123
|
end
|
129
124
|
|
130
|
-
|
125
|
+
it 'be able to reset multiple times' do
|
131
126
|
@gene_pool.with_connection do |conn1|
|
132
127
|
conn2 = @gene_pool.renew(conn1)
|
133
128
|
conn3 = @gene_pool.renew(conn2)
|
@@ -141,7 +136,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
141
136
|
assert_equal 0, @gene_pool.checked_out.size
|
142
137
|
end
|
143
138
|
|
144
|
-
|
139
|
+
it 'be able to remove connection' do
|
145
140
|
@gene_pool.with_connection do |conn|
|
146
141
|
@gene_pool.remove(conn)
|
147
142
|
assert_equal 0, @gene_pool.connections.size
|
@@ -151,7 +146,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
151
146
|
assert_equal 0, @gene_pool.checked_out.size
|
152
147
|
end
|
153
148
|
|
154
|
-
|
149
|
+
it 'be able to remove multiple connections' do
|
155
150
|
@gene_pool.with_connection do |conn1|
|
156
151
|
@gene_pool.with_connection do |conn2|
|
157
152
|
@gene_pool.with_connection do |conn3|
|
@@ -172,7 +167,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
172
167
|
assert_equal 0, @gene_pool.checked_out.size
|
173
168
|
end
|
174
169
|
|
175
|
-
|
170
|
+
it 'handle aborted connection' do
|
176
171
|
@gene_pool.with_connection do |conn1|
|
177
172
|
@sleep = 2
|
178
173
|
assert_raises RuntimeError do
|
@@ -195,7 +190,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
195
190
|
end
|
196
191
|
end
|
197
192
|
|
198
|
-
|
193
|
+
it 'not allow more than pool_size connections' do
|
199
194
|
conns = []
|
200
195
|
pool_size = @gene_pool.pool_size
|
201
196
|
(1..pool_size).each do |i|
|
@@ -218,7 +213,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
218
213
|
end
|
219
214
|
end
|
220
215
|
|
221
|
-
|
216
|
+
it 'handle thread contention' do
|
222
217
|
conns = []
|
223
218
|
pool_size = @gene_pool.pool_size
|
224
219
|
# Do it with new connections and old connections
|
@@ -248,7 +243,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
248
243
|
assert_equal (1..pool_size).to_a, ival_conns
|
249
244
|
end
|
250
245
|
|
251
|
-
|
246
|
+
it 'be able to auto-retry connection' do
|
252
247
|
conns = []
|
253
248
|
@gene_pool.with_connection_auto_retry do |conn|
|
254
249
|
conns << conn
|
@@ -269,7 +264,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
269
264
|
assert_equal 0, @gene_pool.checked_out.size
|
270
265
|
end
|
271
266
|
|
272
|
-
|
267
|
+
it 'fail with auto-retry on double failure' do
|
273
268
|
e = assert_raises Exception do
|
274
269
|
@gene_pool.with_connection_auto_retry do |conn|
|
275
270
|
conn.fail_on(1,2)
|
@@ -283,7 +278,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
283
278
|
end
|
284
279
|
end
|
285
280
|
|
286
|
-
|
281
|
+
it 'not auto-retry on timeout' do
|
287
282
|
assert_raises RuntimeError do
|
288
283
|
Timeout.timeout(1, RuntimeError) do
|
289
284
|
@gene_pool.with_connection_auto_retry do |conn|
|
@@ -303,7 +298,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
303
298
|
assert_equal 0, @gene_pool.checked_out.size
|
304
299
|
end
|
305
300
|
|
306
|
-
|
301
|
+
it 'allow cleanup of idle connections' do
|
307
302
|
conn1 = @gene_pool.checkout
|
308
303
|
conn2 = @gene_pool.checkout
|
309
304
|
@gene_pool.checkin(conn1)
|
@@ -318,8 +313,8 @@ class GenePoolTest < Test::Unit::TestCase
|
|
318
313
|
end
|
319
314
|
end
|
320
315
|
|
321
|
-
|
322
|
-
|
316
|
+
describe 'idle timeout' do
|
317
|
+
before do
|
323
318
|
# Override sleep in individual tests
|
324
319
|
@sleep = nil
|
325
320
|
counter = 0
|
@@ -335,7 +330,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
335
330
|
end
|
336
331
|
end
|
337
332
|
|
338
|
-
|
333
|
+
it 'create a new connection if we pass the idle timeout' do
|
339
334
|
conn1, conn2, conn3 = nil
|
340
335
|
@gene_pool.with_connection { |conn| conn1 = conn }
|
341
336
|
@gene_pool.with_connection { |conn| conn2 = conn }
|
@@ -351,8 +346,8 @@ class GenePoolTest < Test::Unit::TestCase
|
|
351
346
|
end
|
352
347
|
end
|
353
348
|
|
354
|
-
|
355
|
-
|
349
|
+
describe 'close_proc' do
|
350
|
+
it 'default to close if not specified' do
|
356
351
|
@gene_pool = GenePool.new do
|
357
352
|
DummyConnection.new(0, 0)
|
358
353
|
end
|
@@ -363,7 +358,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
363
358
|
assert !conn.other_closed?
|
364
359
|
end
|
365
360
|
|
366
|
-
|
361
|
+
it 'allow symbol to execute a different method from close' do
|
367
362
|
@gene_pool = GenePool.new(:close_proc => :other_close) do
|
368
363
|
DummyConnection.new(0, 0)
|
369
364
|
end
|
@@ -374,7 +369,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
374
369
|
assert conn.other_closed?
|
375
370
|
end
|
376
371
|
|
377
|
-
|
372
|
+
it 'allow nil so it does not execute a close' do
|
378
373
|
@gene_pool = GenePool.new(:close_proc => nil) do
|
379
374
|
DummyConnection.new(0, 0)
|
380
375
|
end
|
@@ -385,7 +380,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
385
380
|
assert !conn.other_closed?
|
386
381
|
end
|
387
382
|
|
388
|
-
|
383
|
+
it 'allow proc that gets executed on close' do
|
389
384
|
foo = 1
|
390
385
|
close_conn = nil
|
391
386
|
my_close = lambda do |conn|
|
@@ -405,8 +400,8 @@ class GenePoolTest < Test::Unit::TestCase
|
|
405
400
|
end
|
406
401
|
end
|
407
402
|
|
408
|
-
|
409
|
-
|
403
|
+
describe 'timeout' do
|
404
|
+
before do
|
410
405
|
@timeout_classes = [nil, MyTimeoutError]
|
411
406
|
@gene_pools = @timeout_classes.map do |timeout_class|
|
412
407
|
GenePool.new(:name => 'TestGenePool',
|
@@ -419,7 +414,7 @@ class GenePoolTest < Test::Unit::TestCase
|
|
419
414
|
end
|
420
415
|
end
|
421
416
|
|
422
|
-
|
417
|
+
it 'timeout when the timeout period has expired' do
|
423
418
|
@gene_pools.each_with_index do |gene_pool, class_index|
|
424
419
|
pool_size = gene_pool.pool_size
|
425
420
|
# Do it with new connections and old connections
|
metadata
CHANGED
@@ -1,65 +1,67 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gene_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brad Pardee
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - '>='
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
14
|
+
name: concurrent-ruby
|
20
15
|
requirement: !ruby/object:Gem::Requirement
|
21
16
|
requirements:
|
22
|
-
- -
|
17
|
+
- - ">="
|
23
18
|
- !ruby/object:Gem::Version
|
24
|
-
version: '0'
|
25
|
-
prerelease: false
|
19
|
+
version: '1.0'
|
26
20
|
type: :runtime
|
27
|
-
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
description: Threadsafe, performant library for managing pools of resources, such
|
28
|
+
as connections.
|
28
29
|
email:
|
29
30
|
- bradpardee@gmail.com
|
30
31
|
executables: []
|
31
32
|
extensions: []
|
32
33
|
extra_rdoc_files: []
|
33
34
|
files:
|
34
|
-
-
|
35
|
-
- LICENSE.txt
|
36
|
-
- Rakefile
|
35
|
+
- CHANGELOG.md
|
37
36
|
- Gemfile
|
38
|
-
-
|
37
|
+
- LICENSE.txt
|
39
38
|
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/gene_pool.rb
|
40
41
|
- test/gene_pool_test.rb
|
41
42
|
homepage: http://github.com/bpardee/gene_pool
|
42
|
-
licenses:
|
43
|
+
licenses:
|
44
|
+
- MIT
|
43
45
|
metadata: {}
|
44
|
-
post_install_message:
|
46
|
+
post_install_message:
|
45
47
|
rdoc_options: []
|
46
48
|
require_paths:
|
47
49
|
- lib
|
48
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|
49
51
|
requirements:
|
50
|
-
- -
|
52
|
+
- - ">="
|
51
53
|
- !ruby/object:Gem::Version
|
52
54
|
version: '0'
|
53
55
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
56
|
requirements:
|
55
|
-
- -
|
57
|
+
- - ">="
|
56
58
|
- !ruby/object:Gem::Version
|
57
59
|
version: '0'
|
58
60
|
requirements: []
|
59
|
-
rubyforge_project:
|
60
|
-
rubygems_version: 2.1
|
61
|
-
signing_key:
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.5.2.1
|
63
|
+
signing_key:
|
62
64
|
specification_version: 4
|
63
|
-
summary:
|
65
|
+
summary: Highly performant Ruby connection pooling library.
|
64
66
|
test_files:
|
65
67
|
- test/gene_pool_test.rb
|