user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] nntp: implement idle time expiration
Date: Thu,  1 Oct 2015 22:10:31 +0000	[thread overview]
Message-ID: <20151001221031.10594-1-e@80x24.org> (raw)

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


             reply	other threads:[~2015-10-01 22:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-01 22:10 Eric Wong [this message]
2015-10-01 22:38 ` [PATCH 2/1] nntp: remove reference to non-existent function Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151001221031.10594-1-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).