Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions lib/redis_failover/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def initialize(options = {})
@slaves = []
@node_addresses = {}
@lock = Monitor.new
@shutdown = false
@current_client_key = "current-client-#{self.object_id}"
yield self if block_given?

Expand Down Expand Up @@ -145,15 +146,17 @@ def manual_failover(options = {})
# and then create a new instance of the client. The underlying
# ZooKeeper client and redis clients will be closed.
def shutdown
@zk.close! if @zk
@zk.close! if @zk and @zk.connected?
@zk = nil
@shutdown = true
purge_clients
end

# Reconnect will first perform a shutdown of the underlying redis clients.
# Next, it attempts to reopen the ZooKeeper client and re-create the redis
# clients after it fetches the most up-to-date list from ZooKeeper.
def reconnect
@shutdown = false
purge_clients
@zk ? @zk.reopen : setup_zk
build_clients
Expand Down Expand Up @@ -319,12 +322,22 @@ def fetch_nodes
nodes
rescue Zookeeper::Exceptions::InheritedConnectionError => ex
logger.debug { "Caught #{ex.class} '#{ex.message}' - reopening ZK client" }
@zk.reopen
retry
unless @shutdown
@zk.reopen
retry
end
rescue Zookeeper::Exceptions::NotConnected => ex
logger.warn { "Caught #{ex.class} '#{ex.message}' - reopening ZK client" }
unless @shutdown
@zk.reopen
retry
end
rescue *ZK_ERRORS => ex
logger.warn { "Caught #{ex.class} '#{ex.message}' - retrying" }
sleep(RETRY_WAIT_TIME)
retry
unless @shutdown
sleep(RETRY_WAIT_TIME)
retry
end
end

# Builds new Redis clients for the specified nodes.
Expand Down