From c38111d6f3877cf31d28b0a0339d063df0fa58f6 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 1 Aug 2022 21:24:43 +0000 Subject: daemon: support per-listener env, .psgi, out, err This allows memory savings by allowing multiple, completely unrelated-PSGI apps to run within the same process as IMAP, NNTP, and POP3. --- t/alt.psgi | 17 +++++++++++++++++ t/httpd-corner.psgi | 8 ++++++-- t/httpd-corner.t | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 58 insertions(+), 6 deletions(-) create mode 100644 t/alt.psgi (limited to 't') diff --git a/t/alt.psgi b/t/alt.psgi new file mode 100644 index 00000000..c7f42979 --- /dev/null +++ b/t/alt.psgi @@ -0,0 +1,17 @@ +# Copyright (C) all contributors +# License: AGPL-3.0+ +use v5.12; +use warnings; +use Plack::Builder; +my $pi_config = $ENV{PI_CONFIG} // 'unset'; # capture ASAP +my $app = sub { + my ($env) = @_; + $env->{'psgi.errors'}->print("ALT\n"); + [ 200, ['Content-Type', 'text/plain'], [ $pi_config ] ] +}; + +builder { + enable 'ContentLength'; + enable 'Head'; + $app; +} diff --git a/t/httpd-corner.psgi b/t/httpd-corner.psgi index e9a3a6b7..10cf8350 100644 --- a/t/httpd-corner.psgi +++ b/t/httpd-corner.psgi @@ -1,11 +1,12 @@ -# Copyright (C) 2016-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # corner case tests for the generic PSGI server # Usage: plackup [OPTIONS] /path/to/this/file -use strict; +use v5.12; use warnings; use Plack::Builder; require Digest::SHA; +my $pi_config = $ENV{PI_CONFIG} // 'unset'; # capture ASAP my $app = sub { my ($env) = @_; my $path = $env->{PATH_INFO}; @@ -114,6 +115,9 @@ my $app = sub { } elsif ($path eq '/url_scheme') { $code = 200; push @$body, $env->{'psgi.url_scheme'} + } elsif ($path eq '/PI_CONFIG') { + $code = 200; + push @$body, $pi_config; # show value at ->refresh_groups } [ $code, $h, $body ] }; diff --git a/t/httpd-corner.t b/t/httpd-corner.t index 0a613a9e..973cc55d 100644 --- a/t/httpd-corner.t +++ b/t/httpd-corner.t @@ -1,4 +1,5 @@ -# Copyright (C) 2016-2021 all contributors +#!perl -w +# Copyright (C) all contributors # License: AGPL-3.0+ # note: our HTTP server should be standalone and capable of running # generic PSGI/Plack apps. @@ -19,7 +20,7 @@ ok(defined mkfifo($fifo, 0777), 'created FIFO'); my $err = "$tmpdir/stderr.log"; my $out = "$tmpdir/stdout.log"; my $psgi = "./t/httpd-corner.psgi"; -my $sock = tcp_server() or die; +my $sock = tcp_server(); my @zmods = qw(PublicInbox::GzipFilter IO::Uncompress::Gunzip); # Make sure we don't clobber socket options set by systemd or similar @@ -53,14 +54,40 @@ sub unix_server ($) { my $upath = "$tmpdir/s"; my $unix = unix_server($upath); +my $alt = tcp_server(); my $td; my $spawn_httpd = sub { my (@args) = @_; - my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ]; - $td = start_script($cmd, undef, { 3 => $sock, 4 => $unix }); + my $x = tcp_host_port($alt); + my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi, + '-l', "http://$x/?psgi=t/alt.psgi,env.PI_CONFIG=/path/to/alt". + ",err=$tmpdir/alt.err" ]; + my $env = { PI_CONFIG => '/dev/null' }; + $td = start_script($cmd, $env, { 3 => $sock, 4 => $unix, 5 => $alt }); }; $spawn_httpd->(); +{ + my $conn = conn_for($alt, 'alt PSGI path'); + $conn->write("GET / HTTP/1.0\r\n\r\n"); + $conn->read(my $buf, 4096); + like($buf, qr!^/path/to/alt\z!sm, + 'alt.psgi loaded on alt socket with correct env'); + + $conn = conn_for($sock, 'default PSGI path'); + $conn->write("GET /PI_CONFIG HTTP/1.0\r\n\r\n"); + $conn->read($buf, 4096); + like($buf, qr!^/dev/null\z!sm, + 'default PSGI on original socket'); + my $log = capture("$tmpdir/alt.err"); + ok(grep(/ALT/, @$log), 'alt psgi.errors written to'); + $log = capture($err); + ok(!grep(/ALT/, @$log), 'STDERR not written to'); + is(unlink($err, "$tmpdir/alt.err"), 2, 'unlinked stderr and alt.err'); + + $td->kill('USR1'); # trigger reopen_logs +} + if ('test worker death') { my $conn = conn_for($sock, 'killed worker'); $conn->write("GET /pid HTTP/1.1\r\nHost:example.com\r\n\r\n"); @@ -82,6 +109,10 @@ if ('test worker death') { like($body, qr/\A[0-9]+\z/, '/pid response'); isnt($body, $pid, 'respawned worker'); } +{ # check on prior USR1 signal + ok(-e $err, 'stderr recreated after USR1'); + ok(-e "$tmpdir/alt.err", 'alt.err recreated after USR1'); +} { my $conn = conn_for($sock, 'Header spaces bogus'); $conn->write("GET /empty HTTP/1.1\r\nSpaced-Out : 3\r\n\r\n"); -- cgit v1.2.3-24-ge0c7