connection_pool 1.1.0 → 1.2.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: eead858733d8dbd69d9ce236f18977ea847b3a83
4
+ data.tar.gz: a686e2981051d33b6b170d2f7aac5709a78bb0cc
5
+ SHA512:
6
+ metadata.gz: 268fe355fb087c14e1820ae8e3f5d903ddf6d2bc9b762091df9e14daa4a20ebf162766f54185c03857ca752f6f15e31a60b4e18c434798950c563a9b17865a73
7
+ data.tar.gz: 9cc05e0c0b554dd131add7a31b328a85d768141be6e250d34472dc8db7bdd1a46f7fca30cf71fc5edb4b33d1d24403a98bc71a3696dec896918f8af05d8e0653
data/Changes.md CHANGED
@@ -1,3 +1,13 @@
1
+ 1.2.0
2
+ -----
3
+
4
+ - Add `with(options)` and `checkout(options)`. [mattcamuto]
5
+ Allows the caller to override the pool timeout.
6
+ ```ruby
7
+ @pool.with(:timeout => 2) do |conn|
8
+ end
9
+ ```
10
+
1
11
  1.1.0
2
12
  -----
3
13
 
data/Gemfile CHANGED
@@ -1,7 +1,3 @@
1
- source "http://rubygems.org"
1
+ source "https://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in connection_pool.gemspec
4
- gemspec
5
-
6
- gem 'rake'
7
- gem 'minitest'
3
+ gemspec(development_group: :runtime)
data/README.md CHANGED
@@ -42,6 +42,19 @@ If all the objects in the connection pool are in use, `with` will block
42
42
  until one becomes available. If no object is available within `:timeout` seconds,
43
43
  `with` will raise a `Timeout::Error`.
44
44
 
45
+ Optionally, you can specify a timeout override using the with-block semantics:
46
+
47
+ ``` ruby
48
+ @memcached.with(:timeout => 2.0) do |dalli|
49
+ dalli.get('some-count')
50
+ end
51
+ ```
52
+
53
+ This will only modify the resource-get timeout for this particular invocation. This
54
+ is useful if you want to fail-fast on certain non critical sections when a resource
55
+ is not available, or conversely if you are comfortable blocking longer on a particular
56
+ resource. This is not implemented in the below `ConnectionPool::Wrapper` class.
57
+
45
58
  You can use `ConnectionPool::Wrapper` to wrap a single global connection, making
46
59
  it easier to port your connection code over time:
47
60
 
@@ -5,8 +5,8 @@ Gem::Specification.new do |s|
5
5
  s.name = "connection_pool"
6
6
  s.version = ConnectionPool::VERSION
7
7
  s.platform = Gem::Platform::RUBY
8
- s.authors = ["Mike Perham"]
9
- s.email = ["mperham@gmail.com"]
8
+ s.authors = ["Mike Perham", "Damian Janowski"]
9
+ s.email = ["mperham@gmail.com", "damian@educabilia.com"]
10
10
  s.homepage = "https://github.com/mperham/connection_pool"
11
11
  s.description = s.summary = %q{Generic connection pool for Ruby}
12
12
 
@@ -16,4 +16,5 @@ Gem::Specification.new do |s|
16
16
  s.require_paths = ["lib"]
17
17
  s.license = "MIT"
18
18
  s.add_development_dependency 'minitest', '>= 5.0.0'
19
+ s.add_development_dependency 'rake'
19
20
  end
@@ -12,6 +12,12 @@ require 'connection_pool/timed_stack'
12
12
  # redis.lpop('my-list') if redis.llen('my-list') > 0
13
13
  # end
14
14
  #
15
+ # Using optional timeout override (for that single invocation)
16
+ #
17
+ # @pool.with(:timeout => 2.0) do |redis|
18
+ # redis.lpop('my-list') if redis.llen('my-list') > 0
19
+ # end
20
+ #
15
21
  # Example usage replacing an existing connection (slower):
16
22
  #
17
23
  # $redis = ConnectionPool.wrap { Redis.new }
@@ -43,8 +49,8 @@ class ConnectionPool
43
49
  @key = :"current-#{@available.object_id}"
44
50
  end
45
51
 
46
- def with
47
- conn = checkout
52
+ def with(options = {})
53
+ conn = checkout(options)
48
54
  begin
49
55
  yield conn
50
56
  ensure
@@ -52,11 +58,12 @@ class ConnectionPool
52
58
  end
53
59
  end
54
60
 
55
- def checkout
61
+ def checkout(options = {})
56
62
  stack = ::Thread.current[@key] ||= []
57
63
 
58
64
  if stack.empty?
59
- conn = @available.pop(@timeout)
65
+ timeout = options[:timeout] || @timeout
66
+ conn = @available.pop(timeout)
60
67
  else
61
68
  conn = stack.last
62
69
  end
@@ -79,7 +86,7 @@ class ConnectionPool
79
86
  end
80
87
 
81
88
  class Wrapper < ::BasicObject
82
- METHODS = [:with]
89
+ METHODS = [:with, :pool_shutdown]
83
90
 
84
91
  def initialize(options = {}, &block)
85
92
  @pool = ::ConnectionPool.new(options, &block)
@@ -91,6 +98,10 @@ class ConnectionPool
91
98
  @pool.checkin
92
99
  end
93
100
 
101
+ def pool_shutdown(&block)
102
+ @pool.shutdown(&block)
103
+ end
104
+
94
105
  def respond_to?(id, *args)
95
106
  METHODS.include?(id) || @pool.with { |c| c.respond_to?(id, *args) }
96
107
  end
@@ -102,5 +113,3 @@ class ConnectionPool
102
113
  end
103
114
  end
104
115
  end
105
-
106
- require_relative 'connection_pool/timed_stack'
@@ -4,6 +4,7 @@ require 'timeout'
4
4
  class ConnectionPool::PoolShuttingDownError < RuntimeError; end
5
5
 
6
6
  class ConnectionPool::TimedStack
7
+
7
8
  def initialize(size = 0)
8
9
  @que = Array.new(size) { yield }
9
10
  @mutex = Mutex.new
@@ -55,10 +56,6 @@ class ConnectionPool::TimedStack
55
56
  @que.empty?
56
57
  end
57
58
 
58
- def clear
59
- @que.clear
60
- end
61
-
62
59
  def length
63
60
  @que.length
64
61
  end
@@ -1,3 +1,3 @@
1
1
  class ConnectionPool
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -1,15 +1,6 @@
1
- require 'rubygems'
2
1
  require 'minitest/pride'
3
2
  require 'minitest/autorun'
4
3
 
5
- require 'connection_pool'
4
+ $VERBOSE = 1
6
5
 
7
- class Minitest::Unit::TestCase
8
-
9
- def async_test(time=0.5)
10
- q = TimedQueue.new
11
- yield Proc.new { q << nil }
12
- q.timed_pop(time)
13
- end
14
-
15
- end
6
+ require_relative '../lib/connection_pool'
@@ -77,7 +77,39 @@ class TestConnectionPool < Minitest::Test
77
77
  refute_nil conn
78
78
  end
79
79
  end
80
+
81
+ def test_with_timeout_override
82
+ pool = ConnectionPool.new(:timeout => 0.05, :size => 1) { NetworkConnection.new }
83
+ Thread.new do
84
+ pool.with do |net|
85
+ net.do_something
86
+ sleep 0.3
87
+ end
88
+ end
89
+ sleep 0.05
90
+ assert_raises Timeout::Error do
91
+ pool.with { |net| net.do_something }
92
+ end
93
+ pool.with(:timeout => 0.4) do |conn|
94
+ refute_nil conn
95
+ end
96
+ end
80
97
 
98
+ def test_checkout_timeout_override
99
+ pool = ConnectionPool.new(:timeout => 0.05, :size => 1) { NetworkConnection.new }
100
+ Thread.new do
101
+ pool.with do |net|
102
+ net.do_something
103
+ sleep 0.2
104
+ end
105
+ end
106
+ sleep 0.05
107
+ assert_raises Timeout::Error do
108
+ pool.checkout
109
+ end
110
+ assert pool.checkout :timeout => 0.3
111
+ end
112
+
81
113
  def test_passthru
82
114
  pool = ConnectionPool.wrap(:timeout => 0.1, :size => 1) { NetworkConnection.new }
83
115
  assert_equal 1, pool.do_something
@@ -199,4 +231,18 @@ class TestConnectionPool < Minitest::Test
199
231
  pool.shutdown
200
232
  end
201
233
  end
234
+
235
+ def test_shutdown_is_executed_for_all_connections_in_wrapped_pool
236
+ recorders = []
237
+
238
+ wrapper = ConnectionPool::Wrapper.new(:size => 3) do
239
+ Recorder.new.tap { |r| recorders << r }
240
+ end
241
+
242
+ wrapper.pool_shutdown do |recorder|
243
+ recorder.do_work("shutdown")
244
+ end
245
+
246
+ assert_equal [["shutdown"]] * 3, recorders.map { |r| r.calls }
247
+ end
202
248
  end
metadata CHANGED
@@ -1,35 +1,48 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
5
- prerelease:
4
+ version: 1.2.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mike Perham
8
+ - Damian Janowski
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-06 00:00:00.000000000 Z
12
+ date: 2013-11-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
16
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
17
  requirements:
19
- - - ! '>='
18
+ - - '>='
20
19
  - !ruby/object:Gem::Version
21
20
  version: 5.0.0
22
21
  type: :development
23
22
  prerelease: false
24
23
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
- - - ! '>='
25
+ - - '>='
28
26
  - !ruby/object:Gem::Version
29
27
  version: 5.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
30
42
  description: Generic connection pool for Ruby
31
43
  email:
32
44
  - mperham@gmail.com
45
+ - damian@educabilia.com
33
46
  executables: []
34
47
  extensions: []
35
48
  extra_rdoc_files: []
@@ -49,27 +62,26 @@ files:
49
62
  homepage: https://github.com/mperham/connection_pool
50
63
  licenses:
51
64
  - MIT
65
+ metadata: {}
52
66
  post_install_message:
53
67
  rdoc_options: []
54
68
  require_paths:
55
69
  - lib
56
70
  required_ruby_version: !ruby/object:Gem::Requirement
57
- none: false
58
71
  requirements:
59
- - - ! '>='
72
+ - - '>='
60
73
  - !ruby/object:Gem::Version
61
74
  version: '0'
62
75
  required_rubygems_version: !ruby/object:Gem::Requirement
63
- none: false
64
76
  requirements:
65
- - - ! '>='
77
+ - - '>='
66
78
  - !ruby/object:Gem::Version
67
79
  version: '0'
68
80
  requirements: []
69
81
  rubyforge_project:
70
- rubygems_version: 1.8.23
82
+ rubygems_version: 2.1.9
71
83
  signing_key:
72
- specification_version: 3
84
+ specification_version: 4
73
85
  summary: Generic connection pool for Ruby
74
86
  test_files:
75
87
  - test/helper.rb