]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Qspawn.pm
update copyrights for 2018
[public-inbox.git] / lib / PublicInbox / Qspawn.pm
index cc9c340da26530993267f8cd00dc30da0d83714f..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};
 }
@@ -47,7 +52,7 @@ sub start {
        my ($self, $limiter, $cb) = @_;
        $self->{limiter} = $limiter;
 
-       if ($limiter->{running} < $limiter->{limit}) {
+       if ($limiter->{running} < $limiter->{max}) {
                _do_spawn($self, $cb);
        } else {
                push @{$limiter->{run_queue}}, [ $self, $cb ];
@@ -59,9 +64,10 @@ use strict;
 use warnings;
 
 sub new {
-       my ($class, $limit) = @_;
+       my ($class, $max) = @_;
        bless {
-               limit => $limit || 1,
+               # 32 is same as the git-daemon connection limit
+               max => $max || 32,
                running => 0,
                run_queue => [],
        }, $class;