summaryrefslogtreecommitdiff
path: root/config/initializers/elastic_client_setup.rb
blob: 2ecb7956007bf7da88c3df5ab91e5a751cb2f1f0 (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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# frozen_string_literal: true

# Be sure to restart your server when you modify this file.

require 'gitlab/current_settings'

Gitlab.ee do
  Elasticsearch::Model::Response::Records.prepend GemExtensions::Elasticsearch::Model::Response::Records
  Elasticsearch::Model::Adapter::Multiple::Records.prepend GemExtensions::Elasticsearch::Model::Adapter::Multiple::Records
  Elasticsearch::Model::Indexing::InstanceMethods.prepend GemExtensions::Elasticsearch::Model::Indexing::InstanceMethods

  module Elasticsearch
    module Model
      module Client
        # This mutex is only used to synchronize *creation* of a new client, so
        # all including classes can share the same client instance
        CLIENT_MUTEX = Mutex.new

        cattr_accessor :cached_client
        cattr_accessor :cached_config

        module ClassMethods
          # Override the default ::Elasticsearch::Model::Client implementation to
          # return a client configured from application settings. All including
          # classes will use the same instance, which is refreshed automatically
          # if the settings change.
          #
          # _client is present to match the arity of the overridden method, where
          # it is also not used.
          #
          # @return [Elasticsearch::Transport::Client]
          def client(_client = nil)
            store = ::Elasticsearch::Model::Client

            store::CLIENT_MUTEX.synchronize do
              config = Gitlab::CurrentSettings.elasticsearch_config

              if store.cached_client.nil? || config != store.cached_config
                store.cached_client = ::Gitlab::Elastic::Client.build(config)
                store.cached_config = config
              end
            end

            store.cached_client
          end
        end
      end
    end
  end
end