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-CZ) #1685

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 attempts to obtain a database connection but all connections in the pool are in use. The error indicates that the current pool size is insufficient to handle the concurrent database connections needed by both Puma (web server) and GoodJob (background job processor).

The original configuration in database.yml had a fixed pool size of 11, calculated based on assumptions about thread usage (3 for Puma, 8 for GoodJob). However, this static configuration doesn't account for variations in thread counts that might be set through environment variables.

The fix involves:

  1. Making the pool size dynamic based on actual thread counts from both Puma and GoodJob
  2. Using environment variables to allow runtime configuration of thread counts
  3. Calculating the pool size using the formula: (Puma Workers * Puma Threads per Worker) + GoodJob Threads

In config/database.yml, we're updating the pool configuration to dynamically calculate the required connections based on:

  • WEB_CONCURRENCY (Puma workers, defaults to 1)
  • RAILS_MAX_THREADS (Puma threads per worker, defaults to 3)
  • GOOD_JOB_MAX_THREADS (GoodJob threads, defaults to 5)

This ensures that the connection pool always has enough connections to handle all possible concurrent database operations, preventing connection timeout errors while not over-allocating database connections.

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