]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Qspawn.pm
bundle Danga::Socket and Sys::Syscall
[public-inbox.git] / lib / PublicInbox / Qspawn.pm
index 509a441246e7407932a4db62d252e8d46caa392f..9aede103521b47d3684032ca29ac6ace7f0a1747 100644 (file)
@@ -12,9 +12,9 @@
 # operate in.  This can be useful to ensure smaller inboxes can
 # be cloned while cloning of large inboxes is maxed out.
 #
-# This does not depend on Danga::Socket or any other external
+# This does not depend on PublicInbox::DS or any other external
 # scheduling mechanism, you just need to call start() and finish()
-# appropriately. However, public-inbox-httpd (which uses Danga::Socket)
+# appropriately. However, public-inbox-httpd (which uses PublicInbox::DS)
 # will be able to schedule this based on readability of stdout from
 # the spawned process.  See GitHTTPBackend.pm and SolverGit.pm for
 # usage examples.  It does not depend on any form of threading.
@@ -43,10 +43,18 @@ sub new ($$$;) {
 sub _do_spawn {
        my ($self, $cb) = @_;
        my $err;
+       my ($cmd, $env, $opts) = @{$self->{args}};
+       my %opts = %{$opts || {}};
+       my $limiter = $self->{limiter};
+       foreach my $k (PublicInbox::Spawn::RLIMITS()) {
+               if (defined(my $rlimit = $limiter->{$k})) {
+                       $opts{$k} = $rlimit;
+               }
+       }
 
-       ($self->{rpipe}, $self->{pid}) = popen_rd(@{$self->{args}});
+       ($self->{rpipe}, $self->{pid}) = popen_rd($cmd, $env, \%opts);
        if (defined $self->{pid}) {
-               $self->{limiter}->{running}++;
+               $limiter->{running}++;
        } else {
                $self->{err} = $!;
        }
@@ -251,9 +259,38 @@ sub new {
                max => $max || 32,
                running => 0,
                run_queue => [],
+               # RLIMIT_CPU => undef,
+               # RLIMIT_DATA => undef,
+               # RLIMIT_CORE => undef,
        }, $class;
 }
 
+sub setup_rlimit {
+       my ($self, $name, $config) = @_;
+       foreach my $rlim (PublicInbox::Spawn::RLIMITS()) {
+               my $k = lc($rlim);
+               $k =~ tr/_//d;
+               $k = "publicinboxlimiter.$name.$k";
+               defined(my $v = $config->{$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;
+       }
+}
+
 # captures everything into a buffer and executes a callback when done
 package PublicInbox::Qspawn::Qx;
 use strict;