From 93a7b219d58aad86438cbf3cbf4318a889831ac1 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 5 May 2022 10:52:15 +0000 Subject: public-inbox-netd: a multi-protocol superserver Since we'll be adding POP3 support as our 4th network protocol; asking admins to run yet another daemon on top of existing -httpd, -nntpd, -imapd is a maintenance burden and a waste of memory. The goal of public-inbox-netd is to be able to replace all existing read-only daemons with a single process to save memory and reduce administrative overhead; hopefully encouraging more users to self-host their own mirrors. It's barely-tested at the moment. Eventually, multiple PI_CONFIG and HOME directories will be supported, as are per-listener .psgi config files. --- lib/PublicInbox/HTTPD.pm | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'lib/PublicInbox/HTTPD.pm') diff --git a/lib/PublicInbox/HTTPD.pm b/lib/PublicInbox/HTTPD.pm index 02f424c6..715e4538 100644 --- a/lib/PublicInbox/HTTPD.pm +++ b/lib/PublicInbox/HTTPD.pm @@ -1,14 +1,15 @@ -# Copyright (C) 2016-2021 all contributors +# Copyright (C) all contributors # License: AGPL-3.0+ # wraps a listen socket for HTTP and links it to the PSGI app in # public-inbox-httpd package PublicInbox::HTTPD; +use v5.10.1; use strict; -use warnings; -use Plack::Util; +use Plack::Util (); +use Plack::Builder; +use PublicInbox::HTTP; use PublicInbox::HTTPD::Async; -use PublicInbox::Daemon; sub pi_httpd_async { PublicInbox::HTTPD::Async->new(@_) } @@ -41,10 +42,41 @@ sub new { # detect when to use async paths for slow blobs 'pi-httpd.async' => \&pi_httpd_async ); - bless { - app => $app, - env => \%env - }, $class; + bless { app => $app, env => \%env }, $class; +} + +my %httpds; # per-listen-FD mapping for HTTPD->{env}->{SERVER_} +my $default_app; # ugh... + +sub refresh { + if (@main::ARGV) { + eval { $default_app = Plack::Util::load_psgi(@ARGV) }; + if ($@) { + die $@, +"$0 runs in /, command-line paths must be absolute\n"; + } + } else { + require PublicInbox::WWW; + my $www = PublicInbox::WWW->new; + $www->preload; + $default_app = builder { + eval { enable 'ReverseProxy' }; + $@ and warn <call(@_) }; + }; + } + %httpds = (); # invalidate cache +} + +sub post_accept { # Listener->{post_accept} + my ($client, $addr, $srv) = @_; # $_[3] - tls_wrap (unused) + my $httpd = $httpds{fileno($srv)} //= + __PACKAGE__->new($srv, $default_app, $client); + PublicInbox::HTTP->new($client, $addr, $httpd), } 1; -- cgit v1.2.3-24-ge0c7