* [PATCH 0/7] another round of NNTP updates
@ 2015-09-24 3:37 Eric Wong
2015-09-24 3:37 ` [PATCH 1/7] nntp: fix XOVER command Eric Wong
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
We'll still need better tests, but most of this seems to
work alright. Must. Fix. XOVER. Performance. Next!
Eric Wong (7):
nntp: fix XOVER command
nntpd: additional daemonization options
nntpd: hard quit after 30s
nntpd: move busy check to NNTP package
nntp: avoid infinite loop on partial read
nntpd: better encapsulation for shutdown
nntpd: support SIGUSR2 in single-process mode, too
lib/PublicInbox/NNTP.pm | 21 +++++----
public-inbox-nntpd | 118 +++++++++++++++++++++++++++++++++++++++++++-----
t/nntpd.t | 9 +++-
3 files changed, 126 insertions(+), 22 deletions(-)
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH 1/7] nntp: fix XOVER command
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
2015-09-24 3:37 ` [PATCH 2/7] nntpd: additional daemonization options Eric Wong
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
Oops, we need to test commands more closely :x
Add a missing prototype while we're at it for extra
checking.
---
lib/PublicInbox/NNTP.pm | 4 ++--
t/nntpd.t | 9 ++++++++-
2 files changed, 10 insertions(+), 3 deletions(-)
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 6dc0db3..0d0de97 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -762,11 +762,11 @@ sub cmd_xrover ($;$) {
});
}
-sub over_line {
+sub over_line ($$) {
my ($self, $r) = @_;
more($self, join("\t", $r->[0], map {
- my $h = xhdr($r, $_);
+ my $h = hdr_val($r, $_);
defined $h ? $h : '';
} @OVERVIEW ));
}
diff --git a/t/nntpd.t b/t/nntpd.t
index 04444e4..ea2c3df 100644
--- a/t/nntpd.t
+++ b/t/nntpd.t
@@ -144,7 +144,14 @@ EOF
}
}
- # TODO: upgrades and such
+ is_deeply($n->xover('1-'), {
+ '1' => ['hihi',
+ 'Me <me@example.com>',
+ 'Thu, 01 Jan 1970 06:06:06 +0000',
+ '<nntp@example.com>',
+ '',
+ '202',
+ '1' ] }, "XOVER works");
ok(kill('TERM', $pid), 'killed nntpd');
$pid = undef;
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 2/7] nntpd: additional daemonization options
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
2015-09-24 3:37 ` [PATCH 1/7] nntp: fix XOVER command Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
2015-09-24 3:37 ` [PATCH 3/7] nntpd: hard quit after 30s Eric Wong
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta; +Cc: Eric Wong
From: Eric Wong <normalperson@yhbt.net>
This should fully support the signals used by nginx and starman,
so scripts used by nginx can be used to manage our nntpd daemon,
too.
---
public-inbox-nntpd | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 93 insertions(+), 5 deletions(-)
diff --git a/public-inbox-nntpd b/public-inbox-nntpd
index 674ecad..defb16b 100644
--- a/public-inbox-nntpd
+++ b/public-inbox-nntpd
@@ -8,26 +8,39 @@ require Danga::Socket;
require IO::Handle;
use Getopt::Long qw/:config gnu_getopt no_ignore_case auto_abbrev/;
require PublicInbox::NewsGroup;
+my $set_user;
my $nntpd = PublicInbox::NNTPD->new;
my $refresh = sub { $nntpd->refresh_groups };
$SIG{HUP} = $SIG{USR1} = $SIG{USR2} = $SIG{PIPE} =
$SIG{TTIN} = $SIG{TTOU} = $SIG{WINCH} = 'IGNORE';
$refresh->();
-my (@cfg_listen, $stdout, $stderr);
+my (@cfg_listen, $stdout, $stderr, $group, $user, $pid_file, $daemonize);
my $worker_processes = 0;
my %opts = (
'l|listen=s' => \@cfg_listen,
'1|stdout=s' => \$stdout,
'2|stderr=s' => \$stderr,
'W|worker-processes=i' => \$worker_processes,
+ 'P|pid-file=s' => \$pid_file,
+ 'u|user=s' => \$user,
+ 'g|group=s' => \$group,
+ 'D|daemonize' => \$daemonize,
);
GetOptions(%opts) or die "bad command-line args\n";
+
+if (defined $pid_file && $pid_file =~ /\.oldbin\z/) {
+ die "--pid-file cannot end with '.oldbin'\n";
+}
+
my %pids;
my %listener_names;
my $reexec_pid;
my @listeners = inherit();
+# ignore daemonize when inheriting
+$daemonize = undef if scalar @listeners;
+
# default NNTP listener if no listeners
push @cfg_listen, '0.0.0.0:119' unless (@listeners || @cfg_listen);
@@ -47,10 +60,50 @@ foreach my $l (@cfg_listen) {
}
}
die 'No listeners bound' unless @listeners;
-open(STDIN, '+<', '/dev/null');
+
+chdir '/' or die "chdir failed: $!\n";
+open(STDIN, '+<', '/dev/null') or die "redirect stdin failed: $!\n";
+
+if (defined $pid_file || defined $group || defined $user || $daemonize) {
+ require Net::Server::Daemonize;
+
+ Net::Server::Daemonize::check_pid_file($pid_file) if defined $pid_file;
+ my $uid = Net::Server::Daemonize::get_uid($user) if defined $user;
+ my $gid;
+ if (defined $group) {
+ $gid = Net::Server::Daemonize::get_gid($group);
+ $gid = (split /\s+/, $gid)[0];
+ } elsif (defined $uid) {
+ $gid = (getpwuid($uid))[3];
+ }
+
+ # We change users in the worker to ensure upgradability,
+ # The upgrade will create the ".oldbin" pid file in the
+ # same directory as the given pid file.
+ $uid and $set_user = sub {
+ Net::Server::Daemonize::set_user($uid, $gid);
+ };
+
+ if ($daemonize) {
+ my ($pid, $err) = do_fork();
+ die "could not fork: $err\n" unless defined $pid;
+ exit if $pid;
+
+ open STDOUT, '>&STDIN' or die "redirect stdout failed: $!\n";
+ open STDERR, '>&STDIN' or die "redirect stderr failed: $!\n";
+ POSIX::setsid();
+ ($pid, $err) = do_fork();
+ die "could not fork: $err\n" unless defined $pid;
+ exit if $pid;
+ }
+ if (defined $pid_file) {
+ my $unlink_pid = $$;
+ Net::Server::Daemonize::create_pid_file($pid_file);
+ END { unlink_pid_file_safe_ish($unlink_pid, $pid_file) };
+ }
+}
if ($worker_processes > 0) {
- # my ($p0, $p1, $r, $w);
pipe(my ($p0, $p1)) or die "failed to create parent-pipe: $!\n";
my %pwatch = ( fileno($p0) => sub { kill('TERM', $$) } );
pipe(my ($r, $w)) or die "failed to create self-pipe: $!\n";
@@ -110,6 +163,7 @@ if ($worker_processes > 0) {
if (!defined $pid) {
warn "failed to fork worker[$i]: $err\n";
} elsif ($pid == 0) {
+ $set_user->() if $set_user;
close($_) for ($w, $r, $p1);
Danga::Socket->AddOtherFds(%pwatch);
goto worker;
@@ -209,6 +263,15 @@ sub upgrade {
warn "upgrade in-progress: $reexec_pid\n";
return;
}
+ if (defined $pid_file) {
+ if ($pid_file =~ /\.oldbin\z/) {
+ warn "BUG: .oldbin suffix exists: $pid_file\n";
+ return;
+ }
+ unlink_pid_file_safe_ish($$, $pid_file);
+ $pid_file .= '.oldbin';
+ Net::Server::Daemonize::create_pid_file($pid_file);
+ }
my ($pid, $err) = do_fork();
unless (defined $pid) {
warn "fork failed: $err\n";
@@ -250,12 +313,25 @@ sub do_fork {
($pid, $err);
}
+sub upgrade_aborted ($) {
+ my ($p) = @_;
+ warn "reexec PID($p) died with: $?\n";
+ $reexec_pid = undef;
+ return unless $pid_file;
+
+ my $file = $pid_file;
+ $file =~ s/\.oldbin\z// or die "BUG: no '.oldbin' suffix in $file\n";
+ unlink_pid_file_safe_ish($$, $pid_file);
+ $pid_file = $file;
+ eval { Net::Server::Daemonize::create_pid_file($pid_file) };
+ warn $@, "\n" if $@;
+}
+
sub reap_children {
while (1) {
my $p = waitpid(-1, &POSIX::WNOHANG) or return;
if (defined $reexec_pid && $p == $reexec_pid) {
- $reexec_pid = undef;
- warn "reexec PID($p) died with: $?\n";
+ upgrade_aborted($p);
} elsif (defined(my $id = delete $pids{$p})) {
warn "worker[$id] PID($p) died with: $?\n";
} elsif ($p > 0) {
@@ -266,6 +342,18 @@ sub reap_children {
}
}
+sub unlink_pid_file_safe_ish ($$) {
+ my ($unlink_pid, $file) = @_;
+ return unless defined $unlink_pid && $unlink_pid == $$;
+
+ open my $fh, '<', $file or return;
+ defined(my $read_pid = <$fh>) or return;
+ chomp $read_pid;
+ if ($read_pid == $unlink_pid) {
+ Net::Server::Daemonize::unlink_pid_file($file);
+ }
+}
+
1;
package PublicInbox::Listener;
use strict;
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 3/7] nntpd: hard quit after 30s
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
2015-09-24 3:37 ` [PATCH 1/7] nntp: fix XOVER command Eric Wong
2015-09-24 3:37 ` [PATCH 2/7] nntpd: additional daemonization options Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
2015-09-24 3:37 ` [PATCH 4/7] nntpd: move busy check to NNTP package Eric Wong
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
We don't want clients hogging server resources and preventing
us from shutting down, so give them 30s to finish whatever
request they're getting.
---
public-inbox-nntpd | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/public-inbox-nntpd b/public-inbox-nntpd
index defb16b..4c15608 100644
--- a/public-inbox-nntpd
+++ b/public-inbox-nntpd
@@ -195,10 +195,14 @@ sub worker_quit {
$_->close for @listeners;
@listeners = ();
+ # give slow clients 30s to finish reading/writing whatever
+ Danga::Socket->AddTimer(30, sub { exit });
+
# drop idle connections and try to quit gracefully
Danga::Socket->SetPostLoopCallback(sub {
my ($dmap, undef) = @_;
my $n = 0;
+
foreach my $s (values %$dmap) {
next unless ref($s) eq 'PublicInbox::NNTP';
if ($s->{write_buf_size} || $s->{rbuf}) {
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 4/7] nntpd: move busy check to NNTP package
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
` (2 preceding siblings ...)
2015-09-24 3:37 ` [PATCH 3/7] nntpd: hard quit after 30s Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
2015-09-24 3:37 ` [PATCH 5/7] nntp: avoid infinite loop on partial read Eric Wong
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
This is better encapsulated and hopefully more readable.
While we're at it, check for being inside a long response, too.
---
lib/PublicInbox/NNTP.pm | 5 +++++
public-inbox-nntpd | 2 +-
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 0d0de97..52d6a64 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -919,4 +919,9 @@ sub watch_read {
$rv;
}
+sub busy () {
+ my ($self) = @_;
+ ($self->{rbuf} ne '' || $self->{long_res} || $self->{write_buf_size});
+}
+
1;
diff --git a/public-inbox-nntpd b/public-inbox-nntpd
index 4c15608..70bab46 100644
--- a/public-inbox-nntpd
+++ b/public-inbox-nntpd
@@ -205,7 +205,7 @@ sub worker_quit {
foreach my $s (values %$dmap) {
next unless ref($s) eq 'PublicInbox::NNTP';
- if ($s->{write_buf_size} || $s->{rbuf}) {
+ if ($s->busy) {
++$n;
} else {
$s->close;
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 5/7] nntp: avoid infinite loop on partial read
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
` (3 preceding siblings ...)
2015-09-24 3:37 ` [PATCH 4/7] nntpd: move busy check to NNTP package Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
2015-09-24 3:37 ` [PATCH 6/7] nntpd: better encapsulation for shutdown Eric Wong
2015-09-24 3:37 ` [PATCH 7/7] nntpd: support SIGUSR2 in single-process mode, too Eric Wong
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
Oops :x
---
lib/PublicInbox/NNTP.pm | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 52d6a64..6c661a1 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -879,22 +879,18 @@ sub event_write {
sub event_read {
my ($self) = @_;
use constant LINE_MAX => 512; # RFC 977 section 2.3
- my $line;
my $r = 1;
-again:
+
+ my $buf = $self->read(LINE_MAX) or return $self->close;
+ $self->{rbuf} .= $$buf;
while ($r > 0 && $self->{rbuf} =~ s/\A\s*([^\r\n]+)\r?\n//) {
- $line = $1;
+ my $line = $1;
my $t0 = now();
$r = eval { $self->process_line($line) };
my $d = $self->{long_res} ?
' deferred['.fileno($self->{sock}).']' : '';
out($self, "$line - %0.6f$d", now() - $t0);
}
- unless (defined $line) {
- my $buf = $self->read(LINE_MAX) or return $self->close;
- $self->{rbuf} .= $$buf;
- goto again;
- }
return $self->close if $r < 0;
my $len = length($self->{rbuf});
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 6/7] nntpd: better encapsulation for shutdown
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
` (4 preceding siblings ...)
2015-09-24 3:37 ` [PATCH 5/7] nntp: avoid infinite loop on partial read Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
2015-09-24 3:37 ` [PATCH 7/7] nntpd: support SIGUSR2 in single-process mode, too Eric Wong
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
We can use the UNIVERSAL::can to better encapsulate the shutdown
process, in case we need to implement a gopher or HTTP daemon.
---
public-inbox-nntpd | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/public-inbox-nntpd b/public-inbox-nntpd
index 70bab46..9fb69ce 100644
--- a/public-inbox-nntpd
+++ b/public-inbox-nntpd
@@ -204,8 +204,7 @@ sub worker_quit {
my $n = 0;
foreach my $s (values %$dmap) {
- next unless ref($s) eq 'PublicInbox::NNTP';
- if ($s->busy) {
+ if ($s->can('busy') && $s->busy) {
++$n;
} else {
$s->close;
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH 7/7] nntpd: support SIGUSR2 in single-process mode, too
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
` (5 preceding siblings ...)
2015-09-24 3:37 ` [PATCH 6/7] nntpd: better encapsulation for shutdown Eric Wong
@ 2015-09-24 3:37 ` Eric Wong
6 siblings, 0 replies; 8+ messages in thread
From: Eric Wong @ 2015-09-24 3:37 UTC (permalink / raw)
To: meta
We may support SIGUSR2 in single-process mode as long
as permissions aren't wonky.
---
public-inbox-nntpd | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/public-inbox-nntpd b/public-inbox-nntpd
index 9fb69ce..67fc90b 100644
--- a/public-inbox-nntpd
+++ b/public-inbox-nntpd
@@ -99,6 +99,9 @@ if (defined $pid_file || defined $group || defined $user || $daemonize) {
if (defined $pid_file) {
my $unlink_pid = $$;
Net::Server::Daemonize::create_pid_file($pid_file);
+ if ($uid and !chown($uid, $gid, $pid_file)) {
+ warn "could not chown $pid_file: $!\n";
+ }
END { unlink_pid_file_safe_ish($unlink_pid, $pid_file) };
}
}
@@ -175,14 +178,15 @@ if ($worker_processes > 0) {
sysread($r, my $buf, 8);
}
} else {
+ $set_user->() if $set_user;
+ $SIG{USR2} = sub { worker_quit() if upgrade() };
worker:
# this calls epoll_create:
- @listeners = map { PublicInbox::Listener->new($_) } @listeners;
reopen_logs();
$SIG{QUIT} = $SIG{INT} = $SIG{TERM} = *worker_quit;
$SIG{USR1} = *reopen_logs;
$SIG{HUP} = $refresh;
- $_->watch_read(1) for @listeners;
+ PublicInbox::Listener->new($_) for @listeners;
Danga::Socket->EventLoop;
}
@@ -192,7 +196,6 @@ sub worker_quit {
# killing again terminates immediately:
exit unless @listeners;
- $_->close for @listeners;
@listeners = ();
# give slow clients 30s to finish reading/writing whatever
@@ -372,7 +375,9 @@ sub new ($$) {
listen($s, 1024);
IO::Handle::blocking($s, 0);
my $self = fields::new($class);
- $self->SUPER::new($s);
+ $self->SUPER::new($s); # calls epoll_create for the first socket
+ $self->watch_read(1);
+ $self
}
sub event_read {
--
EW
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2015-09-24 3:37 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-24 3:37 [PATCH 0/7] another round of NNTP updates Eric Wong
2015-09-24 3:37 ` [PATCH 1/7] nntp: fix XOVER command Eric Wong
2015-09-24 3:37 ` [PATCH 2/7] nntpd: additional daemonization options Eric Wong
2015-09-24 3:37 ` [PATCH 3/7] nntpd: hard quit after 30s Eric Wong
2015-09-24 3:37 ` [PATCH 4/7] nntpd: move busy check to NNTP package Eric Wong
2015-09-24 3:37 ` [PATCH 5/7] nntp: avoid infinite loop on partial read Eric Wong
2015-09-24 3:37 ` [PATCH 6/7] nntpd: better encapsulation for shutdown Eric Wong
2015-09-24 3:37 ` [PATCH 7/7] nntpd: support SIGUSR2 in single-process mode, too Eric Wong
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).