connection_pool 3.0.2 → 3.0.3
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 +6 -0
- data/connection_pool.gemspec +1 -0
- data/lib/connection_pool/timed_stack.rb +17 -14
- data/lib/connection_pool/version.rb +1 -1
- data/lib/connection_pool/wrapper.rb +13 -13
- data/lib/connection_pool.rb +41 -28
- metadata +16 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e605c5eee4bf9cbc123ae068f893f55b930529255ed52e9e1219d45c5182d5d7
|
|
4
|
+
data.tar.gz: 9f604dda5745eb5522308ccfd5834c181fc38dd01b3255fdfa62abfe48cbc5a7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f3581866a887e9666d289303f438afe265d48573c670b5fbef0740e103602262ff5433489076d4a81623f8e0582456d8d7321c5396bc2550385e90f48b9771a8
|
|
7
|
+
data.tar.gz: 1026ac2eb3009c52fa69299ad9e1a0ffa4b66cceadfc0d5f21923bfaee7f5afaf7504a29ce38f9eca819c15bb6417931e50ae4279e1f91b9cb4099ee6fd69469
|
data/Changes.md
CHANGED
data/connection_pool.gemspec
CHANGED
|
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
|
|
|
22
22
|
s.required_ruby_version = ">= 3.2.0"
|
|
23
23
|
s.add_development_dependency "bundler"
|
|
24
24
|
s.add_development_dependency "maxitest"
|
|
25
|
+
s.add_development_dependency "minitest-mock"
|
|
25
26
|
s.add_development_dependency "rake"
|
|
26
27
|
|
|
27
28
|
s.metadata = {
|
|
@@ -35,13 +35,13 @@ class ConnectionPool::TimedStack
|
|
|
35
35
|
##
|
|
36
36
|
# Returns +obj+ to the stack. Additional kwargs are ignored in TimedStack but may be
|
|
37
37
|
# used by subclasses that extend TimedStack.
|
|
38
|
-
def push(obj, **)
|
|
38
|
+
def push(obj, **kwargs)
|
|
39
39
|
@mutex.synchronize do
|
|
40
40
|
if @shutdown_block
|
|
41
41
|
@created -= 1 unless @created == 0
|
|
42
42
|
@shutdown_block.call(obj)
|
|
43
43
|
else
|
|
44
|
-
store_connection obj, **
|
|
44
|
+
store_connection obj, **kwargs
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
@resource.broadcast
|
|
@@ -59,16 +59,16 @@ class ConnectionPool::TimedStack
|
|
|
59
59
|
# if an entry was not available within the timeout period. Use `exception: false` to return nil.
|
|
60
60
|
#
|
|
61
61
|
# Other options may be used by subclasses that extend TimedStack.
|
|
62
|
-
def pop(timeout: 0.5, exception: ConnectionPool::TimeoutError, **)
|
|
62
|
+
def pop(timeout: 0.5, exception: ConnectionPool::TimeoutError, **kwargs)
|
|
63
63
|
deadline = current_time + timeout
|
|
64
64
|
@mutex.synchronize do
|
|
65
65
|
loop do
|
|
66
66
|
raise ConnectionPool::PoolShuttingDownError if @shutdown_block
|
|
67
|
-
if (conn = try_fetch_connection(**))
|
|
67
|
+
if (conn = try_fetch_connection(**kwargs))
|
|
68
68
|
return conn
|
|
69
69
|
end
|
|
70
70
|
|
|
71
|
-
connection = try_create(**)
|
|
71
|
+
connection = try_create(**kwargs)
|
|
72
72
|
return connection if connection
|
|
73
73
|
|
|
74
74
|
to_wait = deadline - current_time
|
|
@@ -141,7 +141,10 @@ class ConnectionPool::TimedStack
|
|
|
141
141
|
##
|
|
142
142
|
# Reduce the created count
|
|
143
143
|
def decrement_created
|
|
144
|
-
@
|
|
144
|
+
@mutex.synchronize do
|
|
145
|
+
@created -= 1 unless @created == 0
|
|
146
|
+
@resource.broadcast
|
|
147
|
+
end
|
|
145
148
|
end
|
|
146
149
|
|
|
147
150
|
private
|
|
@@ -156,15 +159,15 @@ class ConnectionPool::TimedStack
|
|
|
156
159
|
# This method must returns a connection from the stack if one exists. Allows
|
|
157
160
|
# subclasses with expensive match/search algorithms to avoid double-handling
|
|
158
161
|
# their stack.
|
|
159
|
-
def try_fetch_connection(**)
|
|
160
|
-
connection_stored?(**) && fetch_connection(**)
|
|
162
|
+
def try_fetch_connection(**kwargs)
|
|
163
|
+
connection_stored?(**kwargs) && fetch_connection(**kwargs)
|
|
161
164
|
end
|
|
162
165
|
|
|
163
166
|
##
|
|
164
167
|
# This is an extension point for TimedStack and is called with a mutex.
|
|
165
168
|
#
|
|
166
169
|
# This method must returns true if a connection is available on the stack.
|
|
167
|
-
def connection_stored?(**)
|
|
170
|
+
def connection_stored?(**_kwargs)
|
|
168
171
|
!@que.empty?
|
|
169
172
|
end
|
|
170
173
|
|
|
@@ -172,7 +175,7 @@ class ConnectionPool::TimedStack
|
|
|
172
175
|
# This is an extension point for TimedStack and is called with a mutex.
|
|
173
176
|
#
|
|
174
177
|
# This method must return a connection from the stack.
|
|
175
|
-
def fetch_connection(**)
|
|
178
|
+
def fetch_connection(**_kwargs)
|
|
176
179
|
@que.pop&.first
|
|
177
180
|
end
|
|
178
181
|
|
|
@@ -180,8 +183,8 @@ class ConnectionPool::TimedStack
|
|
|
180
183
|
# This is an extension point for TimedStack and is called with a mutex.
|
|
181
184
|
#
|
|
182
185
|
# This method must shut down all connections on the stack.
|
|
183
|
-
def shutdown_connections(**)
|
|
184
|
-
while (conn = try_fetch_connection(**))
|
|
186
|
+
def shutdown_connections(**kwargs)
|
|
187
|
+
while (conn = try_fetch_connection(**kwargs))
|
|
185
188
|
@created -= 1 unless @created == 0
|
|
186
189
|
@shutdown_block.call(conn)
|
|
187
190
|
end
|
|
@@ -217,7 +220,7 @@ class ConnectionPool::TimedStack
|
|
|
217
220
|
# This is an extension point for TimedStack and is called with a mutex.
|
|
218
221
|
#
|
|
219
222
|
# This method must return +obj+ to the stack.
|
|
220
|
-
def store_connection(obj, **)
|
|
223
|
+
def store_connection(obj, **_kwargs)
|
|
221
224
|
@que.push [obj, current_time]
|
|
222
225
|
end
|
|
223
226
|
|
|
@@ -226,7 +229,7 @@ class ConnectionPool::TimedStack
|
|
|
226
229
|
#
|
|
227
230
|
# This method must create a connection if and only if the total number of
|
|
228
231
|
# connections allowed has not been met.
|
|
229
|
-
def try_create(**)
|
|
232
|
+
def try_create(**_kwargs)
|
|
230
233
|
unless @created == @max
|
|
231
234
|
object = @create_block.call
|
|
232
235
|
@created += 1
|
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
class ConnectionPool
|
|
2
2
|
class Wrapper < ::BasicObject
|
|
3
|
-
METHODS = [:with, :pool_shutdown, :wrapped_pool]
|
|
3
|
+
METHODS = [:with, :pool_shutdown, :pool_size, :pool_available, :wrapped_pool]
|
|
4
4
|
|
|
5
|
-
def initialize(**options, &)
|
|
6
|
-
@pool = options.fetch(:pool) { ::ConnectionPool.new(**options, &) }
|
|
5
|
+
def initialize(**options, &block)
|
|
6
|
+
@pool = options.fetch(:pool) { ::ConnectionPool.new(**options, &block) }
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def wrapped_pool
|
|
10
10
|
@pool
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def with(
|
|
14
|
-
@pool.with(
|
|
13
|
+
def with(**kwargs, &block)
|
|
14
|
+
@pool.with(**kwargs, &block)
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def pool_shutdown(&)
|
|
18
|
-
@pool.shutdown(&)
|
|
17
|
+
def pool_shutdown(&block)
|
|
18
|
+
@pool.shutdown(&block)
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def pool_size
|
|
@@ -26,17 +26,17 @@ class ConnectionPool
|
|
|
26
26
|
@pool.available
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def respond_to?(id,
|
|
30
|
-
METHODS.include?(id) || with { |c| c.respond_to?(id,
|
|
29
|
+
def respond_to?(id, *args, **kwargs)
|
|
30
|
+
METHODS.include?(id) || with { |c| c.respond_to?(id, *args, **kwargs) }
|
|
31
31
|
end
|
|
32
32
|
|
|
33
|
-
def respond_to_missing?(id,
|
|
34
|
-
with { |c| c.respond_to?(id,
|
|
33
|
+
def respond_to_missing?(id, *args, **kwargs)
|
|
34
|
+
with { |c| c.respond_to?(id, *args, **kwargs) }
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
-
def method_missing(name,
|
|
37
|
+
def method_missing(name, *args, **kwargs, &block)
|
|
38
38
|
with do |connection|
|
|
39
|
-
connection.send(name,
|
|
39
|
+
connection.send(name, *args, **kwargs, &block)
|
|
40
40
|
end
|
|
41
41
|
end
|
|
42
42
|
end
|
data/lib/connection_pool.rb
CHANGED
|
@@ -39,32 +39,40 @@ end
|
|
|
39
39
|
# - :auto_reload_after_fork - automatically drop all connections after fork, defaults to true
|
|
40
40
|
#
|
|
41
41
|
class ConnectionPool
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
# Built once: a Hash literal argument costs a Hash and its keyword copy per call.
|
|
43
|
+
DEFER_INTERRUPTS = {Exception => :never}.freeze
|
|
44
|
+
IMMEDIATE_INTERRUPTS = {Exception => :immediate}.freeze
|
|
45
|
+
private_constant :DEFER_INTERRUPTS, :IMMEDIATE_INTERRUPTS
|
|
46
|
+
|
|
47
|
+
def self.wrap(**kwargs, &block)
|
|
48
|
+
Wrapper.new(**kwargs, &block)
|
|
44
49
|
end
|
|
45
50
|
|
|
46
51
|
attr_reader :size
|
|
47
52
|
|
|
48
|
-
def initialize(timeout: 5, size: 5, auto_reload_after_fork: true, name: nil, &)
|
|
53
|
+
def initialize(timeout: 5, size: 5, auto_reload_after_fork: true, name: nil, &block)
|
|
49
54
|
raise ArgumentError, "Connection pool requires a block" unless block_given?
|
|
50
55
|
|
|
51
56
|
@size = Integer(size)
|
|
57
|
+
raise ArgumentError, "Connection pool size cannot be negative" if @size.negative?
|
|
58
|
+
|
|
52
59
|
@timeout = Float(timeout)
|
|
53
|
-
@available = TimedStack.new(size: @size, &)
|
|
60
|
+
@available = TimedStack.new(size: @size, &block)
|
|
54
61
|
@key = :"pool-#{@available.object_id}"
|
|
55
62
|
@key_count = :"pool-#{@available.object_id}-count"
|
|
63
|
+
@key_kwargs = :"pool-#{@available.object_id}-kwargs"
|
|
56
64
|
@discard_key = :"pool-#{@available.object_id}-discard"
|
|
57
65
|
INSTANCES[self] = self if auto_reload_after_fork && INSTANCES
|
|
58
66
|
end
|
|
59
67
|
|
|
60
|
-
def with(**)
|
|
68
|
+
def with(**kwargs)
|
|
61
69
|
# We need to manage exception handling manually here in order
|
|
62
70
|
# to work correctly with `Timeout.timeout` and `Thread#raise`.
|
|
63
71
|
# Otherwise an interrupted Thread can leak connections.
|
|
64
|
-
Thread.handle_interrupt(
|
|
65
|
-
conn = checkout(**)
|
|
72
|
+
Thread.handle_interrupt(DEFER_INTERRUPTS) do
|
|
73
|
+
conn = checkout(**kwargs)
|
|
66
74
|
begin
|
|
67
|
-
Thread.handle_interrupt(
|
|
75
|
+
Thread.handle_interrupt(IMMEDIATE_INTERRUPTS) do
|
|
68
76
|
yield conn
|
|
69
77
|
end
|
|
70
78
|
ensure
|
|
@@ -108,14 +116,15 @@ class ConnectionPool
|
|
|
108
116
|
::Thread.current[@discard_key] = block || proc { |conn| conn }
|
|
109
117
|
end
|
|
110
118
|
|
|
111
|
-
def checkout(timeout: @timeout, **)
|
|
119
|
+
def checkout(timeout: @timeout, **kwargs)
|
|
112
120
|
if ::Thread.current[@key]
|
|
113
121
|
::Thread.current[@key_count] += 1
|
|
114
122
|
::Thread.current[@key]
|
|
115
123
|
else
|
|
116
|
-
conn = @available.pop(timeout:, **)
|
|
124
|
+
conn = @available.pop(timeout:, **kwargs)
|
|
117
125
|
::Thread.current[@key] = conn
|
|
118
126
|
::Thread.current[@key_count] = 1
|
|
127
|
+
::Thread.current[@key_kwargs] = kwargs
|
|
119
128
|
conn
|
|
120
129
|
end
|
|
121
130
|
end
|
|
@@ -123,20 +132,24 @@ class ConnectionPool
|
|
|
123
132
|
def checkin(force: false)
|
|
124
133
|
if ::Thread.current[@key]
|
|
125
134
|
if ::Thread.current[@key_count] == 1 || force
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
135
|
+
begin
|
|
136
|
+
if ::Thread.current[@discard_key]
|
|
137
|
+
begin
|
|
138
|
+
@available.decrement_created
|
|
139
|
+
::Thread.current[@discard_key].call(::Thread.current[@key])
|
|
140
|
+
rescue
|
|
141
|
+
nil
|
|
142
|
+
ensure
|
|
143
|
+
::Thread.current[@discard_key] = nil
|
|
144
|
+
end
|
|
145
|
+
else
|
|
146
|
+
@available.push(::Thread.current[@key], **::Thread.current[@key_kwargs])
|
|
134
147
|
end
|
|
135
|
-
|
|
136
|
-
|
|
148
|
+
ensure
|
|
149
|
+
::Thread.current[@key] = nil
|
|
150
|
+
::Thread.current[@key_count] = nil
|
|
151
|
+
::Thread.current[@key_kwargs] = nil
|
|
137
152
|
end
|
|
138
|
-
::Thread.current[@key] = nil
|
|
139
|
-
::Thread.current[@key_count] = nil
|
|
140
153
|
else
|
|
141
154
|
::Thread.current[@key_count] -= 1
|
|
142
155
|
end
|
|
@@ -151,22 +164,22 @@ class ConnectionPool
|
|
|
151
164
|
# Shuts down the ConnectionPool by passing each connection to +block+ and
|
|
152
165
|
# then removing it from the pool. Attempting to checkout a connection after
|
|
153
166
|
# shutdown will raise +ConnectionPool::PoolShuttingDownError+.
|
|
154
|
-
def shutdown(&)
|
|
155
|
-
@available.shutdown(&)
|
|
167
|
+
def shutdown(&block)
|
|
168
|
+
@available.shutdown(&block)
|
|
156
169
|
end
|
|
157
170
|
|
|
158
171
|
##
|
|
159
172
|
# Reloads the ConnectionPool by passing each connection to +block+ and then
|
|
160
173
|
# removing it the pool. Subsequent checkouts will create new connections as
|
|
161
174
|
# needed.
|
|
162
|
-
def reload(&)
|
|
163
|
-
@available.shutdown(reload: true, &)
|
|
175
|
+
def reload(&block)
|
|
176
|
+
@available.shutdown(reload: true, &block)
|
|
164
177
|
end
|
|
165
178
|
|
|
166
179
|
## Reaps idle connections that have been idle for over +idle_seconds+.
|
|
167
180
|
# +idle_seconds+ defaults to 60.
|
|
168
|
-
def reap(idle_seconds: 60, &)
|
|
169
|
-
@available.reap(idle_seconds:, &)
|
|
181
|
+
def reap(idle_seconds: 60, &block)
|
|
182
|
+
@available.reap(idle_seconds:, &block)
|
|
170
183
|
end
|
|
171
184
|
|
|
172
185
|
# Number of pool entries available for checkout at this instant.
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: connection_pool
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.0.
|
|
4
|
+
version: 3.0.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mike Perham
|
|
@@ -38,6 +38,20 @@ dependencies:
|
|
|
38
38
|
- - ">="
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
40
|
version: '0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: minitest-mock
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
41
55
|
- !ruby/object:Gem::Dependency
|
|
42
56
|
name: rake
|
|
43
57
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -93,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
93
107
|
- !ruby/object:Gem::Version
|
|
94
108
|
version: '0'
|
|
95
109
|
requirements: []
|
|
96
|
-
rubygems_version:
|
|
110
|
+
rubygems_version: 4.0.6
|
|
97
111
|
specification_version: 4
|
|
98
112
|
summary: Generic connection pool for Ruby
|
|
99
113
|
test_files: []
|