lib/redis_failover/client.rb in redis_failover-0.9.0 vs lib/redis_failover/client.rb in redis_failover-0.9.1
- old
+ new
@@ -175,10 +175,12 @@
purge_clients
@zk.stat(@znode, :watch => true)
else
logger.error("Unknown ZK node event: #{event.inspect}")
end
+ ensure
+ @zk.stat(@znode, :watch => true)
end
# Determines if a method is a known redis operation.
#
# @param [Symbol] method the method to check
@@ -235,11 +237,11 @@
# @note If there are no slaves, the master is returned.
# @return [Redis] the Redis client for the slave or master
# @raise [NoMasterError] if no master fallback is available
def slave
# pick a slave, if none available fallback to master
- if slave = @lock.synchronize { @slaves.sample }
+ if slave = @lock.synchronize { @slaves.shuffle.first }
verify_role!(slave, :slave)
return slave
end
master
end
@@ -251,11 +253,11 @@
begin
nodes = fetch_nodes
return unless nodes_changed?(nodes)
purge_clients
- logger.info("Building new clients for nodes #{nodes}")
+ logger.info("Building new clients for nodes #{nodes.inspect}")
new_master = new_clients_for(nodes[:master]).first if nodes[:master]
new_slaves = new_clients_for(*nodes[:slaves])
@master = new_master
@slaves = new_slaves
rescue
@@ -285,10 +287,10 @@
#
# @return [Hash] the known master/slave redis servers
def fetch_nodes
data = @zk.get(@znode, :watch => true).first
nodes = symbolize_keys(decode(data))
- logger.debug("Fetched nodes: #{nodes}")
+ logger.debug("Fetched nodes: #{nodes.inspect}")
nodes
rescue Zookeeper::Exceptions::InheritedConnectionError => ex
logger.debug { "Caught #{ex.class} '#{ex.message}' - reopening ZK client" }
@zk.reopen