Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: ActiveRecord::ConnectionTimeoutError (MAYBE-RAILS-CX) #1684

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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 } %>
# Puma: 3 threads × 1 worker = 3 connections
# GoodJob: 5 queue threads + 3 system threads = 8 connections
# Buffer: 5 additional connections
# Total: 16 connections
pool: <%= ENV.fetch("DB_POOL_SIZE") { 16 } %>
host: <%= ENV.fetch("DB_HOST") { "127.0.0.1" } %>
port: <%= ENV.fetch("DB_PORT") { "5432" } %>
user: <%= ENV.fetch("POSTGRES_USER") { nil } %>
Expand Down
48 changes: 48 additions & 0 deletions spec/models/database_connection_pool_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require 'rails_helper'

RSpec.describe "Database Connection Pool" do
it "has the correct pool size configuration" do
expected_pool_size = ENV.fetch("DB_POOL_SIZE") { 16 }.to_i
actual_pool_size = ActiveRecord::Base.connection.pool.size

Check failure on line 7 in spec/models/database_connection_pool_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
expect(actual_pool_size).to eq(expected_pool_size)
end

it "handles concurrent database operations without connection timeout" do
threads = []
mutex = Mutex.new
error_occurred = false

10.times do
threads << Thread.new do
begin
User.transaction do
mutex.synchronize do
User.count
sleep 0.1 # Simulate some work
end
end
rescue ActiveRecord::ConnectionTimeoutError
mutex.synchronize { error_occurred = true }
end
end
end

threads.each(&:join)
expect(error_occurred).to be false
end

it "respects custom pool size from environment variable" do
original_pool_size = ENV["DB_POOL_SIZE"]
ENV["DB_POOL_SIZE"] = "20"

Check failure on line 38 in spec/models/database_connection_pool_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
# Force reconnection to apply new pool size
ActiveRecord::Base.establish_connection

Check failure on line 41 in spec/models/database_connection_pool_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
expect(ActiveRecord::Base.connection.pool.size).to eq(20)

Check failure on line 43 in spec/models/database_connection_pool_spec.rb

View workflow job for this annotation

GitHub Actions / ci / lint

Layout/TrailingWhitespace: Trailing whitespace detected.
# Cleanup
ENV["DB_POOL_SIZE"] = original_pool_size
ActiveRecord::Base.establish_connection
end
end
Loading