1 # Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Limits the number of processes spawned
5 # This does not depend on Danga::Socket or any other external
6 # scheduling mechanism, you just need to call start and finish
8 package PublicInbox::Qspawn;
11 use PublicInbox::Spawn qw(popen_rd);
16 my ($class, $cmd, $env, $opt) = @_;
17 bless { args => [ $cmd, $env, $opt ] }, $class;
24 ($self->{rpipe}, $self->{pid}) = popen_rd(@{$self->{args}});
25 if (defined $self->{pid}) {
26 $self->{limiter}->{running}++;
30 $cb->($self->{rpipe});
34 my ($child_error) = @_; # typically $?
35 my $exitstatus = ($child_error >> 8) or return;
36 my $sig = $child_error & 127;
37 my $msg = "exit status=$exitstatus";
38 $msg .= " signal=$sig" if $sig;
44 my $limiter = $self->{limiter};
46 if (delete $self->{rpipe}) {
47 my $pid = delete $self->{pid};
48 $self->{err} = $pid == waitpid($pid, 0) ? child_err($?) :
49 "PID:$pid still running?";
50 $running = --$limiter->{running};
53 # limiter->{max} may change dynamically
54 if (($running || $limiter->{running}) < $limiter->{max}) {
55 if (my $next = shift @{$limiter->{run_queue}}) {
63 my ($self, $limiter, $cb) = @_;
64 $self->{limiter} = $limiter;
66 if ($limiter->{running} < $limiter->{max}) {
67 _do_spawn($self, $cb);
69 push @{$limiter->{run_queue}}, [ $self, $cb ];
73 sub _psgi_finish ($$) {
74 my ($self, $env) = @_;
75 my $err = $self->finish;
76 if ($err && !$env->{'qspawn.quiet'}) {
77 $err = join(' ', @{$self->{args}->[0]}).": $err\n";
78 $env->{'psgi.errors'}->print($err);
83 my ($self, $env, $limiter, $qx_cb) = @_;
84 my $qx = PublicInbox::Qspawn::Qx->new;
86 _psgi_finish($self, $env);
87 eval { $qx_cb->($qx) };
91 my $async = $env->{'pi-httpd.async'};
93 my $r = sysread($rpipe, my $buf, 8192);
95 $async->async_pass($env->{'psgix.io'}, $qx, \$buf);
96 } elsif (defined $r) {
97 $r ? $qx->write($buf) : $end->();
99 return if $!{EAGAIN} || $!{EINTR}; # loop again
103 $limiter ||= $def_limiter ||= PublicInbox::Qspawn::Limiter->new(32);
104 $self->start($limiter, sub { # may run later, much later...
107 # PublicInbox::HTTPD::Async->new($rpipe, $cb, $end)
108 $async = $async->($rpipe, $cb, $end);
109 } else { # generic PSGI
115 # create a filter for "push"-based streaming PSGI writes used by HTTPD::Async
117 my ($fh, $filter) = @_;
118 Plack::Util::inline_object(
120 $fh->write($filter->(undef));
124 $fh->write($filter->($_[0]));
129 my ($self, $env, $limiter, $parse_hdr) = @_;
132 _psgi_finish($self, $env);
133 $fh->close if $fh; # async-only
138 my $r = sysread($rpipe, $buf, 1024, length($buf));
139 return if !defined($r) && ($!{EINTR} || $!{EAGAIN});
140 $parse_hdr->($r, \$buf);
142 my $res = delete $env->{'qspawn.response'};
143 my $async = $env->{'pi-httpd.async'};
145 my $r = $rd_hdr->() or return;
147 my $filter = delete $env->{'qspawn.filter'};
148 if (scalar(@$r) == 3) { # error
150 $async->close; # calls rpipe->close and $end
157 $fh = $res->($r); # scalar @$r == 2
158 $fh = filter_fh($fh, $filter) if $filter;
159 $async->async_pass($env->{'psgix.io'}, $fh, \$buf);
160 } else { # for synchronous PSGI servers
161 require PublicInbox::GetlineBody;
162 $r->[2] = PublicInbox::GetlineBody->new($rpipe, $end,
167 $limiter ||= $def_limiter ||= PublicInbox::Qspawn::Limiter->new(32);
168 my $start_cb = sub { # may run later, much later...
171 # PublicInbox::HTTPD::Async->new($rpipe, $cb, $end)
172 $async = $async->($rpipe, $cb, $end);
173 } else { # generic PSGI
174 $cb->() while $rd_hdr;
178 return $self->start($limiter, $start_cb) if $res;
182 $self->start($limiter, $start_cb);
186 package PublicInbox::Qspawn::Limiter;
191 my ($class, $max) = @_;
193 # 32 is same as the git-daemon connection limit
200 # captures everything into a buffer and executes a callback when done
201 package PublicInbox::Qspawn::Qx;
211 # called by PublicInbox::HTTPD::Async ($fh->write)