Module: Gitlab::Patch::NodeLoader

Extended by:
ActiveSupport::Concern
Defined in:
lib/gitlab/patch/node_loader.rb

Class Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



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
# File 'lib/gitlab/patch/node_loader.rb', line 24

def self.prepended(base)
  base.class_eval do
    # monkey-patches https://github.com/redis/redis-rb/blob/v4.8.0/lib/redis/cluster/node_loader.rb#L23
    def self.fetch_node_info(node)
      node.call(%i[cluster nodes]).split("\n").map(&:split).to_h do |arr|
        [
          extract_host_identifier(arr[1]),
          (arr[2].split(',') & %w[master slave]).first # rubocop:disable Naming/InclusiveLanguage
        ]
      end
    end

    # Since `CLUSTER SLOT` uses the preferred endpoint determined by
    # the `cluster-preferred-endpoint-type` config value, we will prefer hostname over IP address.
    # See https://redis.io/commands/cluster-nodes/ for details on the output format.
    #
    # @param [String] Address info matching fhe format: <ip:port@cport[,hostname[,auxiliary_field=value]*]>
    def self.extract_host_identifier(node_address)
      ip_chunk, hostname, _auxiliaries = node_address.split(',')
      return ip_chunk.split('@').first if hostname.blank?

      port = ip_chunk.split('@').first.split(':')[1]
      "#{hostname}:#{port}"
    end
  end
end