Skip to content

Commit 073114d

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

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

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

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ def initialize(app, options = {})
99
@app = app
1010
@options = DEFAULT_OPTIONS.merge(options)
1111
@mutex = Mutex.new
12-
@ar_version = ActiveRecord.gem_version.to_s
1312

13+
resolve_clear_connections
1414
reset_remain_count
1515
end
1616

@@ -33,32 +33,25 @@ def call(env)
3333
private
3434

3535
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
36+
__send__(@clear_connections)
4737
end
4838

49-
def legacy_connection_handling?
50-
begin
51-
ActiveRecord::Base.legacy_connection_handling
52-
rescue NoMethodError
53-
false
54-
end
39+
def resolve_clear_connections
40+
ar_version = ActiveRecord.gem_version.to_s
41+
42+
@clear_connections =
43+
if ar_version >= AR_VERSION_6_1
44+
:clear_multi_db_connections
45+
elsif ar_version >= AR_VERSION_6_0
46+
:clear_legacy_compatible_connections
47+
else
48+
:clear_legacy_connections
49+
end
5550
end
5651

5752
def clear_multi_db_connections
5853
if should_clear_all_connections?
59-
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
60-
pool.disconnect!
61-
end
54+
ActiveRecord::Base.connection_handler.all_connection_pools.each(&:disconnect!)
6255
else
6356
ActiveRecord::Base.connection_handler.all_connection_pools.each do |pool|
6457
pool.release_connection if pool.active_connection? && !pool.connection.transaction_open?
@@ -68,13 +61,9 @@ def clear_multi_db_connections
6861

6962
def clear_legacy_compatible_connections
7063
if should_clear_all_connections?
71-
ActiveRecord::Base.connection_handlers.each_value do |connection_handler|
72-
connection_handler.clear_all_connections!
73-
end
64+
ActiveRecord::Base.connection_handlers.each_value(&:clear_all_connections!)
7465
else
75-
ActiveRecord::Base.connection_handlers.each_value do |connection_handler|
76-
connection_handler.clear_active_connections!
77-
end
66+
ActiveRecord::Base.connection_handlers.each_value(&:clear_active_connections!)
7867
end
7968
end
8069

0 commit comments

Comments
 (0)