summaryrefslogtreecommitdiff
path: root/config/initializers/load_balancing.rb
blob: 2b58ae0f6426f7ea4fa1378a1a8225e2b558a945 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

ActiveRecord::Base.singleton_class.attr_accessor :load_balancing_proxy

if Gitlab::Database::LoadBalancing.enable?
  Gitlab::Database.main.disable_prepared_statements

  Gitlab::Application.configure do |config|
    config.middleware.use(Gitlab::Database::LoadBalancing::RackMiddleware)
  end

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

  Gitlab::Database::LoadBalancing.configure_proxy

  # This needs to be executed after fork of clustered processes
  Gitlab::Cluster::LifecycleEvents.on_worker_start do
    # For Host-based LB, we need to re-connect as Rails discards connections on fork
    Gitlab::Database::LoadBalancing.configure_proxy

    # Service discovery must be started after configuring the proxy, as service
    # discovery depends on this.
    Gitlab::Database::LoadBalancing.start_service_discovery
  end
end