activerecord_connection_reaper 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a83e3a19d85b226ca9a5295bd3cf083a8c91e1b1f1a80615fb9971af6aa2d04
4
+ data.tar.gz: 23e3a1199172ad48954272eee17ab567e9b8aa9a63d448ee931443427d1ff8b2
5
+ SHA512:
6
+ metadata.gz: a60de4a59c2d3f5122e83e6ff94fce48f7a5956d8dfe26871186dae806c27eef848057c48cf750781f903c71e3de2b5ce95f5db5ec44e2e1196178dcfd7503a1
7
+ data.tar.gz: 4a644ffd0750deaaf8625c63dc88af56395cc12e1ffc5caac56278e58d8cfa64f620404103c94f5d367426f82d97b124607e0894940dacc890319bd8541b1702
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2026 Kyle Fazzari
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # ActiverecordConnectionReaper
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activerecord_connection_reaper`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ ```bash
14
+ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
15
+ ```
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ ```bash
20
+ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord_connection_reaper.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
+
6
+ Minitest::TestTask.create # rubocop:disable Rails/SaveBang
7
+
8
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task static: :rubocop
13
+ task default: %i[static test]
@@ -0,0 +1,34 @@
1
+ module ActiveRecordConnectionReaper
2
+ module Extensions
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ module AbstractAdapterTrackConnectedSince
6
+ def initialize(*args, **kwargs, &block)
7
+ super
8
+ @connected_since = Process.clock_gettime(Process::CLOCK_MONOTONIC)
9
+ @pool_jitter = rand * max_jitter
10
+ end
11
+
12
+ def reconnect!(*args, **kwargs, &block)
13
+ super
14
+ @connected_since = Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
+ end
16
+
17
+ def connection_age
18
+ return unless raw_connection && @connected_since
19
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - @connected_since
20
+ end
21
+
22
+ def pool_jitter(duration)
23
+ duration * (1.0 - @pool_jitter)
24
+ end
25
+
26
+ MAX_JITTER = 0.0..1.0
27
+ def max_jitter
28
+ (@config[:pool_jitter] || 0.2).to_f.clamp(MAX_JITTER)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,35 @@
1
+ module ActiveRecordConnectionReaper
2
+ module Extensions
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ module PoolMaxAge
6
+ def initialize(*)
7
+ super
8
+ # db_config was added in Rails v6.1
9
+ configuration_hash = if respond_to?(:db_config)
10
+ db_config.configuration_hash
11
+ else
12
+ spec.config
13
+ end
14
+ @max_age = configuration_hash[:max_age]&.to_f
15
+ end
16
+
17
+ def retire_old_connections(max_age = @max_age)
18
+ max_age ||= Float::INFINITY
19
+
20
+ old_connections = synchronize do
21
+ @connections.select { |c| !c.in_use? && c.connection_age&.>=(c.pool_jitter(max_age)) }.each do |conn|
22
+ conn.lease
23
+
24
+ @available.delete conn
25
+ @connections.delete conn
26
+ end
27
+ end
28
+
29
+ old_connections.each(&:disconnect!)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ module ActiveRecordConnectionReaper
2
+ module Extensions
3
+ module ActiveRecord
4
+ module ConnectionAdapters
5
+ 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)
12
+ end
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def run_once(pool)
19
+ pool.reap
20
+ pool.flush
21
+ pool.retire_old_connections
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails'
4
+
5
+ module ActiveRecordConnectionReaper
6
+ class Railtie < Rails::Railtie
7
+ initializer 'activerecord_connection_reaper.apply_patches' do
8
+ require 'activerecord_connection_reaper/extensions/active_record/connection_adapters/abstract_adapter_track_connected_since' # rubocop:disable Layout/LineLength
9
+ require 'activerecord_connection_reaper/extensions/active_record/connection_adapters/reaper_check_max_age'
10
+ require 'activerecord_connection_reaper/extensions/active_record/connection_adapters/pool_max_age'
11
+
12
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecordConnectionReaper::Extensions::ActiveRecord::ConnectionAdapters::AbstractAdapterTrackConnectedSince)
13
+ ActiveRecord::ConnectionAdapters::ConnectionPool::Reaper.prepend(ActiveRecordConnectionReaper::Extensions::ActiveRecord::ConnectionAdapters::ReaperCheckMaxAge)
14
+ ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(ActiveRecordConnectionReaper::Extensions::ActiveRecord::ConnectionAdapters::PoolMaxAge)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecordConnectionReaper
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'activerecord_connection_reaper/version'
4
+ require_relative 'activerecord_connection_reaper/railtie'
5
+ require 'active_record'
6
+
7
+ module ActiveRecordConnectionReaper
8
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_connection_reaper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Miriam Technologies
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activerecord
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '5.1'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '8.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '5.1'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '8.1'
32
+ - !ruby/object:Gem::Dependency
33
+ name: railties
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '5.1'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '8.1'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '5.1'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '8.1'
52
+ description: This is mostly a backport of Rails v8.1's max_age database parameter.
53
+ email:
54
+ - developers@miriamtech.com
55
+ executables: []
56
+ extensions: []
57
+ extra_rdoc_files: []
58
+ files:
59
+ - LICENSE.txt
60
+ - README.md
61
+ - Rakefile
62
+ - lib/activerecord_connection_reaper.rb
63
+ - lib/activerecord_connection_reaper/extensions/active_record/connection_adapters/abstract_adapter_track_connected_since.rb
64
+ - lib/activerecord_connection_reaper/extensions/active_record/connection_adapters/pool_max_age.rb
65
+ - lib/activerecord_connection_reaper/extensions/active_record/connection_adapters/reaper_check_max_age.rb
66
+ - lib/activerecord_connection_reaper/railtie.rb
67
+ - lib/activerecord_connection_reaper/version.rb
68
+ homepage: https://github.com/miriamtech/activerecord_connection_reaper
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ allowed_push_host: https://rubygems.org
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: 3.4.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubygems_version: 3.6.9
88
+ specification_version: 4
89
+ summary: Retire old ActiveRecord database connections
90
+ test_files: []