Skip to content

Commit b64eadd

Browse files
committed
Don't hold on connection pools from the query cache
Followup: rails#52622 This reference causes connections to linger around longer on Rails CI. Instead we can simply share an atomic integer to bump the version. x
1 parent ffc580a commit b64eadd

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "concurrent/map"
4+
require "concurrent/atomic/atomic_fixnum"
45

56
module ActiveRecord
67
module ConnectionAdapters # :nodoc:
@@ -35,9 +36,9 @@ class Store # :nodoc:
3536
alias_method :enabled?, :enabled
3637
alias_method :dirties?, :dirties
3738

38-
def initialize(pool, max_size)
39-
@pool = pool
40-
@version = pool.query_cache_version
39+
def initialize(version, max_size)
40+
@version = version
41+
@current_version = version.value
4142
@map = {}
4243
@max_size = max_size
4344
@enabled = false
@@ -85,18 +86,17 @@ def clear
8586

8687
private
8788
def check_version
88-
version = @pool.query_cache_version
89-
if version != @version
89+
if @current_version != @version.value
9090
@map.clear
91-
@version = version
91+
@current_version = @version.value
9292
end
9393
end
9494
end
9595

9696
module ConnectionPoolConfiguration # :nodoc:
9797
def initialize(...)
9898
super
99-
@query_cache_version = 0
99+
@query_cache_version = Concurrent::AtomicFixnum.new
100100
@query_cache_max_size = \
101101
case query_cache = db_config&.query_cache
102102
when 0, false
@@ -160,16 +160,14 @@ def clear_query_cache
160160
# With transactional fixtures, and especially systems test
161161
# another thread may use the same connection, but with a different
162162
# query cache. So we must clear them all.
163-
synchronize do
164-
@query_cache_version += 1
165-
end
163+
@query_cache_version.increment
166164
end
167165
query_cache.clear
168166
end
169167

170168
def query_cache
171169
key = :"active_record_query_cache_#{object_id}"
172-
ActiveSupport::IsolatedExecutionState[key] ||= Store.new(self, @query_cache_max_size)
170+
ActiveSupport::IsolatedExecutionState[key] ||= Store.new(@query_cache_version, @query_cache_max_size)
173171
end
174172
end
175173

activerecord/test/cases/query_cache_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ def test_cache_is_expired_by_habtm_delete
10261026
end
10271027

10281028
def test_query_cache_lru_eviction
1029-
store = ActiveRecord::ConnectionAdapters::QueryCache::Store.new(ActiveRecord::Base.connection_pool, 2)
1029+
store = ActiveRecord::ConnectionAdapters::QueryCache::Store.new(Concurrent::AtomicFixnum.new, 2)
10301030
store.enabled = true
10311031

10321032
connection = Post.lease_connection

0 commit comments

Comments
 (0)