From c83d4d6acdd3488306764d4e385957b5567d1e90 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Fri, 13 Mar 2015 13:07:10 -0700 Subject: [PATCH 1/2] [reconnect_on_error] support reconnect on error (unbind w/o close) - also fixed auto_reconnect option, was forced to `true`. --- lib/em-redis/redis_protocol.rb | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/lib/em-redis/redis_protocol.rb b/lib/em-redis/redis_protocol.rb index f4e12c5..7b55d80 100644 --- a/lib/em-redis/redis_protocol.rb +++ b/lib/em-redis/redis_protocol.rb @@ -337,12 +337,13 @@ def connect(*args) end def initialize(options = {}) - @host = options[:host] - @port = options[:port] - @db = (options[:db] || 0).to_i - @password = options[:password] - @auto_reconnect = options[:auto_reconnect] || true - @logger = options[:logger] + @host = options[:host] + @port = options[:port] + @db = (options[:db] || 0).to_i + @password = options[:password] + @auto_reconnect = options.fetch(:auto_reconnect, true) + @reconnect_on_error = options.fetch(:reconnect_on_error, false) + @logger = options[:logger] @error_callback = lambda do |err| raise err end @@ -486,7 +487,7 @@ def unbind @logger.debug { "Disconnected" } if @logger if @closing @reconnecting = false - elsif (@connected || @reconnecting) && @auto_reconnect + elsif ((@connected || @reconnecting) && @auto_reconnect) || @reconnect_on_error @reconnect_callbacks[:before].call if @connected @reconnecting = true EM.add_timer(1) do From a261ab542d339e7d698f847a5233d967a4bf7009 Mon Sep 17 00:00:00 2001 From: Sean Porter Date: Fri, 13 Mar 2015 13:15:51 -0700 Subject: [PATCH 2/2] [reconnect_on_error] always call before reconnect callback on the first reconnect attempt --- lib/em-redis/redis_protocol.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/em-redis/redis_protocol.rb b/lib/em-redis/redis_protocol.rb index 7b55d80..00a0e4f 100644 --- a/lib/em-redis/redis_protocol.rb +++ b/lib/em-redis/redis_protocol.rb @@ -488,7 +488,7 @@ def unbind if @closing @reconnecting = false elsif ((@connected || @reconnecting) && @auto_reconnect) || @reconnect_on_error - @reconnect_callbacks[:before].call if @connected + @reconnect_callbacks[:before].call unless @reconnecting @reconnecting = true EM.add_timer(1) do @logger.debug { "Reconnecting to #{@host}:#{@port}" } if @logger