connection_pool 2.5.0 → 2.5.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/Changes.md +22 -0
- data/README.md +21 -0
- data/lib/connection_pool/timed_stack.rb +26 -23
- data/lib/connection_pool/version.rb +1 -1
- data/lib/connection_pool.rb +50 -6
- metadata +3 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e6266fe91b9d1e5285e6e4cd873b0721b18b2c49140fff54b43c9df4d5acd7b1
|
4
|
+
data.tar.gz: cc95e0178a26a01d9fc3933d8b65468d203c7fb6ed415312f86e392551659db8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9641879d84cc20db729d1780283fca3a0328e957502369bd22dbbdd0f6601974204acdd8dcd1929e79da505402b73f3fe5eaf2842cfe64d542640ab1e70ad21c
|
7
|
+
data.tar.gz: 34afe9f57cbaff9b08066725964d776e81e1e0e484eccd54c361cc2a77fe24fbda181c9576754367515799f24f8095504ce2b4c8718f37a90b75c559782be0b4
|
data/Changes.md
CHANGED
@@ -1,5 +1,27 @@
|
|
1
1
|
# connection_pool Changelog
|
2
2
|
|
3
|
+
2.5.4
|
4
|
+
------
|
5
|
+
|
6
|
+
- Add ability to remove a broken connection from the pool [#204, womblep]
|
7
|
+
|
8
|
+
2.5.3
|
9
|
+
------
|
10
|
+
|
11
|
+
- Fix TruffleRuby/JRuby crash [#201]
|
12
|
+
|
13
|
+
2.5.2
|
14
|
+
------
|
15
|
+
|
16
|
+
- Rollback inadvertant change to `auto_reload_after_fork` default. [#200]
|
17
|
+
|
18
|
+
2.5.1
|
19
|
+
------
|
20
|
+
|
21
|
+
- Pass options to TimedStack in `checkout` [#195]
|
22
|
+
- Optimize connection lookup [#196]
|
23
|
+
- Fixes for use with Ractors
|
24
|
+
|
3
25
|
2.5.0
|
4
26
|
------
|
5
27
|
|
data/README.md
CHANGED
@@ -129,6 +129,27 @@ Thread.new do
|
|
129
129
|
end
|
130
130
|
```
|
131
131
|
|
132
|
+
## Discarding Connections
|
133
|
+
|
134
|
+
You can discard connections in the ConnectionPool instance to remove connections that are broken and can't be restarted.
|
135
|
+
|
136
|
+
NOTE: the connection is not closed. It will just be removed from the pool so it won't be selected again.
|
137
|
+
|
138
|
+
It can only be done inside the block passed to `with` or `with_timeout`.
|
139
|
+
|
140
|
+
Takes an optional block that will be executed with the connection.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
pool.with do |conn|
|
144
|
+
begin
|
145
|
+
conn.execute("SELECT 1")
|
146
|
+
rescue SomeConnectionError
|
147
|
+
pool.discard_current_connection # remove the connection from the pool
|
148
|
+
raise
|
149
|
+
end
|
150
|
+
end
|
151
|
+
```
|
152
|
+
|
132
153
|
## Current State
|
133
154
|
|
134
155
|
There are several methods that return information about a pool.
|
@@ -1,8 +1,8 @@
|
|
1
1
|
##
|
2
2
|
# The TimedStack manages a pool of homogeneous connections (or any resource
|
3
|
-
# you wish to manage).
|
3
|
+
# you wish to manage). Connections are created lazily up to a given maximum
|
4
4
|
# number.
|
5
|
-
|
5
|
+
#
|
6
6
|
# Examples:
|
7
7
|
#
|
8
8
|
# ts = TimedStack.new(1) { MyConnection.new }
|
@@ -16,14 +16,12 @@
|
|
16
16
|
# conn = ts.pop
|
17
17
|
# ts.pop timeout: 5
|
18
18
|
# #=> raises ConnectionPool::TimeoutError after 5 seconds
|
19
|
-
|
20
19
|
class ConnectionPool::TimedStack
|
21
20
|
attr_reader :max
|
22
21
|
|
23
22
|
##
|
24
23
|
# Creates a new pool with +size+ connections that are created from the given
|
25
24
|
# +block+.
|
26
|
-
|
27
25
|
def initialize(size = 0, &block)
|
28
26
|
@create_block = block
|
29
27
|
@created = 0
|
@@ -35,9 +33,8 @@ class ConnectionPool::TimedStack
|
|
35
33
|
end
|
36
34
|
|
37
35
|
##
|
38
|
-
# Returns +obj+ to the stack.
|
36
|
+
# Returns +obj+ to the stack. +options+ is ignored in TimedStack but may be
|
39
37
|
# used by subclasses that extend TimedStack.
|
40
|
-
|
41
38
|
def push(obj, options = {})
|
42
39
|
@mutex.synchronize do
|
43
40
|
if @shutdown_block
|
@@ -53,14 +50,13 @@ class ConnectionPool::TimedStack
|
|
53
50
|
alias_method :<<, :push
|
54
51
|
|
55
52
|
##
|
56
|
-
# Retrieves a connection from the stack.
|
57
|
-
# immediately returned.
|
53
|
+
# Retrieves a connection from the stack. If a connection is available it is
|
54
|
+
# immediately returned. If no connection is available within the given
|
58
55
|
# timeout a ConnectionPool::TimeoutError is raised.
|
59
56
|
#
|
60
57
|
# +:timeout+ is the only checked entry in +options+ and is preferred over
|
61
|
-
# the +timeout+ argument (which will be removed in a future release).
|
58
|
+
# the +timeout+ argument (which will be removed in a future release). Other
|
62
59
|
# options may be used by subclasses that extend TimedStack.
|
63
|
-
|
64
60
|
def pop(timeout = 0.5, options = {})
|
65
61
|
options, timeout = timeout, 0.5 if Hash === timeout
|
66
62
|
timeout = options.fetch :timeout, timeout
|
@@ -69,7 +65,9 @@ class ConnectionPool::TimedStack
|
|
69
65
|
@mutex.synchronize do
|
70
66
|
loop do
|
71
67
|
raise ConnectionPool::PoolShuttingDownError if @shutdown_block
|
72
|
-
|
68
|
+
if (conn = try_fetch_connection(options))
|
69
|
+
return conn
|
70
|
+
end
|
73
71
|
|
74
72
|
connection = try_create(options)
|
75
73
|
return connection if connection
|
@@ -86,7 +84,6 @@ class ConnectionPool::TimedStack
|
|
86
84
|
# removing it from the pool. Attempting to checkout a connection after
|
87
85
|
# shutdown will raise +ConnectionPool::PoolShuttingDownError+ unless
|
88
86
|
# +:reload+ is +true+.
|
89
|
-
|
90
87
|
def shutdown(reload: false, &block)
|
91
88
|
raise ArgumentError, "shutdown must receive a block" unless block
|
92
89
|
|
@@ -121,14 +118,12 @@ class ConnectionPool::TimedStack
|
|
121
118
|
|
122
119
|
##
|
123
120
|
# Returns +true+ if there are no available connections.
|
124
|
-
|
125
121
|
def empty?
|
126
122
|
(@created - @que.length) >= @max
|
127
123
|
end
|
128
124
|
|
129
125
|
##
|
130
126
|
# The number of connections available on the stack.
|
131
|
-
|
132
127
|
def length
|
133
128
|
@max - @created + @que.length
|
134
129
|
end
|
@@ -139,6 +134,12 @@ class ConnectionPool::TimedStack
|
|
139
134
|
@que.length
|
140
135
|
end
|
141
136
|
|
137
|
+
##
|
138
|
+
# Reduce the created count
|
139
|
+
def decrement_created
|
140
|
+
@created -= 1 unless @created == 0
|
141
|
+
end
|
142
|
+
|
142
143
|
private
|
143
144
|
|
144
145
|
def current_time
|
@@ -148,8 +149,17 @@ class ConnectionPool::TimedStack
|
|
148
149
|
##
|
149
150
|
# This is an extension point for TimedStack and is called with a mutex.
|
150
151
|
#
|
151
|
-
# This method must returns
|
152
|
+
# This method must returns a connection from the stack if one exists. Allows
|
153
|
+
# subclasses with expensive match/search algorithms to avoid double-handling
|
154
|
+
# their stack.
|
155
|
+
def try_fetch_connection(options = nil)
|
156
|
+
connection_stored?(options) && fetch_connection(options)
|
157
|
+
end
|
152
158
|
|
159
|
+
##
|
160
|
+
# This is an extension point for TimedStack and is called with a mutex.
|
161
|
+
#
|
162
|
+
# This method must returns true if a connection is available on the stack.
|
153
163
|
def connection_stored?(options = nil)
|
154
164
|
!@que.empty?
|
155
165
|
end
|
@@ -158,7 +168,6 @@ class ConnectionPool::TimedStack
|
|
158
168
|
# This is an extension point for TimedStack and is called with a mutex.
|
159
169
|
#
|
160
170
|
# This method must return a connection from the stack.
|
161
|
-
|
162
171
|
def fetch_connection(options = nil)
|
163
172
|
@que.pop&.first
|
164
173
|
end
|
@@ -167,10 +176,8 @@ class ConnectionPool::TimedStack
|
|
167
176
|
# This is an extension point for TimedStack and is called with a mutex.
|
168
177
|
#
|
169
178
|
# This method must shut down all connections on the stack.
|
170
|
-
|
171
179
|
def shutdown_connections(options = nil)
|
172
|
-
while
|
173
|
-
conn = fetch_connection(options)
|
180
|
+
while (conn = try_fetch_connection(options))
|
174
181
|
@created -= 1 unless @created == 0
|
175
182
|
@shutdown_block.call(conn)
|
176
183
|
end
|
@@ -181,7 +188,6 @@ class ConnectionPool::TimedStack
|
|
181
188
|
#
|
182
189
|
# This method returns the oldest idle connection if it has been idle for more than idle_seconds.
|
183
190
|
# This requires that the stack is kept in order of checked in time (oldest first).
|
184
|
-
|
185
191
|
def reserve_idle_connection(idle_seconds)
|
186
192
|
return unless idle_connections?(idle_seconds)
|
187
193
|
|
@@ -194,7 +200,6 @@ class ConnectionPool::TimedStack
|
|
194
200
|
# This is an extension point for TimedStack and is called with a mutex.
|
195
201
|
#
|
196
202
|
# Returns true if the first connection in the stack has been idle for more than idle_seconds
|
197
|
-
|
198
203
|
def idle_connections?(idle_seconds)
|
199
204
|
connection_stored? && (current_time - @que.first.last > idle_seconds)
|
200
205
|
end
|
@@ -203,7 +208,6 @@ class ConnectionPool::TimedStack
|
|
203
208
|
# This is an extension point for TimedStack and is called with a mutex.
|
204
209
|
#
|
205
210
|
# This method must return +obj+ to the stack.
|
206
|
-
|
207
211
|
def store_connection(obj, options = nil)
|
208
212
|
@que.push [obj, current_time]
|
209
213
|
end
|
@@ -213,7 +217,6 @@ class ConnectionPool::TimedStack
|
|
213
217
|
#
|
214
218
|
# This method must create a connection if and only if the total number of
|
215
219
|
# connections allowed has not been met.
|
216
|
-
|
217
220
|
def try_create(options = nil)
|
218
221
|
unless @created == @max
|
219
222
|
object = @create_block.call
|
data/lib/connection_pool.rb
CHANGED
@@ -39,7 +39,7 @@ end
|
|
39
39
|
# - :auto_reload_after_fork - automatically drop all connections after fork, defaults to true
|
40
40
|
#
|
41
41
|
class ConnectionPool
|
42
|
-
DEFAULTS = {size: 5, timeout: 5, auto_reload_after_fork: true}
|
42
|
+
DEFAULTS = {size: 5, timeout: 5, auto_reload_after_fork: true}.freeze
|
43
43
|
|
44
44
|
def self.wrap(options, &block)
|
45
45
|
Wrapper.new(options, &block)
|
@@ -99,7 +99,8 @@ class ConnectionPool
|
|
99
99
|
@available = TimedStack.new(@size, &block)
|
100
100
|
@key = :"pool-#{@available.object_id}"
|
101
101
|
@key_count = :"pool-#{@available.object_id}-count"
|
102
|
-
|
102
|
+
@discard_key = :"pool-#{@available.object_id}-discard"
|
103
|
+
INSTANCES[self] = self if @auto_reload_after_fork && INSTANCES
|
103
104
|
end
|
104
105
|
|
105
106
|
def with(options = {})
|
@@ -116,20 +117,65 @@ class ConnectionPool
|
|
116
117
|
end
|
117
118
|
alias_method :then, :with
|
118
119
|
|
120
|
+
##
|
121
|
+
# Marks the current thread's checked-out connection for discard.
|
122
|
+
#
|
123
|
+
# When a connection is marked for discard, it will not be returned to the pool
|
124
|
+
# when checked in. Instead, the connection will be discarded.
|
125
|
+
# This is useful when a connection has become invalid or corrupted
|
126
|
+
# and should not be reused.
|
127
|
+
#
|
128
|
+
# Takes an optional block that will be called with the connection to be discarded.
|
129
|
+
# The block should perform any necessary clean-up on the connection.
|
130
|
+
#
|
131
|
+
# @yield [conn]
|
132
|
+
# @yieldparam conn [Object] The connection to be discarded.
|
133
|
+
# @yieldreturn [void]
|
134
|
+
#
|
135
|
+
#
|
136
|
+
# Note: This only affects the connection currently checked out by the calling thread.
|
137
|
+
# The connection will be discarded when +checkin+ is called.
|
138
|
+
#
|
139
|
+
# @return [void]
|
140
|
+
#
|
141
|
+
# @example
|
142
|
+
# pool.with do |conn|
|
143
|
+
# begin
|
144
|
+
# conn.execute("SELECT 1")
|
145
|
+
# rescue SomeConnectionError
|
146
|
+
# pool.discard_current_connection # Mark connection as bad
|
147
|
+
# raise
|
148
|
+
# end
|
149
|
+
# end
|
150
|
+
def discard_current_connection(&block)
|
151
|
+
::Thread.current[@discard_key] = block || proc { |conn| conn }
|
152
|
+
end
|
153
|
+
|
119
154
|
def checkout(options = {})
|
120
155
|
if ::Thread.current[@key]
|
121
156
|
::Thread.current[@key_count] += 1
|
122
157
|
::Thread.current[@key]
|
123
158
|
else
|
124
159
|
::Thread.current[@key_count] = 1
|
125
|
-
::Thread.current[@key] = @available.pop(options[:timeout] || @timeout)
|
160
|
+
::Thread.current[@key] = @available.pop(options[:timeout] || @timeout, options)
|
126
161
|
end
|
127
162
|
end
|
128
163
|
|
129
164
|
def checkin(force: false)
|
130
165
|
if ::Thread.current[@key]
|
131
166
|
if ::Thread.current[@key_count] == 1 || force
|
132
|
-
|
167
|
+
if ::Thread.current[@discard_key]
|
168
|
+
begin
|
169
|
+
@available.decrement_created
|
170
|
+
::Thread.current[@discard_key].call(::Thread.current[@key])
|
171
|
+
rescue
|
172
|
+
nil
|
173
|
+
ensure
|
174
|
+
::Thread.current[@discard_key] = nil
|
175
|
+
end
|
176
|
+
else
|
177
|
+
@available.push(::Thread.current[@key])
|
178
|
+
end
|
133
179
|
::Thread.current[@key] = nil
|
134
180
|
::Thread.current[@key_count] = nil
|
135
181
|
else
|
@@ -146,7 +192,6 @@ class ConnectionPool
|
|
146
192
|
# Shuts down the ConnectionPool by passing each connection to +block+ and
|
147
193
|
# then removing it from the pool. Attempting to checkout a connection after
|
148
194
|
# shutdown will raise +ConnectionPool::PoolShuttingDownError+.
|
149
|
-
|
150
195
|
def shutdown(&block)
|
151
196
|
@available.shutdown(&block)
|
152
197
|
end
|
@@ -155,7 +200,6 @@ class ConnectionPool
|
|
155
200
|
# Reloads the ConnectionPool by passing each connection to +block+ and then
|
156
201
|
# removing it the pool. Subsequent checkouts will create new connections as
|
157
202
|
# needed.
|
158
|
-
|
159
203
|
def reload(&block)
|
160
204
|
@available.shutdown(reload: true, &block)
|
161
205
|
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: connection_pool
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.5.
|
4
|
+
version: 2.5.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Perham
|
8
8
|
- Damian Janowski
|
9
|
-
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
@@ -75,7 +74,6 @@ licenses:
|
|
75
74
|
metadata:
|
76
75
|
changelog_uri: https://github.com/mperham/connection_pool/blob/main/Changes.md
|
77
76
|
rubygems_mfa_required: 'true'
|
78
|
-
post_install_message:
|
79
77
|
rdoc_options: []
|
80
78
|
require_paths:
|
81
79
|
- lib
|
@@ -90,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
88
|
- !ruby/object:Gem::Version
|
91
89
|
version: '0'
|
92
90
|
requirements: []
|
93
|
-
rubygems_version: 3.
|
94
|
-
signing_key:
|
91
|
+
rubygems_version: 3.6.9
|
95
92
|
specification_version: 4
|
96
93
|
summary: Generic connection pool for Ruby
|
97
94
|
test_files: []
|