* [PATCH] nntp: implement idle time expiration
@ 2015-10-01 22:10 Eric Wong
2015-10-01 22:38 ` [PATCH 2/1] nntp: remove reference to non-existent function Eric Wong
0 siblings, 1 reply; 2+ messages in thread
From: Eric Wong @ 2015-10-01 22:10 UTC (permalink / raw)
To: meta
We don't want to waste precious server memory on clients which
have been idle for longer than 3 minutes.
---
lib/PublicInbox/NNTP.pm | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 7fe7f2f..796f091 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -33,6 +33,48 @@ my $LIST_HEADERS = join("\r\n", @OVERVIEW,
# LISTGROUP could get pretty bad, too...
my %DISABLED; # = map { $_ => 1 } qw(xover list_overview_fmt newnews xhdr);
+my $EXPMAP; # fd -> [ idle_time, $self ]
+my $EXPTIMER;
+our $EXPTIME = 180; # 3 minutes
+
+sub update_idle_time ($) {
+ my ($self) = @_;
+ my $tmp = $self->{sock} or return;
+ $tmp = fileno($tmp);
+ defined $tmp and $EXPMAP->{$tmp} = [ now(), $self ];
+}
+
+sub expire_old () {
+ my $now = now();
+ my $exp = $EXPTIME;
+ my $old = $now - $exp;
+ my $next = $now + $exp;
+ my $nr = 0;
+ my %new;
+ while (my ($fd, $v) = each %$EXPMAP) {
+ my ($idle_time, $nntp) = @$v;
+ if ($idle_time < $old) {
+ $nntp->close; # idempotent
+ } else {
+ my $nexp = $idle_time + $exp;
+ $next = $nexp if ($nexp < $next);
+ ++$nr;
+ $new{$fd} = $v;
+ }
+ }
+ $EXPMAP = \%new;
+ if ($nr) {
+ $next -= $now;
+ $next = 0 if $next < 0;
+ $EXPTIMER = Danga::Socket->AddTimer($next, *expire_old);
+ } else {
+ $EXPTIMER = undef;
+ # noop to kick outselves out of the loop so descriptors
+ # really get closed
+ Danga::Socket->AddTimer(0, *expire_cleanup);
+ }
+}
+
sub new ($$$) {
my ($class, $sock, $nntpd) = @_;
my $self = fields::new($class);
@@ -42,6 +84,8 @@ sub new ($$$) {
res($self, '201 server ready - post via email');
$self->{rbuf} = '';
$self->watch_read(1);
+ update_idle_time($self);
+ $EXPTIMER ||= Danga::Socket->AddTimer($EXPTIME, *expire_old);
$self;
}
@@ -531,11 +575,13 @@ sub long_response ($$$$) {
out($self, " deferred[$fd] aborted - %0.6f",
now() - $t0);
} else {
+ update_idle_time($self);
$self->watch_read(1);
}
} elsif (!$lim || $self->{write_buf_size}) {
# no recursion, schedule another call ASAP
# but only after all pending writes are done
+ update_idle_time($self);
Danga::Socket->AddTimer(0, sub {
$self->write($self->{long_res});
});
@@ -858,6 +904,7 @@ sub event_err { $_[0]->close }
sub event_write {
my ($self) = @_;
+ update_idle_time($self);
# only continue watching for readability when we are done writing:
if ($self->write(undef) == 1 && !$self->{long_res}) {
$self->watch_read(1);
@@ -884,6 +931,7 @@ sub event_read {
return $self->close if $r < 0;
my $len = length($self->{rbuf});
return $self->close if ($len >= LINE_MAX);
+ update_idle_time($self);
}
sub watch_read {
--
EW
^ permalink raw reply related [flat|nested] 2+ messages in thread
* [PATCH 2/1] nntp: remove reference to non-existent function
2015-10-01 22:10 [PATCH] nntp: implement idle time expiration Eric Wong
@ 2015-10-01 22:38 ` Eric Wong
0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-10-01 22:38 UTC (permalink / raw)
Cc: meta
Oops
---
lib/PublicInbox/NNTP.pm | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 796f091..000b2c6 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -71,7 +71,7 @@ sub expire_old () {
$EXPTIMER = undef;
# noop to kick outselves out of the loop so descriptors
# really get closed
- Danga::Socket->AddTimer(0, *expire_cleanup);
+ Danga::Socket->AddTimer(0, sub {});
}
}
--
EW
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-10-01 22:38 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-01 22:10 [PATCH] nntp: implement idle time expiration Eric Wong
2015-10-01 22:38 ` [PATCH 2/1] nntp: remove reference to non-existent function Eric Wong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).