fresh_connection 3.1.2 → 3.1.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/README.md +190 -152
- data/fresh_connection.gemspec +3 -3
- data/lib/fresh_connection/access_control.rb +144 -21
- data/lib/fresh_connection/connection_manager.rb +6 -2
- data/lib/fresh_connection/connection_specification/rails_70.rb +48 -0
- data/lib/fresh_connection/connection_specification.rb +10 -2
- data/lib/fresh_connection/deprecator.rb +10 -0
- data/lib/fresh_connection/executor_hook.rb +4 -1
- data/lib/fresh_connection/extend/adapters/base_adapter.rb +116 -11
- data/lib/fresh_connection/extend/adapters/m2_adapter.rb +2 -11
- data/lib/fresh_connection/extend/adapters/pg_adapter.rb +2 -14
- data/lib/fresh_connection/extend/adapters/trilogy_adapter.rb +15 -0
- data/lib/fresh_connection/extend/ar_base.rb +115 -3
- data/lib/fresh_connection/extend/ar_connection_handler.rb +42 -9
- data/lib/fresh_connection/extend/ar_relation.rb +105 -13
- data/lib/fresh_connection/extend/ar_relation_merger.rb +1 -1
- data/lib/fresh_connection/extend/ar_statement_cache.rb +39 -2
- data/lib/fresh_connection/extend.rb +49 -3
- data/lib/fresh_connection/rails_replica.rb +559 -0
- data/lib/fresh_connection/railtie.rb +13 -1
- data/lib/fresh_connection/replica_connection_handler.rb +1 -0
- data/lib/fresh_connection/version.rb +1 -1
- data/lib/fresh_connection.rb +46 -2
- metadata +22 -19
- data/.travis.yml +0 -24
- data/Appraisals +0 -12
- data/benchmarks/benchmark.rb +0 -25
- data/bin/test +0 -24
- data/gemfiles/rails52.gemfile +0 -8
- data/gemfiles/rails60.gemfile +0 -7
- data/gemfiles/rails61.gemfile +0 -7
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FreshConnection
|
|
4
|
+
module RailsReplica
|
|
5
|
+
MYSQL_CONNECTION_LOST_PATTERN = /lost connection|server has gone away|can't connect|closed mysql connection/i
|
|
6
|
+
private_constant :MYSQL_CONNECTION_LOST_PATTERN
|
|
7
|
+
|
|
8
|
+
class << self
|
|
9
|
+
def supported?
|
|
10
|
+
ActiveRecord::VERSION::MAJOR >= 7 ||
|
|
11
|
+
(ActiveRecord::VERSION::MAJOR == 6 && ActiveRecord::VERSION::MINOR >= 1)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def use_rails_replica?(model)
|
|
15
|
+
return false unless supported?
|
|
16
|
+
return false if FreshConnection.custom_connection_manager?
|
|
17
|
+
return false if model.explicit_fresh_connection?
|
|
18
|
+
|
|
19
|
+
!reading_pool_for(model).nil?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def legacy_replica_enabled?(model)
|
|
23
|
+
return true unless supported?
|
|
24
|
+
return false if use_rails_replica?(model)
|
|
25
|
+
return true if FreshConnection.custom_connection_manager?
|
|
26
|
+
return true if model.explicit_fresh_connection?
|
|
27
|
+
|
|
28
|
+
conn_class = connection_class_for(model)
|
|
29
|
+
is_primary = (conn_class == ActiveRecord::Base) ||
|
|
30
|
+
(conn_class.respond_to?(:primary_class?) && conn_class.primary_class?)
|
|
31
|
+
return false unless is_primary
|
|
32
|
+
|
|
33
|
+
reading_pool_for(ActiveRecord::Base).nil?
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def reading_role
|
|
37
|
+
if ActiveRecord.respond_to?(:reading_role)
|
|
38
|
+
ActiveRecord.reading_role
|
|
39
|
+
elsif ActiveRecord::Base.respond_to?(:reading_role)
|
|
40
|
+
ActiveRecord::Base.reading_role
|
|
41
|
+
else
|
|
42
|
+
:reading
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def writing_role
|
|
47
|
+
if ActiveRecord.respond_to?(:writing_role)
|
|
48
|
+
ActiveRecord.writing_role
|
|
49
|
+
elsif ActiveRecord::Base.respond_to?(:writing_role)
|
|
50
|
+
ActiveRecord::Base.writing_role
|
|
51
|
+
else
|
|
52
|
+
:writing
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def in_reading_role?(model)
|
|
57
|
+
return false unless supported?
|
|
58
|
+
|
|
59
|
+
model_role = model.respond_to?(:current_role) ? model.current_role : ActiveRecord::Base.current_role
|
|
60
|
+
model_role == reading_role
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def any_reading_or_preventing_writes?
|
|
64
|
+
return false unless supported?
|
|
65
|
+
|
|
66
|
+
if !legacy_connection_handling? && ActiveRecord::Base.respond_to?(:connected_to_stack, true)
|
|
67
|
+
stack = ActiveRecord::Base.send(:connected_to_stack)
|
|
68
|
+
return false if stack.nil? || stack.empty?
|
|
69
|
+
|
|
70
|
+
r_role = reading_role
|
|
71
|
+
return stack.any? { |entry| entry[:role] == r_role || entry[:prevent_writes] == true }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
(ActiveRecord::Base.respond_to?(:current_role) && ActiveRecord::Base.current_role == reading_role) ||
|
|
75
|
+
(ActiveRecord::Base.respond_to?(:current_preventing_writes) && ActiveRecord::Base.current_preventing_writes)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def reading_pool_for(model)
|
|
79
|
+
return nil unless supported?
|
|
80
|
+
|
|
81
|
+
role = reading_role
|
|
82
|
+
conn_class = connection_class_for(model)
|
|
83
|
+
spec_name = conn_class.connection_specification_name
|
|
84
|
+
shard = current_shard_for(model)
|
|
85
|
+
|
|
86
|
+
if legacy_connection_handling?
|
|
87
|
+
handler = ActiveRecord::Base.connection_handlers[role]
|
|
88
|
+
handler && handler.retrieve_connection_pool(spec_name, role: role, shard: shard)
|
|
89
|
+
else
|
|
90
|
+
ActiveRecord::Base.connection_handler.retrieve_connection_pool(spec_name, role: role, shard: shard)
|
|
91
|
+
end
|
|
92
|
+
rescue ActiveRecord::ConnectionNotDefined, ActiveRecord::ConnectionNotEstablished
|
|
93
|
+
nil
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def writing_pool_for(model)
|
|
97
|
+
return model.connection_pool unless supported?
|
|
98
|
+
|
|
99
|
+
role = writing_role
|
|
100
|
+
conn_class = connection_class_for(model)
|
|
101
|
+
spec_name = conn_class.connection_specification_name
|
|
102
|
+
shard = current_shard_for(model)
|
|
103
|
+
|
|
104
|
+
if legacy_connection_handling?
|
|
105
|
+
handler = ActiveRecord::Base.connection_handlers[role]
|
|
106
|
+
handler && handler.retrieve_connection_pool(spec_name, role: role, shard: shard)
|
|
107
|
+
else
|
|
108
|
+
ActiveRecord::Base.connection_handler.retrieve_connection_pool(spec_name, role: role, shard: shard)
|
|
109
|
+
end
|
|
110
|
+
rescue ActiveRecord::ConnectionNotDefined, ActiveRecord::ConnectionNotEstablished
|
|
111
|
+
nil
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def with_reading(model, &block)
|
|
115
|
+
pool = reading_pool_for(model)
|
|
116
|
+
return yield unless pool
|
|
117
|
+
|
|
118
|
+
track_reading_pool(pool) if FreshConnection.put_aside
|
|
119
|
+
|
|
120
|
+
target = connected_to_target(model)
|
|
121
|
+
role = reading_role
|
|
122
|
+
if target.respond_to?(:current_role) && target.current_role == role
|
|
123
|
+
return yield
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
with_internal_connected_to do
|
|
127
|
+
target.connected_to(role: role, prevent_writes: true, &block)
|
|
128
|
+
end
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def with_writing(model, &block)
|
|
132
|
+
role = writing_role
|
|
133
|
+
model_role = model.respond_to?(:current_role) ? model.current_role : ActiveRecord::Base.current_role
|
|
134
|
+
preventing = model.respond_to?(:current_preventing_writes) && model.current_preventing_writes
|
|
135
|
+
|
|
136
|
+
if model_role == role && !preventing &&
|
|
137
|
+
(!ActiveRecord::Base.respond_to?(:current_role) || ActiveRecord::Base.current_role == role) &&
|
|
138
|
+
!any_reading_or_preventing_writes?
|
|
139
|
+
return yield
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
with_internal_connected_to do
|
|
143
|
+
ActiveRecord::Base.connected_to(role: role, prevent_writes: false, &block)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def with_user_connected_to(klass, role)
|
|
148
|
+
if internal_connected_to?
|
|
149
|
+
state_set(:fresh_connection_internal_connected_to, false)
|
|
150
|
+
return yield
|
|
151
|
+
end
|
|
152
|
+
return yield if role.nil?
|
|
153
|
+
|
|
154
|
+
stack = user_connected_to_stack
|
|
155
|
+
stack.push({ klass: klass, role: role.to_sym })
|
|
156
|
+
yield
|
|
157
|
+
ensure
|
|
158
|
+
stack.pop if stack
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def explicitly_connected_to_writing?(model)
|
|
162
|
+
return false unless supported?
|
|
163
|
+
|
|
164
|
+
stack = state_get(:fresh_connection_user_connected_to_stack)
|
|
165
|
+
return false if stack.nil? || stack.empty?
|
|
166
|
+
|
|
167
|
+
conn_class = connection_class_for(model)
|
|
168
|
+
role = writing_role
|
|
169
|
+
|
|
170
|
+
stack.reverse_each do |entry|
|
|
171
|
+
entry_klass = entry[:klass]
|
|
172
|
+
matches = (entry_klass == ActiveRecord::Base) ||
|
|
173
|
+
(entry_klass == conn_class) ||
|
|
174
|
+
(conn_class == ActiveRecord::Base && entry_klass.respond_to?(:primary_class?) && entry_klass.primary_class?)
|
|
175
|
+
return entry[:role] == role if matches
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
false
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def active_connection_for(pool)
|
|
182
|
+
return nil unless pool
|
|
183
|
+
|
|
184
|
+
active = pool.active_connection?
|
|
185
|
+
return nil unless active
|
|
186
|
+
return active if active.respond_to?(:open_transactions)
|
|
187
|
+
|
|
188
|
+
if pool.respond_to?(:active_connection)
|
|
189
|
+
pool.active_connection
|
|
190
|
+
elsif pool.respond_to?(:lease_connection)
|
|
191
|
+
pool.lease_connection
|
|
192
|
+
else
|
|
193
|
+
pool.connection
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
def pool_in_transaction?(pool)
|
|
198
|
+
conn = active_connection_for(pool)
|
|
199
|
+
return false unless conn
|
|
200
|
+
|
|
201
|
+
conn.open_transactions > 0
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
def in_transaction?(model)
|
|
205
|
+
pool = writing_pool_for(model)
|
|
206
|
+
return true if pool_in_transaction?(pool)
|
|
207
|
+
|
|
208
|
+
if supported?
|
|
209
|
+
writing_pools.any? do |other_pool|
|
|
210
|
+
other_pool != pool && same_writing_database?(pool, other_pool) && pool_in_transaction?(other_pool)
|
|
211
|
+
end
|
|
212
|
+
else
|
|
213
|
+
base_pool = writing_pool_for(ActiveRecord::Base)
|
|
214
|
+
base_pool && base_pool != pool && pool_in_transaction?(base_pool)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def recovery?(model, exception: nil)
|
|
219
|
+
return false if defined?(ActiveRecord::ConnectionNotDefined) && exception.is_a?(ActiveRecord::ConnectionNotDefined)
|
|
220
|
+
|
|
221
|
+
pool = reading_pool_for(model)
|
|
222
|
+
return false unless pool
|
|
223
|
+
|
|
224
|
+
conn = active_connection_for(pool)
|
|
225
|
+
if connection_error?(exception)
|
|
226
|
+
if conn
|
|
227
|
+
safe_put_aside_pool!(pool)
|
|
228
|
+
else
|
|
229
|
+
discard_dead_idle_connections!(pool)
|
|
230
|
+
end
|
|
231
|
+
return true
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
if conn
|
|
235
|
+
return false if (conn.active? rescue false)
|
|
236
|
+
|
|
237
|
+
safe_put_aside_pool!(pool)
|
|
238
|
+
return true
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Rails 7.2+ with_connection already returned or discarded the connection on exception.
|
|
242
|
+
# Inspect idle connections directly without calling pool.with_connection (which auto-verifies/reconnects).
|
|
243
|
+
candidates = pool.synchronize do
|
|
244
|
+
pool.connections.select { |c| !c.in_use? && !c.transaction_open? }
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# If the pool has no connections left (e.g. broken connection was already discarded), allow retry.
|
|
248
|
+
return true if candidates.empty?
|
|
249
|
+
|
|
250
|
+
discard_dead_connections_from_candidates!(pool, candidates)
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def put_aside!
|
|
254
|
+
return unless supported?
|
|
255
|
+
return unless FreshConnection.put_aside
|
|
256
|
+
|
|
257
|
+
tracked_pools = take_tracked_reading_pools
|
|
258
|
+
reading_pools.each do |pool|
|
|
259
|
+
put_aside_pool!(pool, was_tracked: tracked_pools.include?(pool))
|
|
260
|
+
end
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
def put_aside_pool!(pool, was_tracked: false)
|
|
264
|
+
return unless pool
|
|
265
|
+
|
|
266
|
+
conn = active_connection_for(pool)
|
|
267
|
+
if conn
|
|
268
|
+
return if conn.transaction_open?
|
|
269
|
+
|
|
270
|
+
pool.remove(conn)
|
|
271
|
+
conn.expire if conn.respond_to?(:expire)
|
|
272
|
+
conn.disconnect! rescue nil
|
|
273
|
+
elsif was_tracked
|
|
274
|
+
idle = pool.synchronize do
|
|
275
|
+
pool.connections.find { |c| !c.in_use? && !c.transaction_open? }
|
|
276
|
+
end
|
|
277
|
+
if idle
|
|
278
|
+
removed = pool.synchronize do
|
|
279
|
+
if !idle.in_use? && !idle.transaction_open? && pool.connections.include?(idle)
|
|
280
|
+
pool.remove(idle)
|
|
281
|
+
true
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
idle.disconnect! rescue nil if removed
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
def clear_all_connections!
|
|
290
|
+
return unless supported?
|
|
291
|
+
|
|
292
|
+
reading_pools.each(&:disconnect!)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def with_reading_pools_query_cache(enable:, dirties: true, &block)
|
|
296
|
+
return yield unless supported?
|
|
297
|
+
|
|
298
|
+
pools = reading_pools
|
|
299
|
+
return yield if pools.empty?
|
|
300
|
+
|
|
301
|
+
apply_query_cache_to_pools(pools, 0, enable, dirties, &block)
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
def connection_class_for(model)
|
|
305
|
+
if model.respond_to?(:connection_class_for_self)
|
|
306
|
+
model.connection_class_for_self
|
|
307
|
+
elsif model.respond_to?(:connection_classes)
|
|
308
|
+
model.connection_classes
|
|
309
|
+
else
|
|
310
|
+
klass = model
|
|
311
|
+
while klass != ActiveRecord::Base && !klass.abstract_class?
|
|
312
|
+
klass = klass.superclass
|
|
313
|
+
end
|
|
314
|
+
klass
|
|
315
|
+
end
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
private
|
|
319
|
+
|
|
320
|
+
def safe_put_aside_pool!(pool)
|
|
321
|
+
put_aside_pool!(pool)
|
|
322
|
+
rescue StandardError
|
|
323
|
+
nil
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def connected_to_target(model)
|
|
327
|
+
return ActiveRecord::Base if legacy_connection_handling?
|
|
328
|
+
|
|
329
|
+
conn_class = connection_class_for(model)
|
|
330
|
+
if conn_class == ActiveRecord::Base
|
|
331
|
+
ActiveRecord::Base
|
|
332
|
+
elsif conn_class.abstract_class? &&
|
|
333
|
+
(conn_class.name == conn_class.connection_specification_name ||
|
|
334
|
+
(conn_class.respond_to?(:primary_class?) && conn_class.primary_class?))
|
|
335
|
+
conn_class
|
|
336
|
+
else
|
|
337
|
+
ActiveRecord::Base
|
|
338
|
+
end
|
|
339
|
+
end
|
|
340
|
+
|
|
341
|
+
def legacy_connection_handling?
|
|
342
|
+
if ActiveRecord.respond_to?(:legacy_connection_handling)
|
|
343
|
+
ActiveRecord.legacy_connection_handling
|
|
344
|
+
elsif ActiveRecord::Base.respond_to?(:legacy_connection_handling)
|
|
345
|
+
ActiveRecord::Base.legacy_connection_handling
|
|
346
|
+
else
|
|
347
|
+
false
|
|
348
|
+
end
|
|
349
|
+
end
|
|
350
|
+
|
|
351
|
+
def current_shard_for(model)
|
|
352
|
+
if model.respond_to?(:current_shard)
|
|
353
|
+
model.current_shard
|
|
354
|
+
elsif ActiveRecord::Base.respond_to?(:default_shard)
|
|
355
|
+
ActiveRecord::Base.default_shard
|
|
356
|
+
else
|
|
357
|
+
:default
|
|
358
|
+
end
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
def same_writing_database?(pool_a, pool_b)
|
|
362
|
+
return false if pool_a.nil? || pool_b.nil?
|
|
363
|
+
return false unless pool_a.respond_to?(:db_config) && pool_b.respond_to?(:db_config)
|
|
364
|
+
|
|
365
|
+
cfg_a = pool_a.db_config
|
|
366
|
+
cfg_b = pool_b.db_config
|
|
367
|
+
return false unless cfg_a.name == cfg_b.name
|
|
368
|
+
|
|
369
|
+
if cfg_a.respond_to?(:configuration_hash) && cfg_b.respond_to?(:configuration_hash)
|
|
370
|
+
ha = cfg_a.configuration_hash
|
|
371
|
+
hb = cfg_b.configuration_hash
|
|
372
|
+
%i[adapter host port database].all? { |k| ha[k] == hb[k] }
|
|
373
|
+
else
|
|
374
|
+
true
|
|
375
|
+
end
|
|
376
|
+
end
|
|
377
|
+
|
|
378
|
+
def connection_error?(exception)
|
|
379
|
+
return false unless exception
|
|
380
|
+
|
|
381
|
+
current = exception
|
|
382
|
+
visited = 0
|
|
383
|
+
while current && visited < 5
|
|
384
|
+
return true if match_connection_error_class?(current)
|
|
385
|
+
|
|
386
|
+
current = current.respond_to?(:cause) ? current.cause : nil
|
|
387
|
+
visited += 1
|
|
388
|
+
end
|
|
389
|
+
false
|
|
390
|
+
end
|
|
391
|
+
|
|
392
|
+
def match_connection_error_class?(err)
|
|
393
|
+
return false if defined?(ActiveRecord::ConnectionNotDefined) && err.is_a?(ActiveRecord::ConnectionNotDefined)
|
|
394
|
+
return true if err.is_a?(ActiveRecord::ConnectionNotEstablished)
|
|
395
|
+
return true if defined?(ActiveRecord::ConnectionFailed) && err.is_a?(ActiveRecord::ConnectionFailed)
|
|
396
|
+
return true if defined?(::Mysql2::Error::ConnectionError) && err.is_a?(::Mysql2::Error::ConnectionError)
|
|
397
|
+
return true if defined?(::Mysql2::Error) && err.is_a?(::Mysql2::Error) && err.message.to_s.match?(MYSQL_CONNECTION_LOST_PATTERN)
|
|
398
|
+
return true if defined?(::Trilogy::ConnectionError) && err.is_a?(::Trilogy::ConnectionError)
|
|
399
|
+
return true if defined?(::Trilogy::EOFError) && err.is_a?(::Trilogy::EOFError)
|
|
400
|
+
return true if defined?(::Trilogy::TimeoutError) && err.is_a?(::Trilogy::TimeoutError)
|
|
401
|
+
return true if defined?(::Trilogy::SyscallError) && err.is_a?(::Trilogy::SyscallError)
|
|
402
|
+
return true if defined?(::PG::ConnectionBad) && err.is_a?(::PG::ConnectionBad)
|
|
403
|
+
return true if defined?(::PG::UnableToSend) && err.is_a?(::PG::UnableToSend)
|
|
404
|
+
return true if defined?(::PG::AdminShutdown) && err.is_a?(::PG::AdminShutdown)
|
|
405
|
+
return true if defined?(::PG::CrashShutdown) && err.is_a?(::PG::CrashShutdown)
|
|
406
|
+
return true if defined?(::PG::CannotConnectNow) && err.is_a?(::PG::CannotConnectNow)
|
|
407
|
+
|
|
408
|
+
false
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
def reading_pools
|
|
412
|
+
pools_for_role(reading_role)
|
|
413
|
+
end
|
|
414
|
+
|
|
415
|
+
def writing_pools
|
|
416
|
+
pools_for_role(writing_role)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
def pools_for_role(role)
|
|
420
|
+
if legacy_connection_handling?
|
|
421
|
+
handler = ActiveRecord::Base.connection_handlers[role]
|
|
422
|
+
handler ? handler.connection_pool_list(role) : []
|
|
423
|
+
else
|
|
424
|
+
ActiveRecord::Base.connection_handler.connection_pool_list(role)
|
|
425
|
+
end
|
|
426
|
+
rescue StandardError
|
|
427
|
+
[]
|
|
428
|
+
end
|
|
429
|
+
|
|
430
|
+
def apply_query_cache_to_pools(pools, index, enable, dirties, &block)
|
|
431
|
+
return yield if index >= pools.length
|
|
432
|
+
|
|
433
|
+
pool = pools[index]
|
|
434
|
+
next_step = -> { apply_query_cache_to_pools(pools, index + 1, enable, dirties, &block) }
|
|
435
|
+
|
|
436
|
+
if enable
|
|
437
|
+
if pool.respond_to?(:enable_query_cache)
|
|
438
|
+
was_enabled = pool.respond_to?(:query_cache_enabled) && pool.query_cache_enabled
|
|
439
|
+
begin
|
|
440
|
+
pool.enable_query_cache(&next_step)
|
|
441
|
+
ensure
|
|
442
|
+
pool.clear_query_cache if !was_enabled && pool.respond_to?(:clear_query_cache)
|
|
443
|
+
end
|
|
444
|
+
elsif pool.respond_to?(:enable_query_cache!)
|
|
445
|
+
was_enabled = pool.respond_to?(:query_cache_enabled) && pool.query_cache_enabled
|
|
446
|
+
pool.enable_query_cache!
|
|
447
|
+
begin
|
|
448
|
+
next_step.call
|
|
449
|
+
ensure
|
|
450
|
+
unless was_enabled
|
|
451
|
+
pool.disable_query_cache!
|
|
452
|
+
active = active_connection_for(pool)
|
|
453
|
+
active.clear_query_cache if active && active.respond_to?(:clear_query_cache)
|
|
454
|
+
end
|
|
455
|
+
end
|
|
456
|
+
else
|
|
457
|
+
next_step.call
|
|
458
|
+
end
|
|
459
|
+
else
|
|
460
|
+
if pool.respond_to?(:disable_query_cache)
|
|
461
|
+
if pool.method(:disable_query_cache).parameters.any? { |type, name| (type == :key || type == :keyreq) && name == :dirties }
|
|
462
|
+
pool.disable_query_cache(dirties: dirties, &next_step)
|
|
463
|
+
else
|
|
464
|
+
pool.disable_query_cache(&next_step)
|
|
465
|
+
end
|
|
466
|
+
elsif pool.respond_to?(:disable_query_cache!)
|
|
467
|
+
was_enabled = pool.respond_to?(:query_cache_enabled) && pool.query_cache_enabled
|
|
468
|
+
pool.disable_query_cache!
|
|
469
|
+
begin
|
|
470
|
+
next_step.call
|
|
471
|
+
ensure
|
|
472
|
+
pool.enable_query_cache! if was_enabled
|
|
473
|
+
end
|
|
474
|
+
else
|
|
475
|
+
next_step.call
|
|
476
|
+
end
|
|
477
|
+
end
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
def with_internal_connected_to
|
|
481
|
+
prev = state_get(:fresh_connection_internal_connected_to)
|
|
482
|
+
state_set(:fresh_connection_internal_connected_to, true)
|
|
483
|
+
yield
|
|
484
|
+
ensure
|
|
485
|
+
state_set(:fresh_connection_internal_connected_to, prev)
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
def internal_connected_to?
|
|
489
|
+
!!state_get(:fresh_connection_internal_connected_to)
|
|
490
|
+
end
|
|
491
|
+
|
|
492
|
+
def user_connected_to_stack
|
|
493
|
+
stack = state_get(:fresh_connection_user_connected_to_stack)
|
|
494
|
+
unless stack
|
|
495
|
+
stack = []
|
|
496
|
+
state_set(:fresh_connection_user_connected_to_stack, stack)
|
|
497
|
+
end
|
|
498
|
+
stack
|
|
499
|
+
end
|
|
500
|
+
|
|
501
|
+
def track_reading_pool(pool)
|
|
502
|
+
tracked = state_get(:fresh_connection_tracked_reading_pools)
|
|
503
|
+
unless tracked
|
|
504
|
+
tracked = []
|
|
505
|
+
state_set(:fresh_connection_tracked_reading_pools, tracked)
|
|
506
|
+
end
|
|
507
|
+
tracked << pool unless tracked.include?(pool)
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
def take_tracked_reading_pools
|
|
511
|
+
tracked = state_get(:fresh_connection_tracked_reading_pools)
|
|
512
|
+
state_set(:fresh_connection_tracked_reading_pools, nil)
|
|
513
|
+
tracked || []
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
def discard_dead_idle_connections!(pool)
|
|
517
|
+
candidates = pool.synchronize do
|
|
518
|
+
pool.connections.select { |c| !c.in_use? && !c.transaction_open? }
|
|
519
|
+
end
|
|
520
|
+
discard_dead_connections_from_candidates!(pool, candidates)
|
|
521
|
+
end
|
|
522
|
+
|
|
523
|
+
def discard_dead_connections_from_candidates!(pool, candidates)
|
|
524
|
+
return false if candidates.empty?
|
|
525
|
+
|
|
526
|
+
# Check active? outside pool.synchronize to avoid holding pool mutex during network I/O
|
|
527
|
+
dead = candidates.reject { |c| c.active? rescue false }
|
|
528
|
+
return false if dead.empty?
|
|
529
|
+
|
|
530
|
+
removed = pool.synchronize do
|
|
531
|
+
dead.select do |c|
|
|
532
|
+
if !c.in_use? && !c.transaction_open? && pool.connections.include?(c)
|
|
533
|
+
pool.remove(c)
|
|
534
|
+
true
|
|
535
|
+
end
|
|
536
|
+
end
|
|
537
|
+
end
|
|
538
|
+
removed.each { |c| c.disconnect! rescue nil }
|
|
539
|
+
!removed.empty?
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
def state_get(key)
|
|
543
|
+
if defined?(ActiveSupport::IsolatedExecutionState)
|
|
544
|
+
ActiveSupport::IsolatedExecutionState[key]
|
|
545
|
+
else
|
|
546
|
+
Thread.current[key]
|
|
547
|
+
end
|
|
548
|
+
end
|
|
549
|
+
|
|
550
|
+
def state_set(key, value)
|
|
551
|
+
if defined?(ActiveSupport::IsolatedExecutionState)
|
|
552
|
+
ActiveSupport::IsolatedExecutionState[key] = value
|
|
553
|
+
else
|
|
554
|
+
Thread.current[key] = value
|
|
555
|
+
end
|
|
556
|
+
end
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
end
|
|
@@ -3,7 +3,19 @@ require 'fresh_connection/executor_hook'
|
|
|
3
3
|
|
|
4
4
|
module FreshConnection
|
|
5
5
|
class Railtie < Rails::Railtie
|
|
6
|
-
initializer "fresh_connection.
|
|
6
|
+
initializer "fresh_connection.deprecator", before: :load_environment_config do |app|
|
|
7
|
+
if app.respond_to?(:deprecators)
|
|
8
|
+
app.deprecators[:fresh_connection] = FreshConnection.deprecator
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
initializer "fresh_connection.deprecation_behavior", after: "active_support.deprecation_behavior" do |app|
|
|
13
|
+
if !app.respond_to?(:deprecators) && ActiveSupport::Deprecation.respond_to?(:behavior)
|
|
14
|
+
FreshConnection.deprecator.behavior = ActiveSupport::Deprecation.behavior
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
initializer "fresh_connection.configure_rails_initialization" do |_app|
|
|
7
19
|
ActiveSupport.on_load(:active_record) do
|
|
8
20
|
FreshConnection::ExecutorHook.install_executor_hooks
|
|
9
21
|
end
|
data/lib/fresh_connection.rb
CHANGED
|
@@ -1,9 +1,43 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'fresh_connection/version'
|
|
4
|
+
require 'fresh_connection/deprecator'
|
|
5
|
+
require 'fresh_connection/rails_replica'
|
|
6
|
+
|
|
3
7
|
module FreshConnection
|
|
8
|
+
DEFAULT_RETRY_LIMIT = 3
|
|
9
|
+
|
|
4
10
|
class << self
|
|
11
|
+
def configure
|
|
12
|
+
yield self
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def put_aside
|
|
16
|
+
defined?(@put_aside) ? @put_aside : true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def put_aside=(value)
|
|
20
|
+
@put_aside = !!value
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def recovery
|
|
24
|
+
defined?(@recovery) ? @recovery : true
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def recovery=(value)
|
|
28
|
+
@recovery = !!value
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def retry_limit
|
|
32
|
+
defined?(@retry_limit) ? @retry_limit : DEFAULT_RETRY_LIMIT
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def retry_limit=(value)
|
|
36
|
+
@retry_limit = [value ? value.to_i : 0, 0].max
|
|
37
|
+
end
|
|
38
|
+
|
|
5
39
|
def connection_manager
|
|
6
|
-
if defined?(@connection_manager)
|
|
40
|
+
if defined?(@connection_manager) && @connection_manager
|
|
7
41
|
@connection_manager
|
|
8
42
|
else
|
|
9
43
|
require 'fresh_connection/connection_manager'
|
|
@@ -12,9 +46,19 @@ module FreshConnection
|
|
|
12
46
|
end
|
|
13
47
|
|
|
14
48
|
def connection_manager=(mgr)
|
|
49
|
+
if mgr
|
|
50
|
+
FreshConnection.deprecator.warn(
|
|
51
|
+
"FreshConnection.connection_manager= is deprecated and will be removed in FreshConnection 3.2.0. " \
|
|
52
|
+
"Please use Rails' native `connects_to` connection pool management instead."
|
|
53
|
+
)
|
|
54
|
+
end
|
|
15
55
|
FreshConnection::ReplicaConnectionHandler.instance.refresh_all
|
|
16
56
|
@connection_manager = mgr
|
|
17
57
|
end
|
|
58
|
+
|
|
59
|
+
def custom_connection_manager?
|
|
60
|
+
defined?(@connection_manager) && !@connection_manager.nil?
|
|
61
|
+
end
|
|
18
62
|
end
|
|
19
63
|
end
|
|
20
64
|
|
|
@@ -22,4 +66,4 @@ require 'fresh_connection/replica_connection_handler'
|
|
|
22
66
|
require 'fresh_connection/abstract_connection_manager'
|
|
23
67
|
require 'fresh_connection/connection_specification'
|
|
24
68
|
require 'fresh_connection/extend'
|
|
25
|
-
require 'fresh_connection/railtie' if defined?(Rails)
|
|
69
|
+
require 'fresh_connection/railtie' if defined?(Rails::Railtie)
|