From: Eric Wong <e@80x24.org>
Date: Thu, 12 Dec 2019 21:16:47 +0000 (+0000)
Subject: ds: move EvCleanup code into DS
X-Git-Tag: v1.3.0~229
X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=0541761b0558317be1825710c1545f6e718103f2;p=public-inbox.git

ds: move EvCleanup code into DS

EvCleanup only existed since Danga::Socket was a separate
component, and cleanup code belongs with the event loop.
---

diff --git a/MANIFEST b/MANIFEST
index 044bbfef..edbbbfad 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -92,7 +92,6 @@ lib/PublicInbox/DSKQXS.pm
 lib/PublicInbox/DSPoll.pm
 lib/PublicInbox/Daemon.pm
 lib/PublicInbox/Emergency.pm
-lib/PublicInbox/EvCleanup.pm
 lib/PublicInbox/ExtMsg.pm
 lib/PublicInbox/Feed.pm
 lib/PublicInbox/Filter/Base.pm
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index 301ec057..7eb0aada 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -41,7 +41,8 @@ require File::Spec;
 
 my $nextq; # queue for next_tick
 my $WaitPids; # list of [ pid, callback, callback_arg ]
-my $reap_timer;
+my $later_queue; # callbacks
+my ($later_timer, $reap_timer);
 our (
      %DescriptorMap,             # fd (num) -> PublicInbox::DS object
      $Epoll,                     # Global epoll fd (or DSKQXS ref)
@@ -71,7 +72,8 @@ sub Reset {
     %DescriptorMap = ();
     $nextq = [];
     $WaitPids = [];
-    $reap_timer = undef;
+    $later_queue = [];
+    $reap_timer = $later_timer = undef;
     @ToClose = ();
     $LoopTimeout = -1;  # no timeout by default
     @Timers = ();
@@ -250,9 +252,11 @@ sub reap_pids {
 # reentrant SIGCHLD handler (since reap_pids is not reentrant)
 sub enqueue_reap ($) { push @$nextq, \&reap_pids };
 
+sub running () { ($SIG{CHLD} // '') eq \&enqueue_reap }
+
 sub EpollEventLoop {
     local $in_loop = 1;
-    while (1) {
+    do {
         my @events;
         my $i;
         my $timeout = RunTimers();
@@ -266,8 +270,8 @@ sub EpollEventLoop {
             # in that event.
             $DescriptorMap{$events[$i]->[0]}->event_step;
         }
-        return unless PostEventLoop();
-    }
+    } while (PostEventLoop());
+    _run_later();
 }
 
 =head2 C<< CLASS->SetPostLoopCallback( CODEREF ) >>
@@ -640,6 +644,19 @@ sub dwaitpid ($$$) {
     }
 }
 
+sub _run_later () {
+    my $run = $later_queue;
+    $later_timer = undef;
+    $later_queue = [];
+    $_->() for @$run;
+}
+
+sub later ($) {
+    my ($cb) = @_;
+    push @$later_queue, $cb;
+    $later_timer //= AddTimer(undef, 60, \&_run_later);
+}
+
 package PublicInbox::DS::Timer;
 # [$abs_float_firetime, $coderef];
 sub cancel {
diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm
index c7a71ba0..c2c05b96 100644
--- a/lib/PublicInbox/Daemon.pm
+++ b/lib/PublicInbox/Daemon.pm
@@ -16,7 +16,6 @@ STDOUT->autoflush(1);
 STDERR->autoflush(1);
 use PublicInbox::DS qw(now);
 use PublicInbox::Syscall qw(SFD_NONBLOCK);
-require PublicInbox::EvCleanup;
 require PublicInbox::Listener;
 require PublicInbox::ParentPipe;
 require PublicInbox::Sigfd;
@@ -580,7 +579,6 @@ sub defer_accept ($$) {
 
 sub daemon_loop ($$$$) {
 	my ($refresh, $post_accept, $nntpd, $af_default) = @_;
-	PublicInbox::EvCleanup::enable(); # early for $refresh
 	my %post_accept;
 	while (my ($k, $v) = each %tls_opt) {
 		if ($k =~ s!\A(?:nntps|https)://!!) {
diff --git a/lib/PublicInbox/EvCleanup.pm b/lib/PublicInbox/EvCleanup.pm
deleted file mode 100644
index be6672ed..00000000
--- a/lib/PublicInbox/EvCleanup.pm
+++ /dev/null
@@ -1,30 +0,0 @@
-# Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
-# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
-
-# event cleanups (for PublicInbox::DS)
-package PublicInbox::EvCleanup;
-use strict;
-use warnings;
-require PublicInbox::DS;
-
-# this only runs under public-inbox-{httpd/nntpd}, not generic PSGI servers
-my $ENABLED;
-sub enabled { $ENABLED }
-sub enable { $ENABLED = 1 }
-my $laterq = [ [], undef ];
-
-sub _run_later () {
-	my $run = $laterq->[0];
-	$laterq->[0] = [];
-	$laterq->[1] = undef;
-	$_->() foreach @$run;
-}
-
-sub later ($) {
-	my ($cb) = @_;
-	push @{$laterq->[0]}, $cb;
-	$laterq->[1] ||= PublicInbox::DS->AddTimer(60, *_run_later);
-}
-
-END { _run_later() }
-1;
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 009b5ff0..53d50836 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -18,7 +18,6 @@ use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
 use IO::Handle;
-require PublicInbox::EvCleanup;
 use PublicInbox::DS qw(msg_more);
 use PublicInbox::Syscall qw(EPOLLIN EPOLLONESHOT);
 use PublicInbox::Tmpfile;
diff --git a/lib/PublicInbox/Inbox.pm b/lib/PublicInbox/Inbox.pm
index 5feb2154..43c6a428 100644
--- a/lib/PublicInbox/Inbox.pm
+++ b/lib/PublicInbox/Inbox.pm
@@ -50,9 +50,9 @@ sub cleanup_task () {
 }
 
 sub cleanup_possible () {
-	# no need to require EvCleanup, here, if it were enabled another
+	# no need to require DS, here, if it were enabled another
 	# module would've require'd it, already
-	eval { PublicInbox::EvCleanup::enabled() } or return 0;
+	eval { PublicInbox::DS::running() } or return 0;
 
 	eval {
 		require Devel::Peek; # needs separate package in Fedora
@@ -65,7 +65,7 @@ sub _cleanup_later ($) {
 	my ($self) = @_;
 	$cleanup_avail = cleanup_possible() if $cleanup_avail < 0;
 	return if $cleanup_avail != 1;
-	$cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
+	$cleanup_timer ||= PublicInbox::DS::later(*cleanup_task);
 	$CLEANUP->{"$self"} = $self;
 }
 
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index c0496852..c9487114 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -11,7 +11,6 @@ use PublicInbox::Search;
 use PublicInbox::Msgmap;
 use PublicInbox::MID qw(mid_escape);
 use PublicInbox::Git;
-require PublicInbox::EvCleanup;
 use Email::Simple;
 use POSIX qw(strftime);
 use PublicInbox::DS qw(now);
@@ -70,7 +69,7 @@ sub expire_old () {
 		}
 	}
 	$EXPMAP = \%new;
-	$expt = scalar(keys %new) ? PublicInbox::EvCleanup::later(*expire_old)
+	$expt = scalar(keys %new) ? PublicInbox::DS::later(*expire_old)
 	                          : undef;
 }
 
@@ -94,7 +93,7 @@ sub new ($$$) {
 		greet($self);
 	}
 	update_idle_time($self);
-	$expt ||= PublicInbox::EvCleanup::later(*expire_old);
+	$expt ||= PublicInbox::DS::later(*expire_old);
 	$self;
 }