File: node_key.rb

package info (click to toggle)
ruby-redis-cluster-client 0.10.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208 kB
  • sloc: ruby: 2,214; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 894 bytes parent folder | download
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
# frozen_string_literal: true

class RedisClient
  class Cluster
    # Node key's format is `<ip>:<port>`.
    # It is different from node id.
    # Node id is internal identifying code in Redis Cluster.
    module NodeKey
      DELIMITER = ':'

      module_function

      def hashify(node_key)
        host, port = split(node_key)
        { host: host, port: port }
      end

      def split(node_key)
        pos = node_key&.rindex(DELIMITER, -1)
        return [node_key, nil] if pos.nil?

        [node_key[0, pos], node_key[(pos + 1)..]]
      end

      def build_from_uri(uri)
        return '' if uri.nil?

        "#{uri.host}#{DELIMITER}#{uri.port}"
      end

      def build_from_host_port(host, port)
        "#{host}#{DELIMITER}#{port}"
      end

      def build_from_client(client)
        "#{client.config.host}#{DELIMITER}#{client.config.port}"
      end
    end
  end
end