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
20 my $self = bless {}, __PACKAGE__;
21 # ensure the child process has the same @INC we do:
22 my $env = { PERL5LIB => join(':', @INC) };
24 pipe($out_r, $out_w) or die "pipe failed: $!";
27 my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
28 @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
29 fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
32 $self->{inflight} = [];
33 $self->SUPER::new($out_w, EPOLLONESHOT); # detect errors once
38 $self->close; # PublicInbox::DS::close
39 PublicInbox::Git::fail($self, @_);
42 sub cat_async ($$$;$) {
43 my ($self, $req, $cb, $arg) = @_;
44 my $inflight = $self->{inflight};
46 # {wbuf} is rare, I hope:
47 cat_async_step($self, $inflight) if $self->{wbuf};
49 if (!$self->write(\"$req\n")) {
50 $self->fail("gcf2c write: $!") if !$self->{sock};
52 push @$inflight, $req, $cb, $arg;
55 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
56 sub alternates_changed {}
58 # this is the write-only end of a pipe, DS->EventLoop will call this
62 $self->close if !$self->{in}; # process died
70 # GitAsyncCat::event_step may reap us with WNOHANG, too
71 my $pid = delete $self->{pid} or return;
72 PublicInbox::DS->in_loop ? $self->close : delete($self->{sock});
77 *cat_async_step = \&PublicInbox::Git::cat_async_step;