connection_pool 2.2.3 → 2.2.4
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.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +21 -0
- data/Changes.md +6 -0
- data/Gemfile +0 -2
- data/README.md +28 -4
- data/Rakefile +0 -1
- data/connection_pool.gemspec +1 -0
- data/lib/connection_pool.rb +16 -0
- data/lib/connection_pool/timed_stack.rb +9 -5
- data/lib/connection_pool/version.rb +1 -1
- data/lib/connection_pool/wrapper.rb +11 -3
- data/test/test_connection_pool.rb +9 -2
- data/test/test_connection_pool_timed_stack.rb +10 -0
- metadata +8 -8
- data/.travis.yml +0 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0af12e8d2f551b932ee863a1385ddc9a3f3140fff886cacaca25298a4c21d218
|
4
|
+
data.tar.gz: 16bd7b78d7b35337d35befe063ceeb2cb749dfc9e68cd373e7f76bd856f6cba3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
data/Gemfile
CHANGED
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.
|
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.
|
114
|
-
and cannot be used correctly, ever.
|
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, [@
|
145
|
+
Mike Perham, [@getajobmike](https://twitter.com/getajobmike), <https://www.mikeperham.com>
|
data/Rakefile
CHANGED
data/connection_pool.gemspec
CHANGED
data/lib/connection_pool.rb
CHANGED
@@ -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
|
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
|
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
|
85
|
-
#
|
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
|
##
|
@@ -32,9 +32,17 @@ class ConnectionPool
|
|
32
32
|
|
33
33
|
# rubocop:disable Style/MethodMissingSuper
|
34
34
|
# rubocop:disable Style/MissingRespondToMissing
|
35
|
-
|
36
|
-
|
37
|
-
|
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 +=
|
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.
|
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:
|
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:
|
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.
|
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:
|