connection_pool 2.2.3 → 2.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9fefa40cd9db0f5add54482a9f980d808b5bb578e83a5a8bf287c636c41704d
4
- data.tar.gz: ec25d36c42cfb863e768bf49b18316d955397edaa8862f92d27687ab2a089bf0
3
+ metadata.gz: 0af12e8d2f551b932ee863a1385ddc9a3f3140fff886cacaca25298a4c21d218
4
+ data.tar.gz: 16bd7b78d7b35337d35befe063ceeb2cb749dfc9e68cd373e7f76bd856f6cba3
5
5
  SHA512:
6
- metadata.gz: d8653437078b6334be998d16f4851e12a21e8caca8338a07559c9bfbe646dc6023a7370aad6e33be905b05d6eaab8f249387eaa72b16a478f57c9c497adbbf30
7
- data.tar.gz: 2a437d085a3f11376338ee32debe5e7ff38fc28fdc8d0d318287c2890bc90ce2d282468981dde43c8391798d113e1391d7998a5f5e571f1021ae99dfff6893e2
6
+ metadata.gz: edb345021997307fe736408ad3e3cbebd2ce86fcbdc636fe2d4f03d61258ecea22285a4f91e854d43631533513d6500c9f6cf2094f52d6756a2d61ee2daba256
7
+ data.tar.gz: d58d7519b9d4be9bcf7b0628e2aa9d937df2d61d0d1a9423b43618685adf242810256b9decc22bca90cb3ee3eec88e8eabffb409aa9e49f28d56815f26056b81
@@ -0,0 +1,21 @@
1
+ name: CI
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ test:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby: ["2.4", "2.5", "2.6", "2.7", "3.0", "jruby", "truffleruby"]
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - uses: ruby/setup-ruby@v1
15
+ with:
16
+ ruby-version: ${{matrix.ruby}}
17
+ bundler-cache: true
18
+
19
+ - name: Run tests
20
+ timeout-minutes: 5
21
+ run: ${{matrix.env}} bundle exec rake
data/Changes.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # connection_pool Changelog
2
2
 
3
+ 2.2.4
4
+ ------
5
+
6
+ - Add `reload` to close all connections, recreating them afterwards [Andrew Marshall, #140]
7
+ - Add `then` as a way to use a pool or a bare connection with the same code path [#138]
8
+
3
9
  2.2.3
4
10
  ------
5
11
 
data/Gemfile CHANGED
@@ -1,5 +1,3 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gemspec(development_group: :runtime)
4
-
5
- gem "standard"
data/README.md CHANGED
@@ -31,6 +31,14 @@ If all the objects in the connection pool are in use, `with` will block
31
31
  until one becomes available. If no object is available within `:timeout` seconds,
32
32
  `with` will raise a `Timeout::Error`.
33
33
 
34
+ You can also use `ConnectionPool#then` to support _both_ a
35
+ connection pool and a raw client (requires Ruby 2.5+).
36
+
37
+ ```ruby
38
+ # Compatible with a raw Redis::Client, and ConnectionPool Redis
39
+ $redis.then { |r| r.set 'foo' 'bar' }
40
+ ```
41
+
34
42
  Optionally, you can specify a timeout override using the with-block semantics:
35
43
 
36
44
  ``` ruby
@@ -87,6 +95,22 @@ Shutting down a connection pool will block until all connections are checked in
87
95
  **Note that shutting down is completely optional**; Ruby's garbage collector will reclaim
88
96
  unreferenced pools under normal circumstances.
89
97
 
98
+ ## Reload
99
+
100
+ You can reload a ConnectionPool instance in the case it is desired to close all
101
+ connections to the pool and, unlike `shutdown`, afterwards recreate connections
102
+ so the pool may continue to be used. Reloading may be useful after forking the
103
+ process.
104
+
105
+ ```ruby
106
+ cp = ConnectionPool.new { Redis.new }
107
+ cp.reload { |conn| conn.quit }
108
+ cp.with { |conn| conn.get('some-count') }
109
+ ```
110
+
111
+ Like `shutdown`, this will block until all connections are checked in and
112
+ closed.
113
+
90
114
  ## Current State
91
115
 
92
116
  There are several methods that return information about a pool.
@@ -107,15 +131,15 @@ Notes
107
131
 
108
132
  - Connections are lazily created as needed.
109
133
  - There is no provision for repairing or checking the health of a connection;
110
- connections should be self-repairing. This is true of the Dalli and Redis
134
+ connections should be self-repairing. This is true of the Dalli and Redis
111
135
  clients.
112
136
  - **WARNING**: Don't ever use `Timeout.timeout` in your Ruby code or you will see
113
- occasional silent corruption and mysterious errors. The Timeout API is unsafe
114
- and cannot be used correctly, ever. Use proper socket timeout options as
137
+ occasional silent corruption and mysterious errors. The Timeout API is unsafe
138
+ and cannot be used correctly, ever. Use proper socket timeout options as
115
139
  exposed by Net::HTTP, Redis, Dalli, etc.
116
140
 
117
141
 
118
142
  Author
119
143
  ------
120
144
 
121
- Mike Perham, [@mperham](https://twitter.com/mperham), <http://mikeperham.com>
145
+ Mike Perham, [@getajobmike](https://twitter.com/getajobmike), <https://www.mikeperham.com>
data/Rakefile CHANGED
@@ -1,7 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
3
  require "rake/testtask"
4
- require "standard/rake"
5
4
  Rake::TestTask.new
6
5
 
7
6
  task default: :test
@@ -17,4 +17,5 @@ Gem::Specification.new do |s|
17
17
  s.add_development_dependency "bundler"
18
18
  s.add_development_dependency "minitest", ">= 5.0.0"
19
19
  s.add_development_dependency "rake"
20
+ s.required_ruby_version = ">= 2.2.0"
20
21
  end
@@ -67,6 +67,7 @@ class ConnectionPool
67
67
  end
68
68
  end
69
69
  end
70
+ alias then with
70
71
 
71
72
  def checkout(options = {})
72
73
  if ::Thread.current[@key]
@@ -83,6 +84,7 @@ class ConnectionPool
83
84
  if ::Thread.current[@key_count] == 1
84
85
  @available.push(::Thread.current[@key])
85
86
  ::Thread.current[@key] = nil
87
+ ::Thread.current[@key_count] = nil
86
88
  else
87
89
  ::Thread.current[@key_count] -= 1
88
90
  end
@@ -93,10 +95,24 @@ class ConnectionPool
93
95
  nil
94
96
  end
95
97
 
98
+ ##
99
+ # Shuts down the ConnectionPool by passing each connection to +block+ and
100
+ # then removing it from the pool. Attempting to checkout a connection after
101
+ # shutdown will raise +ConnectionPool::PoolShuttingDownError+.
102
+
96
103
  def shutdown(&block)
97
104
  @available.shutdown(&block)
98
105
  end
99
106
 
107
+ ##
108
+ # Reloads the ConnectionPool by passing each connection to +block+ and then
109
+ # removing it the pool. Subsequent checkouts will create new connections as
110
+ # needed.
111
+
112
+ def reload(&block)
113
+ @available.shutdown(reload: true, &block)
114
+ end
115
+
100
116
  # Size of this connection pool
101
117
  attr_reader :size
102
118
 
@@ -15,7 +15,7 @@
15
15
  #
16
16
  # conn = ts.pop
17
17
  # ts.pop timeout: 5
18
- # #=> raises Timeout::Error after 5 seconds
18
+ # #=> raises ConnectionPool::TimeoutError after 5 seconds
19
19
 
20
20
  class ConnectionPool::TimedStack
21
21
  attr_reader :max
@@ -54,7 +54,7 @@ class ConnectionPool::TimedStack
54
54
  ##
55
55
  # Retrieves a connection from the stack. If a connection is available it is
56
56
  # immediately returned. If no connection is available within the given
57
- # timeout a Timeout::Error is raised.
57
+ # timeout a ConnectionPool::TimeoutError is raised.
58
58
  #
59
59
  # +:timeout+ is the only checked entry in +options+ and is preferred over
60
60
  # the +timeout+ argument (which will be removed in a future release). Other
@@ -81,10 +81,12 @@ class ConnectionPool::TimedStack
81
81
  end
82
82
 
83
83
  ##
84
- # Shuts down the TimedStack which prevents connections from being checked
85
- # out. The +block+ is called once for each connection on the stack.
84
+ # Shuts down the TimedStack by passing each connection to +block+ and then
85
+ # removing it from the pool. Attempting to checkout a connection after
86
+ # shutdown will raise +ConnectionPool::PoolShuttingDownError+ unless
87
+ # +:reload+ is +true+.
86
88
 
87
- def shutdown(&block)
89
+ def shutdown(reload: false, &block)
88
90
  raise ArgumentError, "shutdown must receive a block" unless block_given?
89
91
 
90
92
  @mutex.synchronize do
@@ -92,6 +94,7 @@ class ConnectionPool::TimedStack
92
94
  @resource.broadcast
93
95
 
94
96
  shutdown_connections
97
+ @shutdown_block = nil if reload
95
98
  end
96
99
  end
97
100
 
@@ -143,6 +146,7 @@ class ConnectionPool::TimedStack
143
146
  conn = fetch_connection(options)
144
147
  @shutdown_block.call(conn)
145
148
  end
149
+ @created = 0
146
150
  end
147
151
 
148
152
  ##
@@ -1,3 +1,3 @@
1
1
  class ConnectionPool
2
- VERSION = "2.2.3"
2
+ VERSION = "2.2.4"
3
3
  end
@@ -32,9 +32,17 @@ class ConnectionPool
32
32
 
33
33
  # rubocop:disable Style/MethodMissingSuper
34
34
  # rubocop:disable Style/MissingRespondToMissing
35
- def method_missing(name, *args, &block)
36
- with do |connection|
37
- connection.send(name, *args, &block)
35
+ if ::RUBY_VERSION >= "2.7.0"
36
+ def method_missing(name, *args, **kwargs, &block)
37
+ with do |connection|
38
+ connection.send(name, *args, **kwargs, &block)
39
+ end
40
+ end
41
+ else
42
+ def method_missing(name, *args, &block)
43
+ with do |connection|
44
+ connection.send(name, *args, &block)
45
+ end
38
46
  end
39
47
  end
40
48
  # rubocop:enable Style/MethodMissingSuper
@@ -8,8 +8,8 @@ class TestConnectionPool < Minitest::Test
8
8
  @x = 0
9
9
  end
10
10
 
11
- def do_something
12
- @x += 1
11
+ def do_something(*_args, increment: 1)
12
+ @x += increment
13
13
  sleep SLEEP_TIME
14
14
  @x
15
15
  end
@@ -117,6 +117,12 @@ class TestConnectionPool < Minitest::Test
117
117
  assert Thread.new { pool.checkout }.join
118
118
  end
119
119
 
120
+ def test_then
121
+ pool = ConnectionPool.new { Object.new }
122
+
123
+ assert_equal pool.method(:then), pool.method(:with)
124
+ end
125
+
120
126
  def test_with_timeout
121
127
  pool = ConnectionPool.new(timeout: 0, size: 1) { Object.new }
122
128
 
@@ -326,6 +332,7 @@ class TestConnectionPool < Minitest::Test
326
332
  assert_equal 2, pool.do_something
327
333
  assert_equal 5, pool.do_something_with_block { 3 }
328
334
  assert_equal 6, pool.with { |net| net.fast }
335
+ assert_equal 8, pool.do_something(increment: 2)
329
336
  end
330
337
 
331
338
  def test_passthru_respond_to
@@ -102,6 +102,16 @@ class TestConnectionPoolTimedStack < Minitest::Test
102
102
  end
103
103
  end
104
104
 
105
+ def test_pop_shutdown_reload
106
+ stack = ConnectionPool::TimedStack.new(1) { Object.new }
107
+ object = stack.pop
108
+ stack.push(object)
109
+
110
+ stack.shutdown(reload: true) {}
111
+
112
+ refute_equal object, stack.pop
113
+ end
114
+
105
115
  def test_push
106
116
  stack = ConnectionPool::TimedStack.new(1) { Object.new }
107
117
 
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: connection_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.3
4
+ version: 2.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Perham
8
8
  - Damian Janowski
9
- autorequire:
9
+ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-06-02 00:00:00.000000000 Z
12
+ date: 2021-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -61,8 +61,8 @@ executables: []
61
61
  extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
+ - ".github/workflows/ci.yml"
64
65
  - ".gitignore"
65
- - ".travis.yml"
66
66
  - Changes.md
67
67
  - Gemfile
68
68
  - LICENSE
@@ -80,7 +80,7 @@ homepage: https://github.com/mperham/connection_pool
80
80
  licenses:
81
81
  - MIT
82
82
  metadata: {}
83
- post_install_message:
83
+ post_install_message:
84
84
  rdoc_options: []
85
85
  require_paths:
86
86
  - lib
@@ -88,15 +88,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
88
88
  requirements:
89
89
  - - ">="
90
90
  - !ruby/object:Gem::Version
91
- version: '0'
91
+ version: 2.2.0
92
92
  required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 3.1.2
99
- signing_key:
98
+ rubygems_version: 3.2.3
99
+ signing_key:
100
100
  specification_version: 4
101
101
  summary: Generic connection pool for Ruby
102
102
  test_files:
data/.travis.yml DELETED
@@ -1,12 +0,0 @@
1
- ---
2
- cache: bundler
3
- language: ruby
4
- rvm:
5
- - 2.3
6
- - 2.4
7
- - 2.5
8
- - 2.6
9
- - 2.7
10
- - jruby
11
- jdk:
12
- - openjdk11