Skip to content

Commit

Permalink
Rename variable for clarity and reset on reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
mloughran committed Jun 5, 2013
1 parent d29b6aa commit 4b7ec02
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions lib/em-hiredis/base_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def initialize(host = 'localhost', port = 6379, password = nil, db = nil)
@defs = []
@command_queue = []

@closing_connection = false
@reconnect_failed_count = 0
@reconnect_timer = nil
@failed = false
Expand Down Expand Up @@ -64,10 +63,12 @@ def configure(uri_string)
def reconnect!(new_uri = nil)
@connection.close_connection
configure(new_uri) if new_uri
@auto_reconnect = true
EM.next_tick { reconnect_connection }
end

def connect
@auto_reconnect = true
@connection = EM.connect(@host, @port, Connection, @host, @port)

@connection.on(:closed) do
Expand All @@ -76,14 +77,14 @@ def connect
@defs = []
@deferred_status = nil
@connected = false
unless @closing_connection
if @auto_reconnect
# Next tick avoids reconnecting after for example EM.stop
EM.next_tick { reconnect }
end
emit(:disconnected)
EM::Hiredis.logger.info("#{@connection} Disconnected")
else
unless @closing_connection
if @auto_reconnect
@reconnect_failed_count += 1
@reconnect_timer = EM.add_timer(EM::Hiredis.reconnect_timeout) {
@reconnect_timer = nil
Expand Down Expand Up @@ -168,13 +169,14 @@ def auth(password, &blk)

def close_connection
EM.cancel_timer(@reconnect_timer) if @reconnect_timer
@closing_connection = true
@auto_reconnect = false
@connection.close_connection_after_writing
end

# Note: This method doesn't disconnect if already connected. You probably
# want to use `reconnect!`
def reconnect_connection
@auto_reconnect = true
EM.cancel_timer(@reconnect_timer) if @reconnect_timer
reconnect
end
Expand Down

0 comments on commit 4b7ec02

Please sign in to comment.