From: Eric Wong Date: Wed, 5 Mar 2025 00:45:36 +0000 (+0000) Subject: listener: don't set listen backlog on inherited sockets X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7b19a3cd287c696815603e8089897c21f486f7df;p=thirdparty%2Fpublic-inbox.git listener: don't set listen backlog on inherited sockets By using the listen(2) backlog as-is when inheriting (from systemd or similar), we can give the sysadmin more control on controlling overload on a per-listener basis. For systemd users, this means the `Backlog=' parameter in systemd.socket(5) can be respected and configured to give certain sockets a smaller backlog (perhaps combined with with per-listener `multi-accept' parameter on sockets with the standard (huge) backlog). For sockets we create, continue to use INT_MAX and let the kernel clamp it to whatever system-wide limit there is (e.g. `net.core.somaxconn' sysctl on Linux). --- diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index 5d93f81fe..8fe93acde 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -264,7 +264,7 @@ EOF die $@ if $@; %o = (LocalAddr => $l, ReuseAddr => 1, Proto => 'tcp'); } - $o{Listen} = 1024; + $o{Listen} = 2**31 - 1; # kernel will clamp my $prev = umask 0000; my $s = eval { $sock_pkg->new(%o) } or warn "error binding $l: $! ($@)\n"; diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 94bac6885..0a779c4fb 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -9,7 +9,7 @@ package PublicInbox::LEI; use v5.12; use parent qw(PublicInbox::DS PublicInbox::LeiExternal PublicInbox::LeiQuery); -use autodie qw(bind chdir open pipe socket socketpair syswrite unlink); +use autodie qw(bind chdir listen open pipe socket socketpair syswrite unlink); use Getopt::Long (); use Socket qw(AF_UNIX SOCK_SEQPACKET pack_sockaddr_un); use Errno qw(EPIPE EAGAIN ECONNREFUSED ENOENT ECONNRESET EINTR); @@ -1371,6 +1371,7 @@ sub lazy_start { local (%PATH2CFG, $MDIR2CFGPATH); local $daemon_pid = $$; $listener->blocking(0); + listen $listener, 2**31 - 1; # kernel will clamp my $exit_code; my $pil = PublicInbox::Listener->new($listener, \&accept_dispatch); local $quit = do { diff --git a/lib/PublicInbox/Listener.pm b/lib/PublicInbox/Listener.pm index c83901b2a..624756000 100644 --- a/lib/PublicInbox/Listener.pm +++ b/lib/PublicInbox/Listener.pm @@ -21,7 +21,6 @@ sub new { my ($class, $s, $cb, $multi_accept) = @_; setsockopt($s, SOL_SOCKET, SO_KEEPALIVE, 1); setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1); # ignore errors on non-TCP - listen($s, 2**31 - 1); # kernel will clamp my $self = bless { post_accept => $cb }, $class; $self->{multi_accept} = $multi_accept //= $MULTI_ACCEPT; $self->SUPER::new($s, EPOLLIN|EPOLLEXCLUSIVE);