11 my $url = $ARGV[0] // "/analysis.pl"; # Technically an URL regex, not an URL.
12 my $port = $ARGV[1] // 5000;
14 open my $fh, "-|", "varnishncsa -F '%{%s}t %U %q tffb=%{Varnish:time_firstbyte}x' -q 'ReqURL ~ \"^$url\"'"
15 or die "varnishncsa: $!";
18 my $ev = AnyEvent::Handle->new(
24 my ($hdl, $line, $eof) = @_;
30 my $ev2 = AnyEvent->timer(
38 $line =~ m#(\d+) /analysis.pl \?ims=\d+&unique=(.*) tffb=(.*)# or return;
44 print "[$now] $1 $2 $3\n";
48 my $mtime = (stat($remoteglotconf::json_output))[9] - 1; # Compensate for subsecond issues.
51 while (my ($unique, $hash) = each %uniques) {
52 my $last_seen = $hash->{'last_seen'};
53 if ($now - $last_seen <= 5) {
54 # We've seen this user in the last five seconds;
58 if ($last_seen >= $mtime) {
59 # This user has the latest version;
60 # they are probably just hanging.
63 if (!defined($hash->{'grace'})) {
64 # They have five seconds after a new JSON has been
65 # provided to get get it, or they're out.
66 # We don't simply use $mtime, since we don't want to
67 # reset the grace timer just because a new JSON is
69 $hash->{'grace'} = $mtime;
71 if ($now - $hash->{'grace'} > 5) {
72 printf "Timing out %s (last_seen=%d, now=%d, mtime=%d, grace=%d)\n",
73 $unique, $last_seen, $now, $mtime, $hash->{'grace'};
74 delete $uniques{$unique};
78 my $num_viewers = scalar keys %uniques;
79 printf "%d entries in hash, mtime=$mtime\n", scalar keys %uniques;
80 LWP::Simple::get('http://127.0.0.1:' . $port . '/override-num-viewers?num=' . $num_viewers);