1 # Copyright (C) 2020-2021 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::Gcf2; # fails if Inline::C or libgit2-dev isn't available
10 use PublicInbox::Spawn qw(spawn);
11 use Socket qw(AF_UNIX SOCK_STREAM);
12 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
14 # sock => socket to Gcf2::loop
15 # The rest of these fields are compatible with what PublicInbox::Git
17 # pid => PID of Gcf2::loop process
18 # pid.owner => process which spawned {pid}
19 # in => same as {sock}, for compatibility with PublicInbox::Git
20 # inflight => array (see PublicInbox::Git)
21 # cat_rbuf => scalarref, may be non-existent or empty
24 my $self = bless {}, __PACKAGE__;
25 # ensure the child process has the same @INC we do:
26 my $env = { PERL5LIB => join(':', @INC) };
28 socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or die "socketpair $!";
30 $rdr->{0} = $rdr->{1} = $s2;
31 my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop]];
32 $self->{'pid.owner'} = $$;
33 $self->{pid} = spawn($cmd, $env, $rdr);
35 $self->{inflight} = [];
37 $self->SUPER::new($s1, EPOLLIN|EPOLLET);
42 $self->close; # PublicInbox::DS::close
43 PublicInbox::Git::fail($self, @_);
46 sub gcf2_async ($$$;$) {
47 my ($self, $req, $cb, $arg) = @_;
48 my $inflight = $self->{inflight} or return $self->close;
50 # {wbuf} is rare, I hope:
51 cat_async_step($self, $inflight) if $self->{wbuf};
53 $self->fail("gcf2c write: $!") if !$self->write($req) && !$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 # DS::event_loop will call this
64 $self->close if !$self->{in} || !$self->{sock}; # process died
65 my $inflight = $self->{inflight};
66 if ($inflight && @$inflight) {
67 cat_async_step($self, $inflight);
68 return $self->close unless $self->{in}; # process died
70 # ok, more to do, requeue for fairness
71 $self->requeue if @$inflight || exists($self->{cat_rbuf});
77 delete $self->{sock}; # if outside event_loop
78 PublicInbox::Git::DESTROY($self);
83 *cat_async_step = \&PublicInbox::Git::cat_async_step;