Skip to content

Commit

Permalink
Fix: ActiveRecord::ConnectionTimeoutError (MAYBE-RAILS-CZ)
Browse files Browse the repository at this point in the history
  • Loading branch information
revise-dev[bot] authored Jan 24, 2025
1 parent 3140835 commit ebf016d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
6 changes: 4 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
default: &default
adapter: postgresql
encoding: unicode
# 3 connections for Puma, 8 for GoodJob (in async mode, the default for self-hosters) = 11 connections
pool: <%= ENV.fetch("DB_POOL_SIZE") { 11 } %>
pool: <%= ENV.fetch("DB_POOL_SIZE") {
(ENV.fetch("WEB_CONCURRENCY") {1}.to_i * ENV.fetch("RAILS_MAX_THREADS") {3}.to_i) +
ENV.fetch("GOOD_JOB_MAX_THREADS") {5}.to_i
} %>
host: <%= ENV.fetch("DB_HOST") { "127.0.0.1" } %>
port: <%= ENV.fetch("DB_PORT") { "5432" } %>
user: <%= ENV.fetch("POSTGRES_USER") { nil } %>
Expand Down
38 changes: 38 additions & 0 deletions spec/config/database_yml_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'
require 'yaml'

RSpec.describe 'database.yml configuration' do
let(:config) { YAML.load(ERB.new(File.read('config/database.yml')).result) }

describe 'connection pool size' do
before do
ENV['DB_POOL_SIZE'] = nil
ENV['WEB_CONCURRENCY'] = nil
ENV['RAILS_MAX_THREADS'] = nil
ENV['GOOD_JOB_MAX_THREADS'] = nil
end

after do
ENV['DB_POOL_SIZE'] = nil
ENV['WEB_CONCURRENCY'] = nil
ENV['RAILS_MAX_THREADS'] = nil
ENV['GOOD_JOB_MAX_THREADS'] = nil
end

it 'calculates default pool size correctly' do
expect(config['default']['pool']).to eq(8) # (1 * 3) + 5
end

it 'uses custom pool size when DB_POOL_SIZE is set' do
ENV['DB_POOL_SIZE'] = '20'
expect(YAML.load(ERB.new(File.read('config/database.yml')).result)['default']['pool']).to eq(20)
end

it 'calculates pool size based on environment variables' do
ENV['WEB_CONCURRENCY'] = '2'
ENV['RAILS_MAX_THREADS'] = '4'
ENV['GOOD_JOB_MAX_THREADS'] = '6'
expect(YAML.load(ERB.new(File.read('config/database.yml')).result)['default']['pool']).to eq(14) # (2 * 4) + 6
end
end
end

0 comments on commit ebf016d

Please sign in to comment.