]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Qspawn.pm
wwwstream: flesh out clone instructions for v2
[public-inbox.git] / lib / PublicInbox / Qspawn.pm
index 697c55a1b4d5ec8ad579a03a0b8fdea6e45136a2..3500f8a4a86fcd35fe21e6b0fcded3ceeea7f87d 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 
 # Limits the number of processes spawned
@@ -31,14 +31,19 @@ sub _do_spawn {
 sub finish ($) {
        my ($self) = @_;
        my $limiter = $self->{limiter};
+       my $running;
        if (delete $self->{rpipe}) {
                my $pid = delete $self->{pid};
                $self->{err} = $pid == waitpid($pid, 0) ? $? :
                                "PID:$pid still running?";
-               $limiter->{running}--;
+               $running = --$limiter->{running};
        }
-       if (my $next = shift @{$limiter->{run_queue}}) {
-               _do_spawn(@$next);
+
+       # limiter->{max} may change dynamically
+       if (($running || $limiter->{running}) < $limiter->{max}) {
+               if (my $next = shift @{$limiter->{run_queue}}) {
+                       _do_spawn(@$next);
+               }
        }
        $self->{err};
 }