From f54efb0864b4516f942df2011ef1aaf169786288 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Thu, 25 Sep 2025 15:04:21 -0700 Subject: [PATCH] Improve LagChecker - wait until fully connected (irc.welcome) before sending PING - do not send another PING until PONG is received These changes prevent the bot from sending multiple PINGs when the ircd is heavily lagging. The multiple PINGs would queue up and then get sent all at once when the ircd unlagged, potentially causing an excess-flood disconnection. --- lib/PBot/Core/Commands/LagChecker.pm | 4 ++-- lib/PBot/Core/LagChecker.pm | 14 +++++++++++++- lib/PBot/VERSION.pm | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/PBot/Core/Commands/LagChecker.pm b/lib/PBot/Core/Commands/LagChecker.pm index c50778fb..da1496fd 100644 --- a/lib/PBot/Core/Commands/LagChecker.pm +++ b/lib/PBot/Core/Commands/LagChecker.pm @@ -11,7 +11,7 @@ use parent 'PBot::Core::Class'; use PBot::Imports; use Time::Duration qw/concise ago/; -use Time::HiRes qw/gettimeofday/; +use Time::HiRes qw/gettimeofday tv_interval/; sub initialize($self, %conf) { $self->{pbot}->{commands}->register(sub { $self->cmd_lagcheck(@_) }, "lagcheck", 0); @@ -20,7 +20,7 @@ sub initialize($self, %conf) { sub cmd_lagcheck($self, $context) { if (defined $self->{pbot}->{lagchecker}->{pong_received} and $self->{pbot}->{lagchecker}->{pong_received} == 0) { # a ping has been sent (pong_received is not undef) and no pong has been received yet - my $elapsed = tv_interval($self->{pbot}->{lagchecker}->{ping_send_time}); + my $elapsed = tv_interval($self->{pbot}->{lagchecker}->{ping_send_time}) * 1000; my $lag_total = $elapsed; my $len = @{$self->{pbot}->{lagchecker}->{lag_history}}; diff --git a/lib/PBot/Core/LagChecker.pm b/lib/PBot/Core/LagChecker.pm index eb5c458b..ae628b6b 100644 --- a/lib/PBot/Core/LagChecker.pm +++ b/lib/PBot/Core/LagChecker.pm @@ -15,6 +15,9 @@ use Time::HiRes qw(gettimeofday tv_interval); use Time::Duration; sub initialize($self, %conf) { + # are we fully connected yet? + $self->{welcomed} = 0; + # average of entries in lag history, in seconds $self->{lag_average} = undef; @@ -51,6 +54,9 @@ sub initialize($self, %conf) { # PONG IRC handler $self->{pbot}->{event_dispatcher}->register_handler('irc.pong', sub { $self->on_pong(@_) }); + + # Don't send PING until fully connected + $self->{pbot}->{event_dispatcher}->register_handler('irc.welcome', sub { $self->on_welcome(@_) }); } # registry trigger fires when value changes @@ -59,7 +65,9 @@ sub trigger_lag_history_interval($self, $section, $item, $newvalue) { } sub send_ping($self) { - return unless defined $self->{pbot}->{conn}; + return unless defined $self->{pbot}->{conn} && $self->{pbot}->{conn}->connected && $self->{welcomed}; + + return if defined $self->{pong_received} && $self->{pong_received} == 0; $self->{ping_send_time} = [gettimeofday]; $self->{pong_received} = 0; @@ -67,6 +75,10 @@ sub send_ping($self) { $self->{pbot}->{conn}->sl("PING :lagcheck"); } +sub on_welcome($self, $event_type, $event) { + $self->{welcomed} = 1; +} + sub on_pong($self, $event_type, $event) { $self->{pong_received} = 1; diff --git a/lib/PBot/VERSION.pm b/lib/PBot/VERSION.pm index 9e55fcc9..ac08e30e 100644 --- a/lib/PBot/VERSION.pm +++ b/lib/PBot/VERSION.pm @@ -25,7 +25,7 @@ use PBot::Imports; # These are set by the /misc/update_version script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 4895, + BUILD_REVISION => 4896, BUILD_DATE => "2025-09-25", };