* [PATCH 0/3] there is no END{}
@ 2019-12-12 21:16 7% Eric Wong
2019-12-12 21:16 5% ` [PATCH 1/3] ds: move EvCleanup code into DS Eric Wong
0 siblings, 1 reply; 2+ results
From: Eric Wong @ 2019-12-12 21:16 UTC (permalink / raw)
To: meta
END{} blocks make code harder to test and reuse in long-lived
processes. Get rid of them in preparation for allowing daemons
to be restarted in tests without new processes. This may make
our code more multiplicity-friendly in the future :>
Eric Wong (3):
ds: move EvCleanup code into DS
ds: move NNTP-only expiration code into DS
daemon: use DESTROY for unlinking --pid-file
MANIFEST | 1 -
lib/PublicInbox/DS.pm | 64 +++++++++++++++++++++++++++++++++---
lib/PublicInbox/Daemon.pm | 27 +++++++--------
lib/PublicInbox/EvCleanup.pm | 30 -----------------
lib/PublicInbox/HTTP.pm | 1 -
lib/PublicInbox/Inbox.pm | 6 ++--
lib/PublicInbox/NNTP.pm | 49 +++------------------------
t/httpd-unix.t | 41 ++++++++++++-----------
8 files changed, 100 insertions(+), 119 deletions(-)
delete mode 100644 lib/PublicInbox/EvCleanup.pm
^ permalink raw reply [relevance 7%]
* [PATCH 1/3] ds: move EvCleanup code into DS
2019-12-12 21:16 7% [PATCH 0/3] there is no END{} Eric Wong
@ 2019-12-12 21:16 5% ` Eric Wong
0 siblings, 0 replies; 2+ results
From: Eric Wong @ 2019-12-12 21:16 UTC (permalink / raw)
To: meta
EvCleanup only existed since Danga::Socket was a separate
component, and cleanup code belongs with the event loop.
---
MANIFEST | 1 -
lib/PublicInbox/DS.pm | 27 ++++++++++++++++++++++-----
lib/PublicInbox/Daemon.pm | 2 --
lib/PublicInbox/EvCleanup.pm | 30 ------------------------------
lib/PublicInbox/HTTP.pm | 1 -
lib/PublicInbox/Inbox.pm | 6 +++---
lib/PublicInbox/NNTP.pm | 5 ++---
7 files changed, 27 insertions(+), 45 deletions(-)
delete mode 100644 lib/PublicInbox/EvCleanup.pm
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;
}
^ permalink raw reply related [relevance 5%]
Results 1-2 of 2 | reverse | options above
-- pct% links below jump to the message on this page, permalinks otherwise --
2019-12-12 21:16 7% [PATCH 0/3] there is no END{} Eric Wong
2019-12-12 21:16 5% ` [PATCH 1/3] ds: move EvCleanup code into DS 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).