activerecord_connection_reaper 0.2.0 → 0.3.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 32f5626f8dd3cf90b2b020485814417cacd487528ec1c0d994e8ab1e55628f8a
4
- data.tar.gz: 7020099fc782b21c78cd8522afee666a23954befc40d9f17a9b6254d31832b0b
3
+ metadata.gz: de859231591ab97c462288d661a36ec93f747a8d9352c82cc841bd21c7f90c0b
4
+ data.tar.gz: '09c5737830cab9916e818c4a6f7d535bf640c90bc301835c2d692d2299d9baeb'
5
5
  SHA512:
6
- metadata.gz: '08bd4767c9a37ef218fd9c3448ed54481dea420ae51af0b5b11ba5cafa606b5ca960d89ffa31f762b597333baa047386c5c213cf672b43f877389efe4cf0253a'
7
- data.tar.gz: f4762978b2679a85db1b5791b83067bbade6900d8a253a2a24438bf3bce6f8599e682cca7e1ce72fdab9482b2a3fd665e06486542c5145843e2b7d57278c30ce
6
+ metadata.gz: 6c0e49d19b53bc68a2b2d1245c0ab80dddf78f88c29d59a791f1fa36ea0d99da8d6673019322783929f2ec18a1d7d72bc04e8364bbbdcf520408f77c262ee0a5
7
+ data.tar.gz: 4d7d68d0aa1d24b325667441f02f4608e2d2fbd3b5e6fd93a0e1f6a383bc820d86d8cf3e727b483650a49fe912c624811eddf4afd4c5e1d8935a9882e20b0b16
@@ -28,6 +28,10 @@ module ActiveRecordConnectionReaper
28
28
 
29
29
  old_connections.each(&:disconnect!)
30
30
  end
31
+
32
+ def __patched_discarded?
33
+ @connections.nil?
34
+ end
31
35
  end
32
36
  end
33
37
  end
@@ -1,24 +1,65 @@
1
+ require 'weakref'
2
+
1
3
  module ActiveRecordConnectionReaper
2
4
  module Extensions
3
5
  module ActiveRecord
4
6
  module ConnectionAdapters
5
7
  module ReaperCheckMaxAge
6
- def run
7
- return unless frequency&.positive?
8
- Thread.new(frequency, pool) do |t, p|
9
- loop do
10
- sleep t
11
- run_once(p)
8
+ def self.prepended(base)
9
+ base.instance_variable_set(:@mutex, Mutex.new) if base.instance_variable_get(:@mutex).nil?
10
+ base.instance_variable_set(:@pools, {}) if base.instance_variable_get(:@pools).nil?
11
+ base.instance_variable_set(:@threads, {}) if base.instance_variable_get(:@threads).nil?
12
+
13
+ base.singleton_class.send(:prepend, ClassMethods)
14
+ end
15
+
16
+ module ClassMethods
17
+ def __patched_register_pool(pool, frequency) # :nodoc:
18
+ @mutex.synchronize do
19
+ @threads[frequency] = __patched_spawn_thread(frequency) unless @threads[frequency]&.alive?
20
+ @pools[frequency] ||= []
21
+ @pools[frequency] << WeakRef.new(pool)
12
22
  end
13
23
  end
14
- end
15
24
 
16
- private
25
+ private
26
+
27
+ def __patched_spawn_thread(frequency) # rubocop:disable Metrics/MethodLength
28
+ Thread.new(frequency) do |t|
29
+ # Advise multi-threaded app servers to ignore this thread for
30
+ # the purposes of fork safety warnings
31
+ Thread.current.thread_variable_set(:fork_safe, true)
32
+ Thread.current.name = 'AR Pool Reaper'
33
+ running = true
34
+ while running
35
+ sleep t
36
+ @mutex.synchronize do
37
+ @pools[frequency].select! do |pool|
38
+ pool.weakref_alive? && !pool.__patched_discarded?
39
+ end
17
40
 
18
- def run_once(pool)
19
- pool.reap
20
- pool.flush
21
- pool.retire_old_connections
41
+ @pools[frequency].each do |p|
42
+ p.reap
43
+ p.flush
44
+ p.retire_old_connections
45
+ rescue WeakRef::RefError
46
+ # Pool has been garbage collected. Nothing we can do here, move on.
47
+ end
48
+
49
+ if @pools[frequency].empty?
50
+ @pools.delete(frequency)
51
+ @threads.delete(frequency)
52
+ running = false
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def run
61
+ return unless frequency&.positive?
62
+ self.class.__patched_register_pool(pool, frequency)
22
63
  end
23
64
  end
24
65
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecordConnectionReaper
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord_connection_reaper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miriam Technologies