2019-07-08 21:06:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-09-21 14:11:18 -04:00
|
|
|
Gitlab::Application.configure do |config|
|
|
|
|
config.middleware.use(Gitlab::Database::LoadBalancing::RackMiddleware)
|
|
|
|
end
|
|
|
|
|
2021-10-11 11:13:01 -04:00
|
|
|
Gitlab::Database::LoadBalancing.base_models.each do |model|
|
|
|
|
# The load balancer needs to be configured immediately, and re-configured
|
|
|
|
# after forking. This ensures queries that run before forking use the load
|
|
|
|
# balancer, and queries running after a fork don't run into any errors when
|
|
|
|
# using dead database connections.
|
|
|
|
#
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/63485 for more
|
|
|
|
# information.
|
|
|
|
Gitlab::Database::LoadBalancing::Setup.new(model).setup
|
|
|
|
|
2022-08-31 20:09:48 -04:00
|
|
|
# We need re-rerun the setup when code reloads in development
|
2022-04-08 02:08:32 -04:00
|
|
|
Rails.application.reloader.to_prepare do
|
2022-08-31 20:09:48 -04:00
|
|
|
if Rails.env.development? || Rails.env.test?
|
2022-04-08 02:08:32 -04:00
|
|
|
Gitlab::Database::LoadBalancing::Setup.new(model).setup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-11 11:13:01 -04:00
|
|
|
# Database queries may be run before we fork, so we must set up the load
|
|
|
|
# balancer as early as possible. When we do fork, we need to make sure all the
|
|
|
|
# hosts are disconnected.
|
|
|
|
Gitlab::Cluster::LifecycleEvents.on_before_fork do
|
|
|
|
# When forking, we don't want to wait until the connections aren't in use
|
|
|
|
# any more, as this could delay the boot cycle.
|
2021-11-09 01:13:00 -05:00
|
|
|
model.load_balancer.disconnect!(timeout: 0)
|
2021-10-11 11:13:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Service discovery only needs to run in the worker processes, as the main one
|
|
|
|
# won't be running many (if any) database queries.
|
|
|
|
Gitlab::Cluster::LifecycleEvents.on_worker_start do
|
|
|
|
Gitlab::Database::LoadBalancing::Setup
|
|
|
|
.new(model, start_service_discovery: true)
|
|
|
|
.setup
|
|
|
|
end
|
2019-07-08 15:46:34 -04:00
|
|
|
end
|