active_record-async_connection_pool 0.1.0
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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +349 -0
- data/Rakefile +11 -0
- data/lib/active_record/async_connection_pool/adapter_extension/attachment.rb +276 -0
- data/lib/active_record/async_connection_pool/adapter_extension/detection.rb +55 -0
- data/lib/active_record/async_connection_pool/adapter_extension.rb +343 -0
- data/lib/active_record/async_connection_pool/database_options.rb +80 -0
- data/lib/active_record/async_connection_pool/errors.rb +17 -0
- data/lib/active_record/async_connection_pool/handler.rb +183 -0
- data/lib/active_record/async_connection_pool/handlers/postgresql/adapter_methods.rb +216 -0
- data/lib/active_record/async_connection_pool/handlers/postgresql/pg_query_detector.rb +256 -0
- data/lib/active_record/async_connection_pool/handlers/postgresql.rb +347 -0
- data/lib/active_record/async_connection_pool/handlers/sqlite3/adapter_methods.rb +143 -0
- data/lib/active_record/async_connection_pool/handlers/sqlite3.rb +183 -0
- data/lib/active_record/async_connection_pool/handlers/trilogy/adapter_methods.rb +140 -0
- data/lib/active_record/async_connection_pool/handlers/trilogy.rb +159 -0
- data/lib/active_record/async_connection_pool/pool_extension.rb +201 -0
- data/lib/active_record/async_connection_pool/raw_entry.rb +80 -0
- data/lib/active_record/async_connection_pool/raw_pool.rb +336 -0
- data/lib/active_record/async_connection_pool/session_state.rb +223 -0
- data/lib/active_record/async_connection_pool/version.rb +8 -0
- data/lib/active_record/async_connection_pool.rb +86 -0
- metadata +86 -0
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
module AsyncConnectionPool
|
|
6
|
+
module AdapterExtension
|
|
7
|
+
# What happens when a statement leaves state in the database session, per the handler: pinned, the raw
|
|
8
|
+
# connection is tainted so it is reset or closed when the pin ends; unpinned, it is pinned for the rest of the
|
|
9
|
+
# lease, or the statement is refused, per `session_state`.
|
|
10
|
+
module Detection
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
# The patterns are UTF-8, but SQL in another encoding (often binary) or not valid in its own still goes to the
|
|
14
|
+
# driver, so it is read as UTF-8 with invalid bytes replaced, rather than raising from the pattern match. SQL
|
|
15
|
+
# that is not a string is left for the driver to reject.
|
|
16
|
+
#
|
|
17
|
+
#: (untyped sql) -> void
|
|
18
|
+
def async_detect_session_state(sql)
|
|
19
|
+
return unless sql.is_a?(String)
|
|
20
|
+
|
|
21
|
+
unless sql.encoding == Encoding::UTF_8 && sql.valid_encoding?
|
|
22
|
+
sql = sql.dup.force_encoding(Encoding::UTF_8).scrub
|
|
23
|
+
end
|
|
24
|
+
handler = async_raw_pool.handler
|
|
25
|
+
return unless handler.session_state_statement_on?(sql, @async_entry)
|
|
26
|
+
|
|
27
|
+
if raw_pinned?
|
|
28
|
+
@async_entry.taint!
|
|
29
|
+
elsif handler.options.session_state == :raise
|
|
30
|
+
raise UnpinnedSessionStateError,
|
|
31
|
+
"#{sql.inspect} leaves state in the database session outside a pin. Wrap it in " \
|
|
32
|
+
'`pin { ... }` so the connection stays attached, or set `session_state: :pin`.'
|
|
33
|
+
else
|
|
34
|
+
async_pin_for_session_state(sql)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
#: (String sql) -> void
|
|
39
|
+
def async_pin_for_session_state(sql)
|
|
40
|
+
lease_pin!(:session_state)
|
|
41
|
+
@async_entry.taint!
|
|
42
|
+
ActiveSupport::Notifications.instrument(SESSION_STATE, sql:, policy: :pin)
|
|
43
|
+
(logger || ActiveRecord::Base.logger)&.warn(async_session_state_warning(sql))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
#: (String sql) -> String
|
|
47
|
+
def async_session_state_warning(sql)
|
|
48
|
+
source = ActiveRecord::LogSubscriber.backtrace_cleaner.first_clean_frame
|
|
49
|
+
"#{sql.inspect} leaves state in the database session, so the connection is pinned until the lease ends" \
|
|
50
|
+
"#{" (called from #{source})" if source}. Wrap it in `pin { ... }` or remove it."
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'active_record/async_connection_pool/adapter_extension/attachment'
|
|
5
|
+
require 'active_record/async_connection_pool/adapter_extension/detection'
|
|
6
|
+
|
|
7
|
+
module ActiveRecord
|
|
8
|
+
module AsyncConnectionPool
|
|
9
|
+
# Prepended to each supported adapter class. An adapter whose pool has a raw pool holds a physical connection
|
|
10
|
+
# only while a statement runs, a materialized transaction is open, or it is pinned. Without a raw pool every
|
|
11
|
+
# method defers to ActiveRecord.
|
|
12
|
+
module AdapterExtension
|
|
13
|
+
ATTACH = 'attach.active_record_async_connection_pool'
|
|
14
|
+
DETACH = 'detach.active_record_async_connection_pool'
|
|
15
|
+
PIN = 'pin.active_record_async_connection_pool'
|
|
16
|
+
UNPIN = 'unpin.active_record_async_connection_pool'
|
|
17
|
+
SESSION_STATE = 'session_state.active_record_async_connection_pool'
|
|
18
|
+
|
|
19
|
+
include Attachment
|
|
20
|
+
include Detection
|
|
21
|
+
|
|
22
|
+
#: (*untyped args) -> void
|
|
23
|
+
def initialize(*args)
|
|
24
|
+
super
|
|
25
|
+
@async_entry = nil #: RawEntry?
|
|
26
|
+
@async_depth = 0
|
|
27
|
+
@async_pins = 0
|
|
28
|
+
@async_lease_pins = [] #: Array[Symbol]
|
|
29
|
+
@async_building = false
|
|
30
|
+
@async_in_transaction = false
|
|
31
|
+
@async_detection_disabled = false
|
|
32
|
+
@async_transaction_lost = false
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Whether a raw connection is attached right now.
|
|
36
|
+
#
|
|
37
|
+
#: () -> bool
|
|
38
|
+
def attached?
|
|
39
|
+
async_raw_pool ? !@async_entry.nil? : !@raw_connection.nil?
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Keeps the same raw connection attached for the block, for session state such as `SET`, `LISTEN` or temp
|
|
43
|
+
# tables.
|
|
44
|
+
#
|
|
45
|
+
#: [T] () { () -> T } -> T
|
|
46
|
+
def pin
|
|
47
|
+
return yield unless async_raw_pool
|
|
48
|
+
|
|
49
|
+
ActiveSupport::Notifications.instrument(PIN, reason: :explicit) do
|
|
50
|
+
@async_pins += 1
|
|
51
|
+
begin
|
|
52
|
+
@lock.synchronize { async_attach! } unless @async_entry
|
|
53
|
+
async_sync_pinned
|
|
54
|
+
yield
|
|
55
|
+
ensure
|
|
56
|
+
@async_pins -= 1
|
|
57
|
+
async_sync_pinned
|
|
58
|
+
async_detach_if_idle
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Whether the raw connection is pinned, by `pin { }` or until the lease ends. Not ActiveRecord's own `pinned`
|
|
64
|
+
# flag, which `pin_connection!` sets on the adapter.
|
|
65
|
+
#
|
|
66
|
+
#: () -> bool
|
|
67
|
+
def raw_pinned?
|
|
68
|
+
@async_pins.positive? || @async_lease_pins.any?
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The raw pool of this adapter's connection pool. Nil means the extension is inert.
|
|
72
|
+
#
|
|
73
|
+
#: () -> RawPool?
|
|
74
|
+
def async_raw_pool # :nodoc:
|
|
75
|
+
pool = @pool
|
|
76
|
+
pool.raw_pool if pool.is_a?(PoolExtension)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Gives the raw connection back to the raw pool: reset first if tainted or `reset_on_checkin`, closed instead
|
|
80
|
+
# unless the handler finds it clean.
|
|
81
|
+
#
|
|
82
|
+
#: () -> void
|
|
83
|
+
def detach! # :nodoc:
|
|
84
|
+
return unless (entry = @async_entry)
|
|
85
|
+
|
|
86
|
+
@lock.synchronize { async_detach(entry) }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Pins the raw connection until the lease ends.
|
|
90
|
+
#
|
|
91
|
+
#: (Symbol reason) -> void
|
|
92
|
+
def lease_pin!(reason) # :nodoc:
|
|
93
|
+
return unless async_raw_pool
|
|
94
|
+
|
|
95
|
+
@lock.synchronize do
|
|
96
|
+
async_attach! unless @async_entry
|
|
97
|
+
unless @async_lease_pins.include?(reason)
|
|
98
|
+
@async_lease_pins << reason
|
|
99
|
+
ActiveSupport::Notifications.instrument(PIN, reason:)
|
|
100
|
+
end
|
|
101
|
+
async_sync_pinned
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
#: () -> void
|
|
106
|
+
def release_lease_pins! # :nodoc:
|
|
107
|
+
async_forget_lost_transaction
|
|
108
|
+
return if @async_lease_pins.empty?
|
|
109
|
+
|
|
110
|
+
async_clear_lease_pins
|
|
111
|
+
async_sync_pinned
|
|
112
|
+
async_detach_if_idle
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Runs the block without session-state detection, for statements the gem or a handler issues itself.
|
|
116
|
+
#
|
|
117
|
+
#: [T] () { () -> T } -> T
|
|
118
|
+
def without_session_state_detection # :nodoc:
|
|
119
|
+
previous = @async_detection_disabled
|
|
120
|
+
@async_detection_disabled = true
|
|
121
|
+
yield
|
|
122
|
+
ensure
|
|
123
|
+
@async_detection_disabled = previous
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# A detached adapter can attach at any time, so it counts as connected.
|
|
127
|
+
#
|
|
128
|
+
#: () -> bool
|
|
129
|
+
def connected?
|
|
130
|
+
return true if async_detached?
|
|
131
|
+
|
|
132
|
+
super
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
#: () -> bool?
|
|
136
|
+
def active?
|
|
137
|
+
return true if async_detached?
|
|
138
|
+
|
|
139
|
+
super
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
#: () -> untyped
|
|
143
|
+
def verify!
|
|
144
|
+
return true if async_detached?
|
|
145
|
+
|
|
146
|
+
super
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
# Attached, it replaces the raw connection. Detached, there is none, so only the adapter's state is reset.
|
|
150
|
+
#
|
|
151
|
+
#: (?restore_transactions: bool) -> untyped
|
|
152
|
+
def reconnect!(restore_transactions: false)
|
|
153
|
+
return super unless async_raw_pool
|
|
154
|
+
|
|
155
|
+
@lock.synchronize do
|
|
156
|
+
next async_hold { super } if @async_entry
|
|
157
|
+
|
|
158
|
+
async_reset_detached(restore_transactions)
|
|
159
|
+
end
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Overridden on its own because SQLite3Adapter aliases it to the original `reconnect!`, which bypasses the
|
|
163
|
+
# override above. Detached, there is no connection to reset.
|
|
164
|
+
#
|
|
165
|
+
#: () -> untyped
|
|
166
|
+
def reset!
|
|
167
|
+
return super unless async_raw_pool
|
|
168
|
+
|
|
169
|
+
@lock.synchronize do
|
|
170
|
+
next async_hold { super } if @async_entry
|
|
171
|
+
|
|
172
|
+
async_reset_detached(false)
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Closes the attached raw connection, if any, and removes it from the raw pool.
|
|
177
|
+
#
|
|
178
|
+
#: () -> void
|
|
179
|
+
def disconnect!
|
|
180
|
+
return super unless async_raw_pool
|
|
181
|
+
|
|
182
|
+
@lock.synchronize do
|
|
183
|
+
entry = @async_entry
|
|
184
|
+
super
|
|
185
|
+
ensure
|
|
186
|
+
async_clear_lease_pins
|
|
187
|
+
async_drop(entry) if entry
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
# Forgets the attached raw connection without closing it; the pool discards the raw pool after.
|
|
192
|
+
#
|
|
193
|
+
#: () -> void
|
|
194
|
+
def discard!
|
|
195
|
+
super
|
|
196
|
+
ensure
|
|
197
|
+
if (entry = @async_entry)
|
|
198
|
+
async_forget(entry)
|
|
199
|
+
async_clear_lease_pins
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# The handle can be used for anything, so the raw connection stays pinned until the lease ends, and is tainted:
|
|
204
|
+
# its session state is unknown after, so it is reset or closed.
|
|
205
|
+
#
|
|
206
|
+
#: () -> untyped
|
|
207
|
+
def raw_connection
|
|
208
|
+
lease_pin!(:raw_connection)
|
|
209
|
+
@async_entry&.taint!
|
|
210
|
+
super
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# The raw connection stays attached from `BEGIN` until `COMMIT` or `ROLLBACK`. Transaction objects only count
|
|
214
|
+
# as materialized after `BEGIN` returns, and a rolled back one stays on the stack until after `ROLLBACK`, so
|
|
215
|
+
# these track the database transaction itself.
|
|
216
|
+
#
|
|
217
|
+
#: () -> untyped
|
|
218
|
+
def begin_db_transaction
|
|
219
|
+
async_beginning { super }
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
#: (?untyped isolation) -> untyped
|
|
223
|
+
def begin_deferred_transaction(isolation = nil)
|
|
224
|
+
async_beginning { super }
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
#: (untyped isolation) -> untyped
|
|
228
|
+
def begin_isolated_db_transaction(isolation)
|
|
229
|
+
async_beginning { super }
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# A failed `COMMIT` keeps the connection, for the `ROLLBACK` that follows.
|
|
233
|
+
#
|
|
234
|
+
#: () -> untyped
|
|
235
|
+
def commit_db_transaction
|
|
236
|
+
async_refuse_lost_commit!
|
|
237
|
+
result = super
|
|
238
|
+
@async_in_transaction = false
|
|
239
|
+
async_detach_if_idle
|
|
240
|
+
result
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
#: () -> untyped
|
|
244
|
+
def rollback_db_transaction
|
|
245
|
+
super unless async_lost_transaction_rolled_back?
|
|
246
|
+
ensure
|
|
247
|
+
@async_in_transaction = false
|
|
248
|
+
async_detach_if_idle(transaction_over: true)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
private
|
|
252
|
+
|
|
253
|
+
# Every statement passes here, attached, before it reaches the driver.
|
|
254
|
+
#
|
|
255
|
+
#: (untyped raw_connection, untyped sql, *untyped, **untyped) -> untyped
|
|
256
|
+
def perform_query(raw_connection, sql, *, **)
|
|
257
|
+
async_detect_session_state(sql) if @async_entry && !@async_detection_disabled
|
|
258
|
+
super
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# Rails sets this state itself, and undoes it on reconnect, so it is not session state to detect.
|
|
262
|
+
#
|
|
263
|
+
#: () -> untyped
|
|
264
|
+
def configure_connection
|
|
265
|
+
without_session_state_detection { super }
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
#: (?allow_retry: bool, ?materialize_transactions: bool) { (untyped) -> untyped } -> untyped
|
|
269
|
+
def with_raw_connection(allow_retry: false, materialize_transactions: true)
|
|
270
|
+
return super if @async_building || !async_raw_pool
|
|
271
|
+
|
|
272
|
+
@lock.synchronize do
|
|
273
|
+
async_check_transaction_lost!
|
|
274
|
+
async_attach! unless @async_entry
|
|
275
|
+
async_hold { async_in_flight { super } }
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Removes the attached entry, then attaches a replacement. `reconnect!` configures it.
|
|
280
|
+
#
|
|
281
|
+
#: () -> void
|
|
282
|
+
def reconnect
|
|
283
|
+
return super unless async_raw_pool
|
|
284
|
+
|
|
285
|
+
async_drop(@async_entry) if @async_entry
|
|
286
|
+
async_attach!(configure: false)
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
#: (bool restore_transactions) -> void
|
|
290
|
+
def async_reset_detached(restore_transactions)
|
|
291
|
+
enable_lazy_transactions!
|
|
292
|
+
reset_transaction(restore: restore_transactions) { clear_cache!(new_connection: true) }
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
#: () -> bool
|
|
296
|
+
def async_detached?
|
|
297
|
+
!async_raw_pool.nil? && @async_entry.nil?
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
# Counts as a statement in flight for the block, then detaches if nothing else holds the connection.
|
|
301
|
+
#
|
|
302
|
+
#: [T] () { () -> T } -> T
|
|
303
|
+
def async_hold
|
|
304
|
+
@async_depth += 1
|
|
305
|
+
begin
|
|
306
|
+
yield
|
|
307
|
+
ensure
|
|
308
|
+
@async_depth -= 1
|
|
309
|
+
async_detach_if_idle
|
|
310
|
+
end
|
|
311
|
+
end
|
|
312
|
+
|
|
313
|
+
#: [T] () { () -> T } -> T
|
|
314
|
+
def async_beginning
|
|
315
|
+
return yield unless async_raw_pool
|
|
316
|
+
|
|
317
|
+
async_hold do
|
|
318
|
+
result = yield
|
|
319
|
+
@async_in_transaction = true
|
|
320
|
+
result
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
#: (?transaction_over: bool) -> void
|
|
325
|
+
def async_detach_if_idle(transaction_over: false)
|
|
326
|
+
return unless (entry = @async_entry)
|
|
327
|
+
return if @async_depth.positive? || raw_pinned? || @async_in_transaction
|
|
328
|
+
return if !transaction_over && async_materialized_transaction?
|
|
329
|
+
|
|
330
|
+
@lock.synchronize { async_detach(entry) }
|
|
331
|
+
end
|
|
332
|
+
|
|
333
|
+
# Materialized transactions are a prefix of the stack, but a lazy savepoint can sit on a materialized parent.
|
|
334
|
+
#
|
|
335
|
+
#: () -> bool
|
|
336
|
+
def async_materialized_transaction?
|
|
337
|
+
return false unless transaction_open?
|
|
338
|
+
|
|
339
|
+
transaction_manager.instance_variable_get(:@stack).any?(&:materialized?)
|
|
340
|
+
end
|
|
341
|
+
end
|
|
342
|
+
end
|
|
343
|
+
end
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
module AsyncConnectionPool
|
|
6
|
+
# The pool's settings for one database, read from its database.yml entry.
|
|
7
|
+
class DatabaseOptions
|
|
8
|
+
DEFAULT_MAX_RAW_CONNECTIONS = 5
|
|
9
|
+
DEFAULT_ADAPTER_POOL_SIZE = 1024
|
|
10
|
+
|
|
11
|
+
# Raises ArgumentError unless the value is a supported session state, and returns it as a symbol.
|
|
12
|
+
#
|
|
13
|
+
#: (untyped value) -> Symbol
|
|
14
|
+
def self.validate_session_state!(value)
|
|
15
|
+
state = value.to_s.to_sym
|
|
16
|
+
return state if SESSION_STATES.include?(state)
|
|
17
|
+
|
|
18
|
+
raise ArgumentError, "session_state must be one of #{SESSION_STATES.join(', ')} (got #{value.inspect})"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# Validates `session_state` and `max_raw_connections` only when enabled, so a pool without `raw_pool` never
|
|
22
|
+
# raises. Options from a database URL arrive as strings, so they are cast: booleans as ActiveModel casts them
|
|
23
|
+
# (`"false"`, `"0"`, `"off"` and `""` are false), integers in base 10.
|
|
24
|
+
#
|
|
25
|
+
#: (ActiveRecord::DatabaseConfigurations::DatabaseConfig db_config) -> void
|
|
26
|
+
def initialize(db_config)
|
|
27
|
+
@config = db_config.configuration_hash
|
|
28
|
+
@enabled = truthy?(@config[:raw_pool])
|
|
29
|
+
state = @config.fetch(:session_state) { AsyncConnectionPool.session_state }
|
|
30
|
+
@session_state = enabled? ? self.class.validate_session_state!(state) : AsyncConnectionPool.session_state
|
|
31
|
+
@max_raw_connections = enabled? ? cast_max_raw_connections : DEFAULT_MAX_RAW_CONNECTIONS
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
#: () -> bool
|
|
35
|
+
def enabled?
|
|
36
|
+
@enabled
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
#: Integer
|
|
40
|
+
attr_reader :max_raw_connections
|
|
41
|
+
|
|
42
|
+
#: () -> Symbol
|
|
43
|
+
attr_reader :session_state
|
|
44
|
+
|
|
45
|
+
#: () -> bool
|
|
46
|
+
def reset_on_checkin?
|
|
47
|
+
truthy?(@config.fetch(:reset_on_checkin, AsyncConnectionPool.reset_on_checkin))
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The adapter pool is sized by `max_connections` when set, otherwise it is raised to a large default when
|
|
51
|
+
# enabled, as the raw pool is what limits real connections. Nil leaves ActiveRecord's value alone.
|
|
52
|
+
#
|
|
53
|
+
#: () -> Integer?
|
|
54
|
+
def adapter_pool_size
|
|
55
|
+
return unless enabled?
|
|
56
|
+
return if @config.key?(:max_connections) || @config.key?(:pool)
|
|
57
|
+
|
|
58
|
+
DEFAULT_ADAPTER_POOL_SIZE
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
private
|
|
62
|
+
|
|
63
|
+
#: (untyped value) -> bool
|
|
64
|
+
def truthy?(value)
|
|
65
|
+
ActiveModel::Type::Boolean.new.cast(value) ? true : false
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Raises ArgumentError unless the value is a positive integer.
|
|
69
|
+
#
|
|
70
|
+
#: () -> Integer
|
|
71
|
+
def cast_max_raw_connections
|
|
72
|
+
value = @config.fetch(:max_raw_connections, DEFAULT_MAX_RAW_CONNECTIONS)
|
|
73
|
+
count = value.is_a?(String) ? Integer(value, 10, exception: false) : Integer(value, exception: false)
|
|
74
|
+
return count if count&.positive?
|
|
75
|
+
|
|
76
|
+
raise ArgumentError, "max_raw_connections must be a positive integer (got #{value.inspect})"
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
module AsyncConnectionPool
|
|
6
|
+
class Error < ActiveRecord::ActiveRecordError; end
|
|
7
|
+
|
|
8
|
+
# Raised when the database adapter is not one the pool supports.
|
|
9
|
+
class UnsupportedAdapterError < Error; end
|
|
10
|
+
|
|
11
|
+
# Raised when a fiber changes session state while `session_state` is `:raise`.
|
|
12
|
+
class UnpinnedSessionStateError < Error; end
|
|
13
|
+
|
|
14
|
+
# Raised when `postgresql_detector` is `:pg_query` but the pg_query gem can't be loaded.
|
|
15
|
+
class DetectorUnavailableError < Error; end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module ActiveRecord
|
|
5
|
+
module AsyncConnectionPool
|
|
6
|
+
# What differs between database drivers: health checks, cleanliness, reset, closing and per-session state.
|
|
7
|
+
# Subclasses register themselves for an adapter class; the core supplies attach, detach, pinning and the pool.
|
|
8
|
+
class Handler
|
|
9
|
+
Registration = Struct.new(:adapter_class_name, :handler_class, :excludes)
|
|
10
|
+
|
|
11
|
+
@registrations = {} #: Hash[String, Registration]
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# Registered handlers by adapter class name.
|
|
15
|
+
#
|
|
16
|
+
#: () -> Hash[String, Registration]
|
|
17
|
+
attr_reader :registrations # :nodoc:
|
|
18
|
+
|
|
19
|
+
# Registers the handler for the named adapter class and its subclasses, except those in `excludes` (classes
|
|
20
|
+
# or class names). The adapter extension is prepended when the adapter class loads.
|
|
21
|
+
#
|
|
22
|
+
#: (String adapter_class_name, Class handler_class, load_hook: Symbol, ?excludes: Array[Class | String]) -> void
|
|
23
|
+
def register(adapter_class_name, handler_class, load_hook:, excludes: [])
|
|
24
|
+
registrations[adapter_class_name] = Registration.new(adapter_class_name, handler_class, excludes)
|
|
25
|
+
ActiveSupport.on_load(load_hook) { prepend AdapterExtension }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# The handler class of the nearest registered ancestor of the adapter class.
|
|
29
|
+
#
|
|
30
|
+
#: (Class adapter_class) -> Class
|
|
31
|
+
def for(adapter_class)
|
|
32
|
+
registration, = registrations.values.filter_map do |reg|
|
|
33
|
+
registered = reg.adapter_class_name.safe_constantize
|
|
34
|
+
[reg, adapter_class.ancestors.index(registered)] if registered.is_a?(Class) && adapter_class <= registered
|
|
35
|
+
end.min_by(&:last)
|
|
36
|
+
|
|
37
|
+
unless registration
|
|
38
|
+
raise UnsupportedAdapterError, "#{adapter_class.name} does not support raw_pool: no handler is registered"
|
|
39
|
+
end
|
|
40
|
+
if excluded?(adapter_class, registration.excludes)
|
|
41
|
+
raise UnsupportedAdapterError,
|
|
42
|
+
"#{adapter_class.name} does not support raw_pool: #{registration.handler_class.name} excludes it"
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
registration.handler_class
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
#: (Class adapter_class, Array[Class | String] excludes) -> bool
|
|
51
|
+
def excluded?(adapter_class, excludes)
|
|
52
|
+
excludes.any? do |excluded|
|
|
53
|
+
excluded = excluded.safe_constantize if excluded.is_a?(String)
|
|
54
|
+
excluded.is_a?(Class) && adapter_class <= excluded
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
#: ActiveRecord::DatabaseConfigurations::DatabaseConfig
|
|
60
|
+
attr_reader :db_config
|
|
61
|
+
|
|
62
|
+
#: DatabaseOptions
|
|
63
|
+
attr_reader :options
|
|
64
|
+
|
|
65
|
+
#: (ActiveRecord::DatabaseConfigurations::DatabaseConfig db_config, DatabaseOptions options) -> void
|
|
66
|
+
def initialize(db_config, options)
|
|
67
|
+
@db_config = db_config
|
|
68
|
+
@options = options
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The raw pool's size, given the size the database config asked for.
|
|
72
|
+
#
|
|
73
|
+
#: (Integer requested) -> Integer
|
|
74
|
+
def max_raw_connections(requested)
|
|
75
|
+
requested
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Called after the adapter attaches the entry, before it runs anything on it.
|
|
79
|
+
#
|
|
80
|
+
#: (untyped adapter, RawEntry entry) -> void
|
|
81
|
+
def adopt(adapter, entry); end
|
|
82
|
+
|
|
83
|
+
# Called while the adapter is still attached, before the entry goes back to the pool or is closed.
|
|
84
|
+
#
|
|
85
|
+
#: (untyped adapter, RawEntry entry) -> void
|
|
86
|
+
def release(adapter, entry); end
|
|
87
|
+
|
|
88
|
+
# Whether the entry can be reused by another adapter as it is.
|
|
89
|
+
#
|
|
90
|
+
#: (RawEntry _entry) -> bool
|
|
91
|
+
def clean?(_entry)
|
|
92
|
+
true
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Brings the entry back to a known state through the still-attached adapter. False means it must be closed.
|
|
96
|
+
#
|
|
97
|
+
#: (untyped _adapter, RawEntry _entry) -> bool
|
|
98
|
+
def reset(_adapter, _entry) # rubocop:disable Naming/PredicateMethod
|
|
99
|
+
false
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
#: (untyped _raw) -> void
|
|
103
|
+
def close(_raw)
|
|
104
|
+
raise NotImplementedError, "#{self.class.name}#close"
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Closes the raw connection of an entry the raw pool is done with, given the state kept with it.
|
|
108
|
+
#
|
|
109
|
+
#: (RawEntry entry) -> void
|
|
110
|
+
def close_entry(entry)
|
|
111
|
+
close(entry.raw)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Whether the raw pool may close the idle entry for `idle_timeout` or `max_age`. Health checks apply regardless.
|
|
115
|
+
#
|
|
116
|
+
#: (RawEntry _entry) -> bool
|
|
117
|
+
def expirable?(_entry)
|
|
118
|
+
true
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
#: (untyped _raw) -> bool
|
|
122
|
+
def healthy?(_raw)
|
|
123
|
+
raise NotImplementedError, "#{self.class.name}#healthy?"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Lets go of a raw connection inherited across fork without closing it, so the parent's session is untouched
|
|
127
|
+
# even when the child later frees the driver object.
|
|
128
|
+
#
|
|
129
|
+
#: (untyped _raw) -> void
|
|
130
|
+
def discard(_raw); end
|
|
131
|
+
|
|
132
|
+
# Whether a statement is still running on the raw connection, as when the fiber that sent it was interrupted.
|
|
133
|
+
#
|
|
134
|
+
#: (untyped _raw) -> bool
|
|
135
|
+
def in_flight?(_raw)
|
|
136
|
+
false
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# How the database lexes quotes and comments, for `SessionState`: `:postgresql`, `:sqlite`, `:mysql` (or one of
|
|
140
|
+
# its `sql_mode` variants), or `:any` to read SQL as all of them would.
|
|
141
|
+
#
|
|
142
|
+
#: () -> Symbol
|
|
143
|
+
def sql_dialect
|
|
144
|
+
:any
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Patterns for statements that leave state in the database session, matched case-insensitively at the start of
|
|
148
|
+
# each statement.
|
|
149
|
+
#
|
|
150
|
+
#: () -> Array[Regexp | String]
|
|
151
|
+
def session_state_patterns
|
|
152
|
+
[]
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
# Patterns matched case-insensitively anywhere in each statement's code, with quoted text and comments blanked
|
|
156
|
+
# (see `SessionState.code`), for forms such as function calls that need not start the statement. `\A` anchors
|
|
157
|
+
# one at the start of the statement.
|
|
158
|
+
#
|
|
159
|
+
#: () -> Array[Regexp | String]
|
|
160
|
+
def session_state_code_patterns
|
|
161
|
+
[]
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Whether the statement leaves state in the database session that later statements may depend on. `sql` may hold
|
|
165
|
+
# several statements. Not called for statements Rails or the handler issue themselves (`configure_connection`,
|
|
166
|
+
# `reset`).
|
|
167
|
+
#
|
|
168
|
+
#: (String sql) -> bool
|
|
169
|
+
def session_state_statement?(sql)
|
|
170
|
+
SessionState.matches?(sql, session_state_patterns, dialect: sql_dialect) ||
|
|
171
|
+
SessionState.code_matches?(sql, session_state_code_patterns, dialect: sql_dialect)
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# As `session_state_statement?`, for SQL about to run on the entry's raw connection: the core detects through
|
|
175
|
+
# this, so a handler whose lexing depends on the session (MySQL's `sql_mode`) can read it from the entry.
|
|
176
|
+
#
|
|
177
|
+
#: (String sql, RawEntry _entry) -> bool
|
|
178
|
+
def session_state_statement_on?(sql, _entry)
|
|
179
|
+
session_state_statement?(sql)
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|