lib/redis-sentinel/client.rb in redis-sentinel-1.2.0 vs lib/redis-sentinel/client.rb in redis-sentinel-1.3.0
- old
+ new
@@ -3,10 +3,11 @@
class Redis::Client
DEFAULT_FAILOVER_RECONNECT_WAIT_SECONDS = 0.1
class_eval do
def initialize_with_sentinel(options={})
+ options = options.dup # Don't touch my options
@master_name = fetch_option(options, :master_name)
@master_password = fetch_option(options, :master_password)
@sentinels = fetch_option(options, :sentinels)
@failover_reconnect_timeout = fetch_option(options, :failover_reconnect_timeout)
@failover_reconnect_wait = fetch_option(options, :failover_reconnect_wait) ||
@@ -75,9 +76,34 @@
raise Redis::CannotConnectError.new("The master: #{@master_name} is currently not available.")
else
@options.merge!(:host => host, :port => port.to_i, :password => @master_password)
end
end
+
+ def reconnect_with_sentinels
+ redis_sentinels.each do |config, sentinel|
+ sentinel.client.reconnect
+ end
+ reconnect_without_sentinels
+ end
+
+ alias reconnect_without_sentinels reconnect
+ alias reconnect reconnect_with_sentinels
+
+ def call_with_readonly_protection(*args, &block)
+ tries = 0
+ call_without_readonly_protection(*args, &block)
+ rescue Redis::CommandError => e
+ if e.message == "READONLY You can't write against a read only slave."
+ reconnect
+ retry if (tries += 1) < 4
+ else
+ raise
+ end
+ end
+
+ alias call_without_readonly_protection call
+ alias call call_with_readonly_protection
private
def fetch_option(options, key)
options.delete(key) || options.delete(key.to_s)