1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # connects public-inbox processes to PublicInbox::Gcf2::loop()
5 package PublicInbox::Gcf2Client;
7 use parent qw(PublicInbox::DS);
9 use PublicInbox::Spawn qw(popen_rd);
11 use PublicInbox::Syscall qw(EPOLLONESHOT);
12 use PublicInbox::DS qw(dwaitpid);
14 # async_cat => GitAsyncCat ref (read-only pipe)
15 # sock => writable pipe to Gcf2::loop
16 # in => pipe we read from
17 # pid => PID of Gcf2::loop process
18 # owner_pid => process which spawned {pid}
21 my $self = bless {}, __PACKAGE__;
22 # ensure the child process has the same @INC we do:
23 my $env = { PERL5LIB => join(':', @INC) };
25 pipe($out_r, $out_w) or die "pipe failed: $!";
28 my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
29 $self->{owner_pid} = $$;
30 @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
31 fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
34 $self->{inflight} = [];
35 $self->SUPER::new($out_w, EPOLLONESHOT); # detect errors once
40 $self->close; # PublicInbox::DS::close
41 PublicInbox::Git::fail($self, @_);
44 sub cat_async ($$$;$) {
45 my ($self, $req, $cb, $arg) = @_;
46 my $inflight = $self->{inflight};
48 # {wbuf} is rare, I hope:
49 cat_async_step($self, $inflight) if $self->{wbuf};
51 if (!$self->write(\"$req\n")) {
52 $self->fail("gcf2c write: $!") if !$self->{sock};
54 push @$inflight, $req, $cb, $arg;
57 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
58 sub alternates_changed {}
60 # this is the write-only end of a pipe, DS->EventLoop will call this
64 $self->close if !$self->{in}; # process died
72 # GitAsyncCat::event_step may reap us with WNOHANG, too
73 my $pid = delete $self->{pid} or return;
74 if ($$ == $self->{owner_pid}) {
75 PublicInbox::DS->in_loop ? $self->close : delete($self->{sock});
81 *cat_async_step = \&PublicInbox::Git::cat_async_step;