Changeset 43b5cd9777cc13c6d3ac14a4015a0c8e8d9e5b50
- 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
| reebdc69 |
r43b5cd9 |
|
| 278 | 278 | :default => 5, :validate => Proc.new{|v| v >= 0}, |
|---|
| 279 | 279 | :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 }) |
|---|
| 288 | 280 | Config.register Config::IntegerValue.new('server.ping_timeout', |
|---|
| 289 | 281 | :default => 30, :validate => Proc.new{|v| v >= 0}, |
|---|
| … | … | |
| 590 | 582 | end |
|---|
| 591 | 583 | |
|---|
| 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']) |
|---|
| 593 | 585 | @client = Client.new |
|---|
| 594 | 586 | |
|---|
| rf95875a |
r43b5cd9 |
|
| 232 | 232 | attr_reader :throttle_bytes |
|---|
| 233 | 233 | |
|---|
| 234 | | # delay between lines sent |
|---|
| 235 | | attr_accessor :sendq_delay |
|---|
| 236 | | |
|---|
| 237 | | # max lines to burst |
|---|
| 238 | | attr_accessor :sendq_burst |
|---|
| 239 | | |
|---|
| 240 | 234 | # an optional filter object. we call @filter.in(data) for |
|---|
| 241 | 235 | # all incoming data and @filter.out(data) for all outgoing data |
|---|
| … | … | |
| 264 | 258 | # host:: optional local host to bind to (ruby 1.7+ required) |
|---|
| 265 | 259 | # 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={}) |
|---|
| 267 | 261 | @server_list = server_list.dup |
|---|
| 268 | 262 | @server_uri = nil |
|---|
| … | … | |
| 279 | 273 | @ssl = false |
|---|
| 280 | 274 | 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 |
|---|
| 292 | 275 | end |
|---|
| 293 | 276 | |
|---|
| … | … | |
| 331 | 314 | end |
|---|
| 332 | 315 | @sock = sock |
|---|
| 333 | | @last_send = Time.new - @sendq_delay |
|---|
| | 316 | @last_send = Time.new |
|---|
| 334 | 317 | @flood_send = Time.new |
|---|
| 335 | | @last_throttle = Time.new |
|---|
| 336 | 318 | @burst = 0 |
|---|
| 337 | 319 | @sock.extend(MonitorMixin) |
|---|
| … | … | |
| 406 | 388 | end |
|---|
| 407 | 389 | @sock = nil |
|---|
| 408 | | @burst = 0 |
|---|
| 409 | 390 | @sendq.clear |
|---|
| 410 | 391 | end |
|---|
| … | … | |
| 414 | 395 | def writer_loop |
|---|
| 415 | 396 | 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. |
|---|
| 421 | 397 | begin |
|---|
| 422 | 398 | 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) |
|---|
| 435 | 404 | end |
|---|
| 436 | 405 | msg = @sendq.shift |
|---|
| 437 | 406 | now = Time.now |
|---|
| 438 | 407 | @flood_send = now if @flood_send < now |
|---|
| 439 | | @burst = 0 if @last_send + @sendq_delay < now |
|---|
| 440 | 408 | debug "got #{msg.inspect} from queue, sending" |
|---|
| 441 | 409 | emergency_puts(msg, true) |
|---|
| … | … | |
| 464 | 432 | @flood_send += message.irc_send_penalty if penalty |
|---|
| 465 | 433 | @lines_sent += 1 |
|---|
| 466 | | @burst += 1 |
|---|
| 467 | 434 | end |
|---|
| 468 | 435 | rescue Exception => e |
|---|