user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
Search results ordered by [date|relevance]  view[summary|nested|Atom feed]
thread overview below | download mbox.gz: |
* [PATCH] nntp: implement idle time expiration
@ 2015-10-01 22:10  7% Eric Wong
  0 siblings, 0 replies; 1+ results
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	[relevance 7%]

Results 1-1 of 1 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2015-10-01 22:10  7% [PATCH] nntp: implement idle time expiration Eric Wong

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