only use puma, introduce rack-timeout (#9718)
Co-authored-by: Oliver Günther <mail@oliverguenther.de>pull/9728/head
parent
58e5a01309
commit
c9256e644c
@ -0,0 +1,9 @@ |
||||
config = Rails.application.config.database_configuration[Rails.env] |
||||
pool_size = [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 Rails.env.production? && pool_size > ActiveRecord::Base.connection_pool.size |
||||
Rails.logger.debug { "Increasing database pool size to #{pool_size} to match max threads" } |
||||
|
||||
ActiveRecord::Base.establish_connection config.merge(pool: pool_size) |
||||
end |
@ -0,0 +1,43 @@ |
||||
# Use rack-timeout if we run in clustered mode with at least 2 workers |
||||
# so that workers, should a timeout occur, can be restarted without interruption. |
||||
if OpenProject::Configuration.web_workers >= 2 |
||||
timeout = Integer(ENV['RACK_TIMEOUT_SERVICE_TIMEOUT'].presence || OpenProject::Configuration.web_timeout) |
||||
wait_timeout = Integer(ENV['RACK_TIMEOUT_WAIT_TIMEOUT'].presence || OpenProject::Configuration.web_wait_timeout) |
||||
|
||||
Rails.logger.debug { "Enabling Rack::Timeout (service=#{timeout}s wait=#{wait_timeout}s)" } |
||||
|
||||
Rails.application.config.middleware.insert_before( |
||||
::Rack::Runtime, |
||||
::Rack::Timeout, |
||||
service_timeout: timeout, # time after which a request being served times out |
||||
wait_timeout: wait_timeout, # time after which a request waiting to be served times out |
||||
term_on_timeout: 1 # shut down worker (gracefully) right away on timeout to be restarted |
||||
) |
||||
|
||||
# remove default logger (logging uninteresting extra info with each not timed out request) |
||||
Rack::Timeout.unregister_state_change_observer(:logger) |
||||
|
||||
Rack::Timeout.register_state_change_observer(:wait_timeout_logger) do |env| |
||||
details = env[Rack::Timeout::ENV_INFO_KEY] |
||||
|
||||
if details.state == :timed_out && details.wait.present? |
||||
::OpenProject.logger.error "Request timed out waiting to be served!" |
||||
end |
||||
end |
||||
|
||||
# The timeout itself is already reported so no need to |
||||
# report the generic internal server error too as it doesn't |
||||
# add any more information. Even worse, it's not immediately |
||||
# clear that the two reports are related. |
||||
module SuppressInternalErrorReportOnTimeout |
||||
def op_handle_error(message_or_exception, context = {}) |
||||
return if request && request.env[Rack::Timeout::ENV_INFO_KEY].try(:state) == :timed_out |
||||
|
||||
super |
||||
end |
||||
end |
||||
|
||||
OpenProjectErrorHelper.prepend SuppressInternalErrorReportOnTimeout |
||||
else |
||||
Rails.logger.debug { "Not enabling Rack::Timeout since we are not running in cluster mode with at least 2 workers" } |
||||
end |
@ -1,54 +0,0 @@ |
||||
#-- copyright |
||||
# OpenProject is an open source project management software. |
||||
# Copyright (C) 2012-2021 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. |
||||
#++ |
||||
|
||||
worker_processes Integer(ENV['OPENPROJECT_WEB_WORKERS'] || 4) |
||||
timeout Integer(ENV['OPENPROJECT_WEB_TIMEOUT'] || 300) |
||||
preload_app true |
||||
|
||||
# Preloading the unicorn server to have all workers spawn the application |
||||
# automatically. |
||||
# |
||||
# Borrows heavily from https://www.digitalocean.com/community/tutorials/how-to-optimize-unicorn-workers-in-a-ruby-on-rails-app |
||||
# |
||||
# This method requires ActiveRecord to close and re-establish its connection in the slaves, |
||||
# because the connection is not properly shared with them. |
||||
# |
||||
# If you use any other service, you'll need to add them to these _fork blocks to close |
||||
# and reopen sockets when forking. |
||||
# (except Dalli/Memcache store, which detects automatically) |
||||
before_fork do |_server, _worker| |
||||
if defined?(ActiveRecord::Base) |
||||
ActiveRecord::Base.connection.disconnect! |
||||
end |
||||
end |
||||
|
||||
after_fork do |_server, _worker| |
||||
if defined?(ActiveRecord::Base) |
||||
ActiveRecord::Base.establish_connection |
||||
end |
||||
end |
Loading…
Reference in new issue