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 EPOLLOUT);
13 # async_cat => GitAsyncCat ref (read-only pipe)
14 # sock => writable pipe to Gcf2::loop
16 sub new { bless($_[0] // {}, __PACKAGE__) }
20 # ensure the child process has the same @INC we do:
21 my $env = { PERL5LIB => join(':', @INC) };
23 pipe($out_r, $out_w) or die "pipe failed: $!";
24 my $rdr = { 0 => $out_r, 2 => $self->{2} };
25 my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
26 @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
27 fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
30 $self->SUPER::new($out_w, 0); # EPOLL_CTL_ADD (a bit wasteful :x)
31 $self->{inflight} = [];
36 $self->close; # PublicInbox::DS::close
37 PublicInbox::Git::fail($self, @_);
40 sub cat_async ($$$;$) {
41 my ($self, $req, $cb, $arg) = @_;
42 my $inflight = $self->{inflight} // gcf2c_begin($self);
45 cat_async_step($self, $inflight) if $self->{wbuf};
47 $self->write(\"$req\n") or $self->fail("gcf2c write: $!");
48 push @$inflight, $req, $cb, $arg;
51 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
52 sub alternates_changed {}
56 # this is the write-only end of a pipe, DS->EventLoop will call this
57 *event_step = \&PublicInbox::DS::flush_write;
60 *cat_async_step = \&PublicInbox::Git::cat_async_step;