class Redis::Cluster::Node
Constants
- ROLE_SLAVE
- ReloadNeeded
Public Class Methods
new(options, node_flags = {}, with_replica = false)
click to toggle source
# File lib/redis/cluster/node.rb, line 14 def initialize(options, node_flags = {}, with_replica = false) @with_replica = with_replica @node_flags = node_flags @clients = build_clients(options) end
Public Instance Methods
call_all(command, &block)
click to toggle source
# File lib/redis/cluster/node.rb, line 34 def call_all(command, &block) try_map { |_, client| client.call(command, &block) }.values end
call_master(command, &block)
click to toggle source
# File lib/redis/cluster/node.rb, line 38 def call_master(command, &block) try_map do |node_key, client| next if slave?(node_key) client.call(command, &block) end.values end
call_slave(command, &block)
click to toggle source
# File lib/redis/cluster/node.rb, line 45 def call_slave(command, &block) return call_master(command, &block) if replica_disabled? try_map do |node_key, client| next if master?(node_key) client.call(command, &block) end.values end
each(&block)
click to toggle source
# File lib/redis/cluster/node.rb, line 20 def each(&block) @clients.values.each(&block) end
find_by(node_key)
click to toggle source
# File lib/redis/cluster/node.rb, line 28 def find_by(node_key) @clients.fetch(node_key) rescue KeyError raise ReloadNeeded end
process_all(commands, &block)
click to toggle source
# File lib/redis/cluster/node.rb, line 54 def process_all(commands, &block) try_map { |_, client| client.process(commands, &block) }.values end
sample()
click to toggle source
# File lib/redis/cluster/node.rb, line 24 def sample @clients.values.sample end
Private Instance Methods
build_clients(options)
click to toggle source
# File lib/redis/cluster/node.rb, line 72 def build_clients(options) clients = options.map do |node_key, option| next if replica_disabled? && slave?(node_key) client = Client.new(option) client.call(%i[readonly]) if slave?(node_key) [node_key, client] end clients.compact.to_h end
master?(node_key)
click to toggle source
# File lib/redis/cluster/node.rb, line 64 def master?(node_key) !slave?(node_key) end
replica_disabled?()
click to toggle source
# File lib/redis/cluster/node.rb, line 60 def replica_disabled? !@with_replica end
slave?(node_key)
click to toggle source
# File lib/redis/cluster/node.rb, line 68 def slave?(node_key) @node_flags[node_key] == ROLE_SLAVE end
try_map() { |node_key, client| ... }
click to toggle source
# File lib/redis/cluster/node.rb, line 84 def try_map errors = {} results = {} @clients.each do |node_key, client| begin reply = yield(node_key, client) results[node_key] = reply unless reply.nil? rescue CommandError => err errors[node_key] = err next end end return results if errors.empty? raise CommandErrorCollection, errors end