Skip to content

Commit 0ece604

Browse files
committed
Performance improvements and removal of branching of processing by legacy handlers.
1 parent fa792c9 commit 0ece604

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

lib/activerecord-refresh_connection/active_record/connection_adapters/refresh_connection_management.rb

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ module ActiveRecord
22
module ConnectionAdapters
33
class RefreshConnectionManagement
44
DEFAULT_OPTIONS = {max_requests: 1}
5-
AR_VERSION_6_1 = "6.1".freeze
6-
AR_VERSION_6_0 = "6.0".freeze
75

86
def initialize(app, options = {})
97
@app = app
108
@options = DEFAULT_OPTIONS.merge(options)
119
@mutex = Mutex.new
12-
@ar_version = ActiveRecord.gem_version.to_s
1310

11+
resolve_clear_connections
1412
reset_remain_count
1513
end
1614

@@ -33,32 +31,25 @@ def call(env)
3331
private
3432

3533
def clear_connections
36-
if @ar_version >= AR_VERSION_6_1
37-
if legacy_connection_handling?
38-
clear_legacy_compatible_connections
39-
else
40-
clear_multi_db_connections
41-
end
42-
elsif @ar_version >= AR_VERSION_6_0
43-
clear_legacy_compatible_connections
44-
else
45-
clear_legacy_connections
46-
end
34+
__send__(@clear_connections)
4735
end
4836

49-
def legacy_connection_handling?
50-
begin
51-
ActiveRecord::Base.legacy_connection_handling
52-
rescue NoMethodError
53-
false
54-
end
37+
def resolve_clear_connections
38+
ar_version = ActiveRecord.gem_version.to_s
39+
40+
@clear_connections =
41+
if ar_version >= "6.1"
42+
:clear_multi_db_connections
43+
elsif ar_version >= "6.0"
44+
:clear_legacy_compatible_connections
45+
else
46+
:clear_legacy_connections
47+
end
5548
end
5649

5750
def clear_multi_db_connections
5851
if should_clear_all_connections?
59-
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
60-
pool.disconnect!
61-
end
52+
ActiveRecord::Base.connection_handler.all_connection_pools.each(&:disconnect!)
6253
else
6354
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
6455
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
@@ -68,13 +59,9 @@ def clear_multi_db_connections
6859

6960
def clear_legacy_compatible_connections
7061
if should_clear_all_connections?
71-
ActiveRecord::Base.connection_handlers.each_value do |connection_handler|
72-
connection_handler.clear_all_connections!
73-
end
62+
ActiveRecord::Base.connection_handlers.each_value(&:clear_all_connections!)
7463
else
75-
ActiveRecord::Base.connection_handlers.each_value do |connection_handler|
76-
connection_handler.clear_active_connections!
77-
end
64+
ActiveRecord::Base.connection_handlers.each_value(&:clear_active_connections!)
7865
end
7966
end
8067

0 commit comments

Comments
 (0)