Changeset 43b5cd9777cc13c6d3ac14a4015a0c8e8d9e5b50

Show
Ignore:
Timestamp:
08/19/08 22:19:45 (3 months ago)
Author:
Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
git-committer:
Giuseppe Bilotta <giuseppe.bilotta@gmail.com> 1219169985 +0200
git-parent:

[9340ab24696230a7f5debecf272b44ad91886b9c]

git-author:
Giuseppe Bilotta <giuseppe.bilotta@gmail.com> 1219169985 +0200
Message:

IRC socket: get rid of delay/burst

The penalty system should be enough to prevent the bot from being
disconnected because of excess flood, making the old sendq delay/burst
code unnecessary. So get rid of the latter altogether.

(If the penalty system as implemented ever happens to be insufficient as
implemented, it should just get fixed rather than rely on the sendq
delay/burst assistance.)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lib/rbot/ircbot.rb

    reebdc69 r43b5cd9  
    278278      :default => 5, :validate => Proc.new{|v| v >= 0}, 
    279279      :desc => "Seconds to wait before attempting to reconnect, on disconnect") 
    280     Config.register Config::FloatValue.new('server.sendq_delay', 
    281       :default => 2.0, :validate => Proc.new{|v| v >= 0}, 
    282       :desc => "(flood prevention) the delay between sending messages to the server (in seconds)", 
    283       :on_change => Proc.new {|bot, v| bot.socket.sendq_delay = v }) 
    284     Config.register Config::IntegerValue.new('server.sendq_burst', 
    285       :default => 4, :validate => Proc.new{|v| v >= 0}, 
    286       :desc => "(flood prevention) max lines to burst to the server before throttling. Most ircd's allow bursts of up 5 lines", 
    287       :on_change => Proc.new {|bot, v| bot.socket.sendq_burst = v }) 
    288280    Config.register Config::IntegerValue.new('server.ping_timeout', 
    289281      :default => 30, :validate => Proc.new{|v| v >= 0}, 
     
    590582    end 
    591583 
    592     @socket = Irc::Socket.new(@config['server.list'], @config['server.bindhost'], @config['server.sendq_delay'], @config['server.sendq_burst'], :ssl => @config['server.ssl']) 
     584    @socket = Irc::Socket.new(@config['server.list'], @config['server.bindhost'], :ssl => @config['server.ssl']) 
    593585    @client = Client.new 
    594586 
  • lib/rbot/ircsocket.rb

    rf95875a r43b5cd9  
    232232    attr_reader :throttle_bytes 
    233233 
    234     # delay between lines sent 
    235     attr_accessor :sendq_delay 
    236  
    237     # max lines to burst 
    238     attr_accessor :sendq_burst 
    239  
    240234    # an optional filter object. we call @filter.in(data) for 
    241235    # all incoming data and @filter.out(data) for all outgoing data 
     
    264258    # host::   optional local host to bind to (ruby 1.7+ required) 
    265259    # create a new Irc::Socket 
    266     def initialize(server_list, host, sendq_delay=2, sendq_burst=4, opts={}) 
     260    def initialize(server_list, host, opts={}) 
    267261      @server_list = server_list.dup 
    268262      @server_uri = nil 
     
    279273        @ssl = false 
    280274      end 
    281  
    282       if sendq_delay 
    283         @sendq_delay = sendq_delay.to_f 
    284       else 
    285         @sendq_delay = 2 
    286       end 
    287       if sendq_burst 
    288         @sendq_burst = sendq_burst.to_i 
    289       else 
    290         @sendq_burst = 4 
    291       end 
    292275    end 
    293276 
     
    331314      end 
    332315      @sock = sock 
    333       @last_send = Time.new - @sendq_delay 
     316      @last_send = Time.new 
    334317      @flood_send = Time.new 
    335       @last_throttle = Time.new 
    336318      @burst = 0 
    337319      @sock.extend(MonitorMixin) 
     
    406388      end 
    407389      @sock = nil 
    408       @burst = 0 
    409390      @sendq.clear 
    410391    end 
     
    414395    def writer_loop 
    415396      loop do 
    416         # we could wait for the message, then calculate the delay and sleep 
    417         # if necessary. however, if high-priority message is enqueued while 
    418         # we sleep, it won't be the first to go out when the sleep is over. 
    419         # thus, we have to call Time.now() twice, once to calculate the delay 
    420         # and once to adjust @burst / @flood_send. 
    421397        begin 
    422398          now = Time.now 
    423           if @sendq_delay > 0 
    424             burst_delay = 0 
    425             if @burst > @sendq_burst 
    426               burst_delay = @last_send + @sendq_delay - now 
    427             end 
    428  
    429             flood_delay = @flood_send - MAX_IRC_SEND_PENALTY - now 
    430             delay = [burst_delay, flood_delay, 0].max 
    431             if delay > 0 
    432               debug "sleep(#{delay}) # (f: #{flood_delay}, b: #{burst_delay})" 
    433               sleep(delay)  
    434             end 
     399          flood_delay = @flood_send - MAX_IRC_SEND_PENALTY - now 
     400          delay = [flood_delay, 0].max 
     401          if delay > 0 
     402            debug "sleep(#{delay}) # (f: #{flood_delay})" 
     403            sleep(delay) 
    435404          end 
    436405          msg = @sendq.shift 
    437406          now = Time.now 
    438407          @flood_send = now if @flood_send < now 
    439           @burst = 0 if @last_send + @sendq_delay < now 
    440408          debug "got #{msg.inspect} from queue, sending" 
    441409          emergency_puts(msg, true) 
     
    464432          @flood_send += message.irc_send_penalty if penalty 
    465433          @lines_sent += 1 
    466           @burst += 1 
    467434        end 
    468435      rescue Exception => e