Files
openproject/config/initializers/database_pool_size.rb
T
2026-04-26 18:53:24 +02:00

62 lines
2.9 KiB
Ruby

# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
if Rails.env.production?
config = Rails.application.config.database_configuration[Rails.env]
pool_size = config && [OpenProject::Configuration.web_max_threads + 1, config["pool"].to_i].max
# make sure we have enough connections in the pool for each thread and then some
if pool_size && pool_size > ActiveRecord::Base.connection_pool.size
Rails.logger.info { "Increasing database pool size to #{pool_size} to match max threads" }
ActiveRecord::Base.establish_connection config.merge(pool: pool_size)
end
end
# Per GoodJob's own recommendation (README "Database connections"), the pool must
# cover web threads, job threads, and GoodJob utility connections: 1 for the
# Notifier (LISTEN/NOTIFY) plus GoodJob::SharedExecutor::MAX_THREADS for cron/executor.
# The pool size is a ceiling — Rails creates connections lazily — so setting it
# to a large constant like 100 is the simplest safe choice (https://island94.org/2024/09/secret-to-rails-database-connection-pool-size).
if Rails.env.local?
utility_connections = 1 + GoodJob::SharedExecutor::MAX_THREADS
required_pool_size = OpenProject::Configuration.web_max_threads +
OpenProject::Configuration.good_job_max_threads +
utility_connections
if ActiveRecord::Base.connection_pool.size < required_pool_size
raise "DB pool (#{ActiveRecord::Base.connection_pool.size}) too small — need at least #{required_pool_size} " \
"(web_max_threads #{OpenProject::Configuration.web_max_threads} + " \
"good_job_max_threads #{OpenProject::Configuration.good_job_max_threads} + " \
"#{utility_connections} utility). " \
"Set pool: 100 in database.yml or ?pool=100 in DATABASE_URL."
end
end