connection_pool 2.5.0 → 2.5.1
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 +7 -0
- data/lib/connection_pool/timed_stack.rb +20 -23
- data/lib/connection_pool/version.rb +1 -1
- data/lib/connection_pool.rb +3 -5
- 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: 0a46ac6651f50f053c03abb5ab7bdc0a6e3ddde7c1f8dd1d76fbd7939f3ba31d
|
4
|
+
data.tar.gz: 71cd4da429af569a8a8a9d4a2af51081e5363eb184f29b876a3c4a3ccf3e689e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e73f6dea5b92fd29d8249011390f3f1bff7562d0c189602c88d8d95fa5337fa50270f6ed8a8c2031423db2a15d22685668032f3e6e0629c04d99b71342b28a50
|
7
|
+
data.tar.gz: 010ce12b06b7f678eae90c949366c94450a03c3b0f8f676ec35acff8b71470eb66c29b6ec3838981bf070136e72f9698687251284f7d6ac1d7f46cd406727073
|
data/Changes.md
CHANGED
@@ -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
|
@@ -148,8 +143,17 @@ class ConnectionPool::TimedStack
|
|
148
143
|
##
|
149
144
|
# This is an extension point for TimedStack and is called with a mutex.
|
150
145
|
#
|
151
|
-
# This method must returns
|
146
|
+
# This method must returns a connection from the stack if one exists. Allows
|
147
|
+
# subclasses with expensive match/search algorithms to avoid double-handling
|
148
|
+
# their stack.
|
149
|
+
def try_fetch_connection(options = nil)
|
150
|
+
connection_stored?(options) && fetch_connection(options)
|
151
|
+
end
|
152
152
|
|
153
|
+
##
|
154
|
+
# This is an extension point for TimedStack and is called with a mutex.
|
155
|
+
#
|
156
|
+
# This method must returns true if a connection is available on the stack.
|
153
157
|
def connection_stored?(options = nil)
|
154
158
|
!@que.empty?
|
155
159
|
end
|
@@ -158,7 +162,6 @@ class ConnectionPool::TimedStack
|
|
158
162
|
# This is an extension point for TimedStack and is called with a mutex.
|
159
163
|
#
|
160
164
|
# This method must return a connection from the stack.
|
161
|
-
|
162
165
|
def fetch_connection(options = nil)
|
163
166
|
@que.pop&.first
|
164
167
|
end
|
@@ -167,10 +170,8 @@ class ConnectionPool::TimedStack
|
|
167
170
|
# This is an extension point for TimedStack and is called with a mutex.
|
168
171
|
#
|
169
172
|
# This method must shut down all connections on the stack.
|
170
|
-
|
171
173
|
def shutdown_connections(options = nil)
|
172
|
-
while
|
173
|
-
conn = fetch_connection(options)
|
174
|
+
while (conn = try_fetch_connection(options))
|
174
175
|
@created -= 1 unless @created == 0
|
175
176
|
@shutdown_block.call(conn)
|
176
177
|
end
|
@@ -181,7 +182,6 @@ class ConnectionPool::TimedStack
|
|
181
182
|
#
|
182
183
|
# This method returns the oldest idle connection if it has been idle for more than idle_seconds.
|
183
184
|
# This requires that the stack is kept in order of checked in time (oldest first).
|
184
|
-
|
185
185
|
def reserve_idle_connection(idle_seconds)
|
186
186
|
return unless idle_connections?(idle_seconds)
|
187
187
|
|
@@ -194,7 +194,6 @@ class ConnectionPool::TimedStack
|
|
194
194
|
# This is an extension point for TimedStack and is called with a mutex.
|
195
195
|
#
|
196
196
|
# Returns true if the first connection in the stack has been idle for more than idle_seconds
|
197
|
-
|
198
197
|
def idle_connections?(idle_seconds)
|
199
198
|
connection_stored? && (current_time - @que.first.last > idle_seconds)
|
200
199
|
end
|
@@ -203,7 +202,6 @@ class ConnectionPool::TimedStack
|
|
203
202
|
# This is an extension point for TimedStack and is called with a mutex.
|
204
203
|
#
|
205
204
|
# This method must return +obj+ to the stack.
|
206
|
-
|
207
205
|
def store_connection(obj, options = nil)
|
208
206
|
@que.push [obj, current_time]
|
209
207
|
end
|
@@ -213,7 +211,6 @@ class ConnectionPool::TimedStack
|
|
213
211
|
#
|
214
212
|
# This method must create a connection if and only if the total number of
|
215
213
|
# connections allowed has not been met.
|
216
|
-
|
217
214
|
def try_create(options = nil)
|
218
215
|
unless @created == @max
|
219
216
|
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:
|
42
|
+
DEFAULTS = {size: 5, timeout: 5, auto_reload_after_fork: false}.freeze
|
43
43
|
|
44
44
|
def self.wrap(options, &block)
|
45
45
|
Wrapper.new(options, &block)
|
@@ -99,7 +99,7 @@ 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
|
-
INSTANCES[self] = self if
|
102
|
+
INSTANCES[self] = self if @auto_reload_after_fork
|
103
103
|
end
|
104
104
|
|
105
105
|
def with(options = {})
|
@@ -122,7 +122,7 @@ class ConnectionPool
|
|
122
122
|
::Thread.current[@key]
|
123
123
|
else
|
124
124
|
::Thread.current[@key_count] = 1
|
125
|
-
::Thread.current[@key] = @available.pop(options[:timeout] || @timeout)
|
125
|
+
::Thread.current[@key] = @available.pop(options[:timeout] || @timeout, options)
|
126
126
|
end
|
127
127
|
end
|
128
128
|
|
@@ -146,7 +146,6 @@ class ConnectionPool
|
|
146
146
|
# Shuts down the ConnectionPool by passing each connection to +block+ and
|
147
147
|
# then removing it from the pool. Attempting to checkout a connection after
|
148
148
|
# shutdown will raise +ConnectionPool::PoolShuttingDownError+.
|
149
|
-
|
150
149
|
def shutdown(&block)
|
151
150
|
@available.shutdown(&block)
|
152
151
|
end
|
@@ -155,7 +154,6 @@ class ConnectionPool
|
|
155
154
|
# Reloads the ConnectionPool by passing each connection to +block+ and then
|
156
155
|
# removing it the pool. Subsequent checkouts will create new connections as
|
157
156
|
# needed.
|
158
|
-
|
159
157
|
def reload(&block)
|
160
158
|
@available.shutdown(reload: true, &block)
|
161
159
|
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.1
|
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: 2025-
|
11
|
+
date: 2025-04-16 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.2
|
95
92
|
specification_version: 4
|
96
93
|
summary: Generic connection pool for Ruby
|
97
94
|
test_files: []
|