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
|