active_record_host_pool 1.2.3 → 1.2.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06f626b21d430564a336b02a25263b73e005466bb3b654750218aa34af3b3a49
4
- data.tar.gz: bcdae62eb1f0dbf16cc6c2cb2a9c2c17fe587e94cb4ce24b35ccc3cab794d43c
3
+ metadata.gz: cef6cffe1f0c00bea6d588f48093d20abb29f87e9cdab6d84eba31f9b918a44e
4
+ data.tar.gz: 9e443c3874c4b24b2955245dfa12183521386696937bc27d21ce9037b9e90ee5
5
5
  SHA512:
6
- metadata.gz: 74bcb39947f4b10f8bfef8389beb1fc2e14acabc05bea868a13d0b7e921968dc15deb43376cc68cd403b89fc9cac7cd90b436776edf16e3cb18f663aee678845
7
- data.tar.gz: 75253b513d4f8695293de0cb810d9c4bd63e85c0815b56ac6d4984218b8c05d2c11be76dbe5f2b28839198d0f43cc59da768e193f46dd35cf1f3e29c92ddcaf2
6
+ metadata.gz: 2cadfa3a2d2c582a71a3538692edd4fbde67e2266f98d6fdcc1133d40bd0db33969eca2ef1a0ef8ccb22b20a01fb263d76a7cfa88a877b3ac46b7d19d1852346
7
+ data.tar.gz: 1ddd95edd00b9bd496cb861db3ecbc7be607c0174ca59e68ca4eebf2546137dff24881d4a6d3d4cc940fabcc1d5a1c9e7477df14c744f9e00814d7bcf5b5d29a
@@ -1,42 +1,46 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if ActiveRecord.version >= Gem::Version.new('6.0')
4
- module ActiveRecordHostPool
5
- # ActiveRecord 6.0 introduced multiple database support. With that, an update
6
- # has been made in https://github.com/rails/rails/pull/35089 to ensure that
7
- # all query caches are cleared across connection handlers and pools. If you
8
- # write on one connection, the other connection will have the update that
9
- # occurred.
10
- #
11
- # This broke ARHP which implements its own pool, allowing you to access
12
- # multiple databases with the same connection (e.g. 1 connection for 100
13
- # shards on the same server).
14
- #
15
- # This patch maintains the reference to the database during the cache clearing
16
- # to ensure that the database doesn't get swapped out mid-way into an
17
- # operation.
18
- #
19
- # This is a private Rails API and may change in future releases as they're
20
- # actively working on sharding in Rails 6 and above.
21
- module ClearQueryCachePatch
22
- def clear_query_caches_for_current_thread
23
- host_pool_current_database_was = connection_pool._unproxied_connection._host_pool_current_database
24
- super
25
- ensure
26
- # restore in case clearing the cache changed the database
27
- connection_pool._unproxied_connection._host_pool_current_database = host_pool_current_database_was
3
+ # ActiveRecord 6.0 introduced multiple database support. With that, an update
4
+ # has been made in https://github.com/rails/rails/pull/35089 to ensure that
5
+ # all query caches are cleared across connection handlers and pools. If you
6
+ # write on one connection, the other connection will have the update that
7
+ # occurred.
8
+ #
9
+ # This broke ARHP which implements its own pool, allowing you to access
10
+ # multiple databases with the same connection (e.g. 1 connection for 100
11
+ # shards on the same server).
12
+ #
13
+ # This patch maintains the reference to the database during the cache clearing
14
+ # to ensure that the database doesn't get swapped out mid-way into an
15
+ # operation.
16
+ #
17
+ # This is a private Rails API and may change in future releases as they're
18
+ # actively working on sharding in Rails 6 and above.
19
+ module ActiveRecordHostPool
20
+ # For Rails 6.1 & 7.0.
21
+ module ClearOnHandlerPatch
22
+ def clear_on_handler(handler)
23
+ handler.all_connection_pools.each do |pool|
24
+ pool._unproxied_connection.clear_query_cache if pool.active_connection?
28
25
  end
26
+ end
27
+ end
29
28
 
30
- def clear_on_handler(handler)
31
- handler.all_connection_pools.each do |pool|
32
- db_was = pool._unproxied_connection._host_pool_current_database
29
+ # For Rails 6.0.
30
+ module ClearQueryCachePatch
31
+ def clear_query_caches_for_current_thread
32
+ ActiveRecord::Base.connection_handlers.each_value do |handler|
33
+ handler.connection_pool_list.each do |pool|
33
34
  pool._unproxied_connection.clear_query_cache if pool.active_connection?
34
- ensure
35
- pool._unproxied_connection._host_pool_current_database = db_was
36
35
  end
37
36
  end
38
37
  end
39
38
  end
39
+ end
40
40
 
41
- ActiveRecord::Base.singleton_class.prepend ActiveRecordHostPool::ClearQueryCachePatch
41
+ case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
42
+ when '6.1', '7.0'
43
+ ActiveRecord::Base.singleton_class.prepend(ActiveRecordHostPool::ClearOnHandlerPatch)
44
+ when '6.0'
45
+ ActiveRecord::Base.singleton_class.prepend(ActiveRecordHostPool::ClearQueryCachePatch)
42
46
  end
@@ -33,11 +33,14 @@ module ActiveRecordHostPool
33
33
  end
34
34
 
35
35
  def self.ruby2_keywords(*); end unless respond_to?(:ruby2_keywords, true)
36
- ruby2_keywords def execute_with_switching(*args, **kwargs)
36
+ # This one really does need ruby2_keywords; in Rails 6.0 the method does not take
37
+ # any keyword arguments, but in Rails 7.0 it does. So, we don't know whether or not
38
+ # what we're delegating to takes kwargs, so ruby2_keywords is needed.
39
+ ruby2_keywords def execute_with_switching(*args)
37
40
  if _host_pool_current_database && !_no_switch
38
41
  _switch_connection
39
42
  end
40
- execute_without_switching(*args, **kwargs)
43
+ execute_without_switching(*args)
41
44
  end
42
45
 
43
46
  def drop_database_with_no_switching(*args)
@@ -71,7 +74,7 @@ module ActiveRecordHostPool
71
74
  @connection.object_id != @_cached_connection_object_id
72
75
  )
73
76
  log("select_db #{_host_pool_current_database}", "SQL") do
74
- clear_cache! if respond_to?(:clear_cache!)
77
+ clear_cache!
75
78
  raw_connection.select_db(_host_pool_current_database)
76
79
  end
77
80
  @_cached_current_database = _host_pool_current_database
@@ -50,11 +50,13 @@ module ActiveRecordHostPool
50
50
  __getobj__.send(symbol, *args, &blk)
51
51
  end
52
52
  end
53
+ ruby2_keywords :send if respond_to?(:ruby2_keywords, true)
53
54
 
54
55
  private
55
56
 
56
57
  def select(*args)
57
58
  @cx.__send__(:select, *args)
58
59
  end
60
+ ruby2_keywords :select if respond_to?(:ruby2_keywords, true)
59
61
  end
60
62
  end
@@ -3,6 +3,7 @@
3
3
  require 'delegate'
4
4
  require 'active_record'
5
5
  require 'active_record_host_pool/connection_adapter_mixin'
6
+ require 'mutex_m'
6
7
 
7
8
  # this module sits in between ConnectionHandler and a bunch of different ConnectionPools (one per host).
8
9
  # when a connection is requested, it goes like:
@@ -15,6 +16,8 @@ require 'active_record_host_pool/connection_adapter_mixin'
15
16
  module ActiveRecordHostPool
16
17
  # Sits between ConnectionHandler and a bunch of different ConnectionPools (one per host).
17
18
  class PoolProxy < Delegator
19
+ include Mutex_m
20
+
18
21
  def initialize(pool_config)
19
22
  super(pool_config)
20
23
  @pool_config = pool_config
@@ -68,16 +71,18 @@ module ActiveRecordHostPool
68
71
  p = _connection_pool(false)
69
72
  return unless p
70
73
 
71
- p.disconnect!
72
- p.automatic_reconnect = true if p.respond_to?(:automatic_reconnect=)
73
- _clear_connection_proxy_cache
74
+ synchronize do
75
+ p.disconnect!
76
+ p.automatic_reconnect = true
77
+ _clear_connection_proxy_cache
78
+ end
74
79
  end
75
80
 
76
81
  def automatic_reconnect=(value)
77
82
  p = _connection_pool(false)
78
83
  return unless p
79
84
 
80
- p.automatic_reconnect = value if p.respond_to?(:automatic_reconnect=)
85
+ p.automatic_reconnect = value
81
86
  end
82
87
 
83
88
  def clear_reloadable_connections!
@@ -71,7 +71,7 @@ module ActiveRecordHostPool
71
71
  return unless p
72
72
 
73
73
  p.disconnect!
74
- p.automatic_reconnect = true if p.respond_to?(:automatic_reconnect=)
74
+ p.automatic_reconnect = true
75
75
  _clear_connection_proxy_cache
76
76
  end
77
77
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordHostPool
4
- VERSION = "1.2.3"
4
+ VERSION = "1.2.5"
5
5
  end
data/test/helper.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'bundler/setup'
4
4
  require 'minitest/autorun'
5
+ require 'pry-byebug'
5
6
 
6
7
  require 'active_record_host_pool'
7
8
  require 'logger'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_host_pool
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.3
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Quorning
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2023-01-19 00:00:00.000000000 Z
14
+ date: 2023-07-14 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: activerecord
@@ -47,90 +47,6 @@ dependencies:
47
47
  - - ">="
48
48
  - !ruby/object:Gem::Version
49
49
  version: '0'
50
- - !ruby/object:Gem::Dependency
51
- name: bump
52
- requirement: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - ">="
55
- - !ruby/object:Gem::Version
56
- version: '0'
57
- type: :development
58
- prerelease: false
59
- version_requirements: !ruby/object:Gem::Requirement
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: '0'
64
- - !ruby/object:Gem::Dependency
65
- name: minitest
66
- requirement: !ruby/object:Gem::Requirement
67
- requirements:
68
- - - ">="
69
- - !ruby/object:Gem::Version
70
- version: 5.10.0
71
- type: :development
72
- prerelease: false
73
- version_requirements: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: 5.10.0
78
- - !ruby/object:Gem::Dependency
79
- name: minitest-mock_expectations
80
- requirement: !ruby/object:Gem::Requirement
81
- requirements:
82
- - - "~>"
83
- - !ruby/object:Gem::Version
84
- version: 1.1.3
85
- type: :development
86
- prerelease: false
87
- version_requirements: !ruby/object:Gem::Requirement
88
- requirements:
89
- - - "~>"
90
- - !ruby/object:Gem::Version
91
- version: 1.1.3
92
- - !ruby/object:Gem::Dependency
93
- name: phenix
94
- requirement: !ruby/object:Gem::Requirement
95
- requirements:
96
- - - ">="
97
- - !ruby/object:Gem::Version
98
- version: 1.0.1
99
- type: :development
100
- prerelease: false
101
- version_requirements: !ruby/object:Gem::Requirement
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- version: 1.0.1
106
- - !ruby/object:Gem::Dependency
107
- name: rake
108
- requirement: !ruby/object:Gem::Requirement
109
- requirements:
110
- - - ">="
111
- - !ruby/object:Gem::Version
112
- version: 12.0.0
113
- type: :development
114
- prerelease: false
115
- version_requirements: !ruby/object:Gem::Requirement
116
- requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- version: 12.0.0
120
- - !ruby/object:Gem::Dependency
121
- name: rubocop
122
- requirement: !ruby/object:Gem::Requirement
123
- requirements:
124
- - - "~>"
125
- - !ruby/object:Gem::Version
126
- version: 0.80.0
127
- type: :development
128
- prerelease: false
129
- version_requirements: !ruby/object:Gem::Requirement
130
- requirements:
131
- - - "~>"
132
- - !ruby/object:Gem::Version
133
- version: 0.80.0
134
50
  description: ''
135
51
  email:
136
52
  - bquorning@zendesk.com
@@ -168,7 +84,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
168
84
  requirements:
169
85
  - - ">="
170
86
  - !ruby/object:Gem::Version
171
- version: 2.6.0
87
+ version: 2.7.0
172
88
  required_rubygems_version: !ruby/object:Gem::Requirement
173
89
  requirements:
174
90
  - - ">="