From ede8cc1c664e332cfa44bd22c36a31aac1a5fb13 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 12 Dec 2019 21:16:48 +0000 Subject: ds: move NNTP-only expiration code into DS We'll be supporting idle timeout for the HTTP code in the future to deal directly with Internet-exposed clients w/o Varnish or nginx. --- lib/PublicInbox/DS.pm | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'lib/PublicInbox/DS.pm') diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 7eb0aada..856884bb 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -42,7 +42,9 @@ require File::Spec; my $nextq; # queue for next_tick my $WaitPids; # list of [ pid, callback, callback_arg ] my $later_queue; # callbacks -my ($later_timer, $reap_timer); +my $EXPMAP; # fd -> [ idle_time, $self ] +our $EXPTIME = 180; # 3 minutes +my ($later_timer, $reap_timer, $exp_timer); our ( %DescriptorMap, # fd (num) -> PublicInbox::DS object $Epoll, # Global epoll fd (or DSKQXS ref) @@ -73,7 +75,8 @@ sub Reset { $nextq = []; $WaitPids = []; $later_queue = []; - $reap_timer = $later_timer = undef; + $EXPMAP = {}; + $reap_timer = $later_timer = $exp_timer = undef; @ToClose = (); $LoopTimeout = -1; # no timeout by default @Timers = (); @@ -657,6 +660,40 @@ sub later ($) { $later_timer //= AddTimer(undef, 60, \&_run_later); } +sub expire_old () { + my $now = now(); + my $exp = $EXPTIME; + my $old = $now - $exp; + my %new; + while (my ($fd, $v) = each %$EXPMAP) { + my ($idle_time, $ds_obj) = @$v; + if ($idle_time < $old) { + if (!$ds_obj->shutdn) { + $new{$fd} = $v; + } + } else { + $new{$fd} = $v; + } + } + $EXPMAP = \%new; + $exp_timer = scalar(keys %new) ? later(\&expire_old) : undef; +} + +sub update_idle_time { + my ($self) = @_; + my $sock = $self->{sock} or return; + $EXPMAP->{fileno($sock)} = [ now(), $self ]; + $exp_timer //= later(\&expire_old); +} + +sub not_idle_long { + my ($self, $now) = @_; + my $sock = $self->{sock} or return; + my $ary = $EXPMAP->{fileno($sock)} or return; + my $exp_at = $ary->[0] + $EXPTIME; + $exp_at > $now; +} + package PublicInbox::DS::Timer; # [$abs_float_firetime, $coderef]; sub cancel { -- cgit v1.2.3-24-ge0c7