1 # Copyright (C) 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);
13 use PublicInbox::DS qw(awaitpid);
15 # sock => socket to Gcf2::loop
16 # The rest of these fields are compatible with what PublicInbox::Git
18 # pid => PID of Gcf2::loop process
19 # pid.owner => process which spawned {pid}
20 # in => same as {sock}, for compatibility with PublicInbox::Git
21 # inflight => array (see PublicInbox::Git)
22 # rbuf => scalarref, may be non-existent or empty
25 my $self = bless {}, __PACKAGE__;
26 # ensure the child process has the same @INC we do:
27 my $env = { PERL5LIB => join(':', @INC) };
29 socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or die "socketpair $!";
31 $rdr->{0} = $rdr->{1} = $s2;
32 my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop]];
33 $self->{'pid.owner'} = $$;
34 awaitpid($self->{pid} = spawn($cmd, $env, $rdr), undef);
36 $self->{inflight} = [];
38 $self->SUPER::new($s1, EPOLLIN|EPOLLET);
43 $self->close; # PublicInbox::DS::close
44 PublicInbox::Git::fail($self, @_);
47 sub gcf2_async ($$$;$) {
48 my ($self, $req, $cb, $arg) = @_;
49 my $inflight = $self->{inflight} or return $self->close;
51 # {wbuf} is rare, I hope:
52 cat_async_step($self, $inflight) if $self->{wbuf};
54 $self->fail("gcf2c write: $!") if !$self->write($req) && !$self->{sock};
55 push @$inflight, $req, $cb, $arg;
58 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
59 sub alternates_changed {}
61 # DS::event_loop will call this
65 $self->close if !$self->{in} || !$self->{sock}; # process died
66 my $inflight = $self->{inflight};
67 if ($inflight && @$inflight) {
68 cat_async_step($self, $inflight);
69 return $self->close unless $self->{in}; # process died
71 # ok, more to do, requeue for fairness
72 $self->requeue if @$inflight || exists($self->{rbuf});
78 delete $self->{sock}; # if outside event_loop
79 PublicInbox::Git::DESTROY($self);
84 *cat_async_step = \&PublicInbox::Git::cat_async_step;