summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-01-15 20:30:06 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-01-15 20:57:31 +0100
commit929205f7c7e94daf83f6d762ff7614753cfaa712 (patch)
treecf0d1dbe27bd262636687211760acabff2a1093f
parent4a9285ca12580656a8269c16e90e2bcd9374ae74 (diff)
ircbot: fix reconnect() waiting
The refactored reconnect() method would only wait when the socket was connected at the time it got called. In case where the socket would have closed earlier (e.g. because of a network I/O error) it would reconnect directly, which would for example fail to prevent fast reconnections. Fix by fencing the wait code with a check for @last_rec (checked before the optional disconnect) rather than keeping it with the socket connect check, and always initializing @last_rec on socket connect. A side effect of this strategy is that reconnect() will only wait if the bot was previously connect, or if it got disconnected by anything but the disconnect() method. Callers of disconnect() should take care of waiting themselves if they plan to reconnect.
-rw-r--r--lib/rbot/ircbot.rb10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb
index 098a7292..0a18f04c 100644
--- a/lib/rbot/ircbot.rb
+++ b/lib/rbot/ircbot.rb
@@ -859,6 +859,7 @@ class Bot
begin
quit if $interrupted > 0
@socket.connect
+ @last_rec = Time.now
rescue => e
raise e.class, "failed to connect to IRC server at #{@socket.server_uri}: " + e
end
@@ -876,9 +877,17 @@ class Bot
# disconnect the bot from IRC, if connected, and then connect (again)
def reconnect(message=nil, too_fast=false)
+ # we will wait only if @last_rec was not nil, i.e. if we were connected or
+ # got disconnected by a network error
+ # if someone wants to manually call disconnect() _and_ reconnect(), they
+ # will have to take care of the waiting themselves
+ will_wait = !!@last_rec
+
if @socket.connected?
disconnect(message)
+ end
+ if will_wait
log "\n\nDisconnected\n\n"
quit if $interrupted > 0
@@ -887,6 +896,7 @@ class Bot
sleep @config['server.reconnect_wait']
sleep 10*@config['server.reconnect_wait'] if too_fast
end
+
connect
end