From 6ad81c62702028559f2ccc2851842c871a2c79c4 Mon Sep 17 00:00:00 2001 From: Tom Gilbert Date: Sat, 27 Aug 2005 14:16:42 +0000 Subject: [PATCH] better setup for ping timeouts. I need to work on the timer code, it's not currently possible to create a timer within a timer event, due to the thread stop/start thing --- lib/rbot/ircbot.rb | 44 +++++++++++++++++++++----------------------- lib/rbot/timer.rb | 4 ++++ 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/lib/rbot/ircbot.rb b/lib/rbot/ircbot.rb index 3881771d..1374929d 100644 --- a/lib/rbot/ircbot.rb +++ b/lib/rbot/ircbot.rb @@ -140,7 +140,8 @@ class IrcBot Dir.mkdir("#{botclass}/logs") unless File.exist?("#{botclass}/logs") @ping_timer = nil - @timeout_timer = nil + @pong_timer = nil + @last_ping = nil @startup_time = Time.new @config = BotConfig.new(self) # TODO background self after botconfig has a chance to run wizard @@ -186,9 +187,7 @@ class IrcBot @socket.puts "PONG #{data[:pingid]}" } @client[:pong] = proc {|data| - # cancel the timeout timer - debug "got a pong from the server, cancelling timeout" - remove_timeout_timer + @last_ping = nil } @client[:nick] = proc {|data| sourcenick = data[:sourcenick] @@ -574,34 +573,33 @@ class IrcBot @timer.remove @ping_timer @ping_timer = nil end - remove_timeout_timer - timeout = @config['server.ping_timeout'] - return unless timeout > 0 - timeout = 30 if timeout > 30 + unless @pong_timer.nil? + @timer.remove @pong_timer + @pong_timer = nil + end + return unless @config['server.ping_timeout'] > 0 # we want to respond to a hung server within 30 secs or so @ping_timer = @timer.add(30) { - remove_timeout_timer + @last_ping = Time.now @socket.puts "PING :rbot" - @timeout_timer = @timer.add_once(timeout) { - debug "no PONG from server for #{timeout} seconds, reconnecting" - begin - @socket.shutdown - rescue - debug "couldn't shutdown connection (already shutdown?)" + } + @pong_timer = @timer.add(10) { + unless @last_ping.nil? + diff = Time.now - @last_ping + unless diff < @config['server.ping_timeout'] + debug "no PONG from server for #{diff} seconds, reconnecting" + begin + @socket.shutdown + rescue + debug "couldn't shutdown connection (already shutdown?)" + end end - } if @config['server.ping_timeout'] + end } end private - def remove_timeout_timer - unless @timeout_timer.nil? - @timer.remove(@timeout_timer) - @timeout_timer = nil - end - end - # handle help requests for "core" topics def corehelp(topic="") case topic diff --git a/lib/rbot/timer.rb b/lib/rbot/timer.rb index 64324b6a..03a4c91e 100644 --- a/lib/rbot/timer.rb +++ b/lib/rbot/timer.rb @@ -78,6 +78,7 @@ module Timer # # add an action to the timer def add(period, data=nil, &func) + debug "adding timer, period #{period}" @handle += 1 @timers[@handle] = Action.new(period, data, &func) start_on_add @@ -90,6 +91,7 @@ module Timer # # add an action to the timer which will be run just once, after +period+ def add_once(period, data=nil, &func) + debug "adding one-off timer, period #{period}" @handle += 1 @timers[@handle] = Action.new(period, data, true, &func) start_on_add @@ -135,6 +137,8 @@ module Timer @next_action_time = timer.in end } + #debug "ticked. now #{@timers.length} timers remain" + #debug "next timer due at #{@next_action_time}" end # for backwards compat - this is a bit primitive -- 2.39.5