From 60ad02c9af4b46a13102b35f81bc7204707da9c1 Mon Sep 17 00:00:00 2001 From: Pragmatic Software Date: Sun, 11 Dec 2011 23:33:01 +0000 Subject: [PATCH] lagchecker: include elapsed time from last ping sent in average if no pong yet received --- PBot/LagChecker.pm | 18 +++++++++++++++++- PBot/VERSION.pm | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/PBot/LagChecker.pm b/PBot/LagChecker.pm index 2ff93c19..31136152 100644 --- a/PBot/LagChecker.pm +++ b/PBot/LagChecker.pm @@ -47,6 +47,7 @@ sub initialize { $self->{lag_average} = undef; # average of entries in lag history, in seconds $self->{lag_string} = undef; # string representation of lag history and lag average $self->{lag_history} = []; # history of previous PING/PONG timings + $self->{pong_received} = undef; # tracks pong replies; undef if no ping sent; 0 if ping sent but no pong reply yet; 1 if ping/pong completed $pbot->timer->register(sub { $self->send_ping }, $self->{LAG_HISTORY_INTERVAL}); @@ -59,13 +60,15 @@ sub send_ping { return unless defined $self->{pbot}->conn; $self->{ping_send_time} = [gettimeofday]; + $self->{pong_received} = 0; $self->{pbot}->conn->sl("PING :lagcheck"); - # $self->{pbot}->logger->log("sent lagcheck PING\n"); } sub on_pong { my $self = shift; + $self->{pong_received} = 1; + my $elapsed = tv_interval($self->{ping_send_time}); push @{ $self->{lag_history} }, [ $self->{ping_send_time}[0], $elapsed ]; @@ -96,6 +99,19 @@ sub on_pong { sub lagging { my $self = shift; + if(defined $self->{pong_received} and $self->{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->{ping_send_time}); + my $lag_total = $elapsed; + my $len = @{ $self->{lag_history} }; + foreach my $entry (@{ $self->{lag_history} }) { + my ($send_time, $lag_result) = @{ $entry }; + $lag_total += $lag_result; + } + my $average = $lag_total / ($len + 1); + return $average >= $self->{LAG_THRESHOLD}; + } + return 0 if not defined $self->{lag_average}; return $self->{lag_average} >= $self->{LAG_THRESHOLD}; } diff --git a/PBot/VERSION.pm b/PBot/VERSION.pm index 01295b1f..556a2488 100644 --- a/PBot/VERSION.pm +++ b/PBot/VERSION.pm @@ -13,7 +13,7 @@ use warnings; # These are set automatically by the build/commit script use constant { BUILD_NAME => "PBot", - BUILD_REVISION => 326, + BUILD_REVISION => 327, BUILD_DATE => "2011-12-11", };