Sha256: 6c9f9a7c77155f6926e2e2a214230f04ce2b88e2bc94e45b0beb17db18357e35

Contents?: true

Size: 1.3 KB

Versions: 5

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require 'redis/errors'
require 'redis/cluster/node_key'

class Redis
  class Cluster
    # Load and hashify slot info for Redis Cluster Client
    module SlotLoader
      module_function

      def load(nodes)
        errors = nodes.map do |node|
          begin
            return fetch_slot_info(node)
          rescue CannotConnectError, ConnectionError, CommandError => error
            error
          end
        end

        raise InitialSetupError, errors
      end

      def fetch_slot_info(node)
        hash_with_default_arr = Hash.new { |h, k| h[k] = [] }
        node.call(%i[cluster slots])
            .flat_map { |arr| parse_slot_info(arr, default_ip: node.host) }
            .each_with_object(hash_with_default_arr) { |arr, h| h[arr[0]] << arr[1] }
      end

      def parse_slot_info(arr, default_ip:)
        first_slot, last_slot = arr[0..1]
        slot_range = (first_slot..last_slot).freeze
        arr[2..-1].map { |addr| [stringify_node_key(addr, default_ip), slot_range] }
      end

      def stringify_node_key(arr, default_ip)
        ip, port = arr
        ip = default_ip if ip.empty? # When cluster is down
        NodeKey.build_from_host_port(ip, port)
      end

      private_class_method :fetch_slot_info, :parse_slot_info, :stringify_node_key
    end
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/redis-4.8.1/lib/redis/cluster/slot_loader.rb
redis-4.8.1 lib/redis/cluster/slot_loader.rb
redis-4.8.0 lib/redis/cluster/slot_loader.rb
redis-4.7.1 lib/redis/cluster/slot_loader.rb
redis-4.7.0 lib/redis/cluster/slot_loader.rb