connection_pool 2.2.3 → 2.2.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +26 -0
- data/Changes.md +11 -0
- data/Gemfile +0 -2
- data/README.md +29 -5
- 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 +17 -3
- data/test/test_connection_pool.rb +16 -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: '0993b72c233b027a1229d532a126b4a1623ef3f146a7b56502c539084f9a228d'
|
4
|
+
data.tar.gz: 274efa04fc445ca0044f62da29484a4ee9fb5e02b5f213fb873fedfd63cadff0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 17c5bea167386115e8672b394138e0f66b55e6371b803dcfee7be08e51c84353aa9cd0257f169ae04053bc95be6b0c9b06576579f4915e68271146b1c9d602a5
|
7
|
+
data.tar.gz: 4eeccb9eaf397e8e386a41f81005b10d43d7441297661ab0d7656b6be30ac7ff1e751a95ed5c5b5a412124a29a0af3f5c90ae2882d70d3fa29d9dc8b2df412e3
|
@@ -0,0 +1,26 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
continue-on-error: ${{ matrix.experimental }}
|
9
|
+
strategy:
|
10
|
+
fail-fast: false
|
11
|
+
matrix:
|
12
|
+
ruby: ["2.4", "2.5", "2.6", "2.7", "3.0", "jruby"]
|
13
|
+
experimental: [false]
|
14
|
+
include:
|
15
|
+
- ruby: "truffleruby"
|
16
|
+
experimental: true
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- uses: ruby/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
ruby-version: ${{matrix.ruby}}
|
22
|
+
bundler-cache: true
|
23
|
+
|
24
|
+
- name: Run tests
|
25
|
+
timeout-minutes: 5
|
26
|
+
run: ${{matrix.env}} bundle exec rake
|
data/Changes.md
CHANGED
@@ -1,5 +1,16 @@
|
|
1
1
|
# connection_pool Changelog
|
2
2
|
|
3
|
+
2.2.5
|
4
|
+
------
|
5
|
+
|
6
|
+
- Fix argument forwarding on Ruby 2.7 [#149]
|
7
|
+
|
8
|
+
2.2.4
|
9
|
+
------
|
10
|
+
|
11
|
+
- Add `reload` to close all connections, recreating them afterwards [Andrew Marshall, #140]
|
12
|
+
- Add `then` as a way to use a pool or a bare connection with the same code path [#138]
|
13
|
+
|
3
14
|
2.2.3
|
4
15
|
------
|
5
16
|
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
connection\_pool
|
2
2
|
=================
|
3
|
-
[![Build Status](https://
|
3
|
+
[![Build Status](https://github.com/mperham/connection_pool/actions/workflows/ci.yml/badge.svg)](https://github.com/mperham/connection_pool/actions/workflows/ci.yml)
|
4
4
|
|
5
5
|
Generic connection pooling for Ruby.
|
6
6
|
|
@@ -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,23 @@ class ConnectionPool
|
|
32
32
|
|
33
33
|
# rubocop:disable Style/MethodMissingSuper
|
34
34
|
# rubocop:disable Style/MissingRespondToMissing
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
if ::RUBY_VERSION >= "3.0.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
|
+
elsif ::RUBY_VERSION >= "2.7.0"
|
42
|
+
ruby2_keywords def method_missing(name, *args, &block)
|
43
|
+
with do |connection|
|
44
|
+
connection.send(name, *args, &block)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
else
|
48
|
+
def method_missing(name, *args, &block)
|
49
|
+
with do |connection|
|
50
|
+
connection.send(name, *args, &block)
|
51
|
+
end
|
38
52
|
end
|
39
53
|
end
|
40
54
|
# rubocop:enable Style/MethodMissingSuper
|
@@ -8,8 +8,14 @@ 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
|
+
sleep SLEEP_TIME
|
14
|
+
@x
|
15
|
+
end
|
16
|
+
|
17
|
+
def do_something_with_positional_hash(options)
|
18
|
+
@x += options[:increment] || 1
|
13
19
|
sleep SLEEP_TIME
|
14
20
|
@x
|
15
21
|
end
|
@@ -117,6 +123,12 @@ class TestConnectionPool < Minitest::Test
|
|
117
123
|
assert Thread.new { pool.checkout }.join
|
118
124
|
end
|
119
125
|
|
126
|
+
def test_then
|
127
|
+
pool = ConnectionPool.new { Object.new }
|
128
|
+
|
129
|
+
assert_equal pool.method(:then), pool.method(:with)
|
130
|
+
end
|
131
|
+
|
120
132
|
def test_with_timeout
|
121
133
|
pool = ConnectionPool.new(timeout: 0, size: 1) { Object.new }
|
122
134
|
|
@@ -326,6 +338,8 @@ class TestConnectionPool < Minitest::Test
|
|
326
338
|
assert_equal 2, pool.do_something
|
327
339
|
assert_equal 5, pool.do_something_with_block { 3 }
|
328
340
|
assert_equal 6, pool.with { |net| net.fast }
|
341
|
+
assert_equal 8, pool.do_something(increment: 2)
|
342
|
+
assert_equal 10, pool.do_something_with_positional_hash({ increment: 2, symbol_key: 3, "string_key" => 4 })
|
329
343
|
end
|
330
344
|
|
331
345
|
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.5
|
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-14 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.1.
|
99
|
-
signing_key:
|
98
|
+
rubygems_version: 3.1.4
|
99
|
+
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: Generic connection pool for Ruby
|
102
102
|
test_files:
|