]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
limiter: split out from qspawn
authorEric Wong <e@80x24.org>
Wed, 25 Oct 2023 00:29:24 +0000 (00:29 +0000)
committerEric Wong <e@80x24.org>
Wed, 25 Oct 2023 07:28:30 +0000 (07:28 +0000)
It's slightly better organized this way, especially since
`publicinboxLimiter' has its own user-facing config section
and knobs.  I may use it in LeiMirror and CodeSearchIdx for
process management.

MANIFEST
lib/PublicInbox/Config.pm
lib/PublicInbox/GitHTTPBackend.pm
lib/PublicInbox/Inbox.pm
lib/PublicInbox/Limiter.pm [new file with mode: 0644]
lib/PublicInbox/MailDiff.pm
lib/PublicInbox/Qspawn.pm
t/qspawn.t

index 791d91a7422c23b7744025ec5d0543b51fe65d2d..dcce801cecf9a11aea7b3d1c67a878c321eecb42 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -287,6 +287,7 @@ lib/PublicInbox/LeiUp.pm
 lib/PublicInbox/LeiViewText.pm
 lib/PublicInbox/LeiWatch.pm
 lib/PublicInbox/LeiXSearch.pm
+lib/PublicInbox/Limiter.pm
 lib/PublicInbox/Linkify.pm
 lib/PublicInbox/Listener.pm
 lib/PublicInbox/Lock.pm
index 15e0872e3ca46685f9a6945e6e9eee957f71418b..d156b2d33f129d5b0266de9b09374a50e00454df 100644 (file)
@@ -124,9 +124,9 @@ sub lookup_newsgroup {
 sub limiter {
        my ($self, $name) = @_;
        $self->{-limiters}->{$name} //= do {
-               require PublicInbox::Qspawn;
+               require PublicInbox::Limiter;
                my $max = $self->{"publicinboxlimiter.$name.max"} || 1;
-               my $limiter = PublicInbox::Qspawn::Limiter->new($max);
+               my $limiter = PublicInbox::Limiter->new($max);
                $limiter->setup_rlimit($name, $self);
                $limiter;
        };
index 744324294917df02bd07166a8d4e9423714b871d..d69f5f8bb827bf3829890b2231a5d733dd06b8df 100644 (file)
@@ -9,13 +9,14 @@ use v5.10.1;
 use Fcntl qw(:seek);
 use IO::Handle; # ->flush
 use HTTP::Date qw(time2str);
+use PublicInbox::Limiter;
 use PublicInbox::Qspawn;
 use PublicInbox::Tmpfile;
 use PublicInbox::WwwStatic qw(r @NO_CACHE);
 use Carp ();
 
 # 32 is same as the git-daemon connection limit
-my $default_limiter = PublicInbox::Qspawn::Limiter->new(32);
+my $default_limiter = PublicInbox::Limiter->new(32);
 
 # n.b. serving "description" and "cloneurl" should be innocuous enough to
 # not cause problems.  serving "config" might...
index 9afbb4783a8015b9ff3e9ed605f7aceb6aacb765..3dad700438ad9dbaa5b1189296bd5ef796b64fd2 100644 (file)
@@ -55,8 +55,8 @@ sub _set_limiter ($$$) {
                my $val = $self->{$mkey} or return;
                my $lim;
                if ($val =~ /\A[0-9]+\z/) {
-                       require PublicInbox::Qspawn;
-                       $lim = PublicInbox::Qspawn::Limiter->new($val);
+                       require PublicInbox::Limiter;
+                       $lim = PublicInbox::Limiter->new($val);
                } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
                        $lim = $pi_cfg->limiter($val);
                        warn "$mkey limiter=$val not found\n" if !$lim;
diff --git a/lib/PublicInbox/Limiter.pm b/lib/PublicInbox/Limiter.pm
new file mode 100644 (file)
index 0000000..48a2b6a
--- /dev/null
@@ -0,0 +1,47 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+package PublicInbox::Limiter;
+use v5.12;
+use PublicInbox::Spawn;
+
+sub new {
+       my ($class, $max) = @_;
+       bless {
+               # 32 is same as the git-daemon connection limit
+               max => $max || 32,
+               running => 0,
+               run_queue => [],
+               # RLIMIT_CPU => undef,
+               # RLIMIT_DATA => undef,
+               # RLIMIT_CORE => undef,
+       }, $class;
+}
+
+sub setup_rlimit {
+       my ($self, $name, $cfg) = @_;
+       for my $rlim (@PublicInbox::Spawn::RLIMITS) {
+               my $k = lc($rlim);
+               $k =~ tr/_//d;
+               $k = "publicinboxlimiter.$name.$k";
+               my $v = $cfg->{$k} // next;
+               my @rlimit = split(/\s*,\s*/, $v);
+               if (scalar(@rlimit) == 1) {
+                       push @rlimit, $rlimit[0];
+               } elsif (scalar(@rlimit) != 2) {
+                       warn "could not parse $k: $v\n";
+               }
+               eval { require BSD::Resource };
+               if ($@) {
+                       warn "BSD::Resource missing for $rlim";
+                       next;
+               }
+               for my $i (0..$#rlimit) {
+                       next if $rlimit[$i] ne 'INFINITY';
+                       $rlimit[$i] = BSD::Resource::RLIM_INFINITY();
+               }
+               $self->{$rlim} = \@rlimit;
+       }
+}
+
+1;
index 994c7851e81e3405dfbd0c8e8f66881b0f72a513..c3ce93657f2c76bafe11ac84701e9692a1f3d986 100644 (file)
@@ -8,6 +8,7 @@ use PublicInbox::MsgIter qw(msg_part_text);
 use PublicInbox::ViewDiff qw(flush_diff);
 use PublicInbox::GitAsyncCat;
 use PublicInbox::ContentDigestDbg;
+use PublicInbox::Qspawn;
 
 sub write_part { # Eml->each_part callback
        my ($ary, $self) = @_;
index 0e52617c317881787a5973762e101dfdfb4f8a08..a4d78e495e5f4014116c04f722d7d1c3d16868a6 100644 (file)
@@ -29,6 +29,7 @@ use v5.12;
 use PublicInbox::Spawn qw(popen_rd);
 use PublicInbox::GzipFilter;
 use Scalar::Util qw(blessed);
+use PublicInbox::Limiter;
 
 # n.b.: we get EAGAIN with public-inbox-httpd, and EINTR on other PSGI servers
 use Errno qw(EAGAIN EINTR);
@@ -183,7 +184,7 @@ sub psgi_qx {
        $self->{qx_arg} = $qx_arg;
        $self->{qx_fh} = $qx_fh;
        $self->{qx_buf} = \$qx_buf;
-       $limiter ||= $def_limiter ||= PublicInbox::Qspawn::Limiter->new(32);
+       $limiter ||= $def_limiter ||= PublicInbox::Limiter->new(32);
        start($self, $limiter, \&psgi_qx_start);
 }
 
@@ -317,7 +318,7 @@ sub psgi_return {
        $self->{psgi_env} = $env;
        $self->{hdr_buf} = \(my $hdr_buf = '');
        $self->{parse_hdr} = [ $parse_hdr, $hdr_arg ];
-       $limiter ||= $def_limiter ||= PublicInbox::Qspawn::Limiter->new(32);
+       $limiter ||= $def_limiter ||= PublicInbox::Limiter->new(32);
 
        # the caller already captured the PSGI write callback from
        # the PSGI server, so we can call ->start, here:
@@ -334,46 +335,4 @@ sub psgi_return {
        }
 }
 
-package PublicInbox::Qspawn::Limiter;
-use v5.12;
-
-sub new {
-       my ($class, $max) = @_;
-       bless {
-               # 32 is same as the git-daemon connection limit
-               max => $max || 32,
-               running => 0,
-               run_queue => [],
-               # RLIMIT_CPU => undef,
-               # RLIMIT_DATA => undef,
-               # RLIMIT_CORE => undef,
-       }, $class;
-}
-
-sub setup_rlimit {
-       my ($self, $name, $cfg) = @_;
-       foreach my $rlim (@PublicInbox::Spawn::RLIMITS) {
-               my $k = lc($rlim);
-               $k =~ tr/_//d;
-               $k = "publicinboxlimiter.$name.$k";
-               defined(my $v = $cfg->{$k}) or next;
-               my @rlimit = split(/\s*,\s*/, $v);
-               if (scalar(@rlimit) == 1) {
-                       push @rlimit, $rlimit[0];
-               } elsif (scalar(@rlimit) != 2) {
-                       warn "could not parse $k: $v\n";
-               }
-               eval { require BSD::Resource };
-               if ($@) {
-                       warn "BSD::Resource missing for $rlim";
-                       next;
-               }
-               foreach my $i (0..$#rlimit) {
-                       next if $rlimit[$i] ne 'INFINITY';
-                       $rlimit[$i] = BSD::Resource::RLIM_INFINITY();
-               }
-               $self->{$rlim} = \@rlimit;
-       }
-}
-
 1;
index 224e20db4b1717cd7078250a1de41a12e4bf6918..507f86a515888027e2013d905a29596bab71d57d 100644 (file)
@@ -3,6 +3,7 @@
 use v5.12;
 use Test::More;
 use_ok 'PublicInbox::Qspawn';
+use_ok 'PublicInbox::Limiter';
 
 {
        my $cmd = [qw(sh -c), 'echo >&2 err; echo out'];
@@ -23,7 +24,7 @@ sub finish_err ($) {
        $qsp->{qsp_err} && ${$qsp->{qsp_err}};
 }
 
-my $limiter = PublicInbox::Qspawn::Limiter->new(1);
+my $limiter = PublicInbox::Limiter->new(1);
 {
        my $x = PublicInbox::Qspawn->new([qw(true)]);
        $x->{qsp_err} = \(my $err = '');