]> Sergey Matveev's repositories - public-inbox.git/commitdiff
qspawn: prepare to support runtime reloading of Limiter
authorEric Wong <e@80x24.org>
Sat, 7 Jan 2017 01:44:44 +0000 (01:44 +0000)
committerEric Wong <e@80x24.org>
Sat, 7 Jan 2017 01:45:39 +0000 (01:45 +0000)
We may allow the {max} value of a limiter to be changed
in the future, so lets start accounting for it before we
spawn followup processes.

lib/PublicInbox/Qspawn.pm

index 697c55a1b4d5ec8ad579a03a0b8fdea6e45136a2..4950da25f0012aecac5a34e5256926e858ada1df 100644 (file)
@@ -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};
 }