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};
}
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 ];
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;