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-CY) #1683

Closed
wants to merge 1 commit into from

Conversation

revise-dev[bot]
Copy link

@revise-dev revise-dev bot commented Jan 24, 2025

The ActiveRecord::ConnectionTimeoutError occurs when the application runs out of available database connections. This is happening because the current connection pool size (11) is insufficient for the application's concurrent processing needs.

Looking at the application architecture:

  1. Puma web server is configured to use 3 threads per worker (from RAILS_MAX_THREADS)
  2. GoodJob background processor has 5 queue threads:
    • 1 thread for latency_low
    • 2 threads for latency_low,latency_medium
    • 1 thread for latency_low,latency_medium,latency_high
    • 1 thread for catch-all queue
  3. Additional GoodJob threads:
    • 1 thread for job listener
    • 1 thread for cron
    • 1 thread for executor

Total minimum threads needed:

  • Puma: 3 threads × 1 worker = 3 connections
  • GoodJob queues: 5 connections
  • GoodJob system: 3 connections
  • Buffer for peak loads: 9 connections
    Total: 20 connections needed

The current pool size of 11 doesn't account for peak loads and potential concurrent access patterns. By increasing the pool to 20, we ensure there are enough connections available for all concurrent operations while maintaining a buffer for unexpected spikes in database access.

Tests should be added:

# spec/config/database_config_spec.rb
require 'rails_helper'

RSpec.describe "Database Configuration" do
  it "has sufficient connection pool size for concurrent operations" do
    config = ActiveRecord::Base.configurations.configurations.find do |c| 
      c.env_name == Rails.env
    end
    
    min_required_pool = 20
    expect(config.pool).to be >= min_required_pool
  end

  it "handles concurrent database operations without timeout" do
    threads = []
    20.times do
      threads << Thread.new do
        Account.transaction do
          Account.first
          sleep 0.1 # Simulate work
        end
      end
    end
    expect { threads.each(&:join) }.not_to raise_error
  end
end

Tip

You can make revisions or ask questions of Revise.dev by using /revise in any comment or review!

  • /revise Add a comment above the method to explain why we're making this change.
  • /revise Why did you choose to make this change specifically?

Important

If something doesn't look right, click to retry this interaction.

@zachgoll zachgoll closed this Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant