2019-07-08 21:06:24 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-08-05 14:10:13 -04:00
|
|
|
ActiveRecord::Base.singleton_class.attr_accessor :load_balancing_proxy
|
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
if Gitlab::Database::LoadBalancing.enable?
|
2021-07-29 11:09:48 -04:00
|
|
|
Gitlab::Database.main.disable_prepared_statements
|
2019-07-08 15:46:34 -04:00
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
Gitlab::Application.configure do |config|
|
|
|
|
config.middleware.use(Gitlab::Database::LoadBalancing::RackMiddleware)
|
|
|
|
end
|
2019-07-08 15:46:34 -04:00
|
|
|
|
2021-08-05 14:10:13 -04:00
|
|
|
# This hijacks the "connection" method to ensure both
|
|
|
|
# `ActiveRecord::Base.connection` and all models use the same load
|
|
|
|
# balancing proxy.
|
|
|
|
ActiveRecord::Base.singleton_class.prepend(Gitlab::Database::LoadBalancing::ActiveRecordProxy)
|
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
Gitlab::Database::LoadBalancing.configure_proxy
|
2019-07-08 15:46:34 -04:00
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
# This needs to be executed after fork of clustered processes
|
|
|
|
Gitlab::Cluster::LifecycleEvents.on_worker_start do
|
2021-06-08 08:10:09 -04:00
|
|
|
# For Host-based LB, we need to re-connect as Rails discards connections on fork
|
|
|
|
Gitlab::Database::LoadBalancing.configure_proxy
|
|
|
|
|
2021-05-27 02:10:47 -04:00
|
|
|
# Service discovery must be started after configuring the proxy, as service
|
|
|
|
# discovery depends on this.
|
|
|
|
Gitlab::Database::LoadBalancing.start_service_discovery
|
2019-07-08 15:46:34 -04:00
|
|
|
end
|
|
|
|
end
|