X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FGcf2Client.pm;h=2022293d6b0a4684c6d72adf74e6413a426fc0bf;hb=af0b0fb7a454470a32c452119d0392e0dedb3fe1;hp=5120698f2e689ca751c0aeeb75da8bf910f41cef;hpb=dc03cabb5d167618797e9e8a6ec615bda7b0638b;p=public-inbox.git
diff --git a/lib/PublicInbox/Gcf2Client.pm b/lib/PublicInbox/Gcf2Client.pm
index 5120698f..2022293d 100644
--- a/lib/PublicInbox/Gcf2Client.pm
+++ b/lib/PublicInbox/Gcf2Client.pm
@@ -1,40 +1,83 @@
-# Copyright (C) 2020 all contributors
+# Copyright (C) 2020-2021 all contributors
# License: AGPL-3.0+
+
+# connects public-inbox processes to PublicInbox::Gcf2::loop()
package PublicInbox::Gcf2Client;
use strict;
-use parent 'PublicInbox::Git';
+use parent qw(PublicInbox::DS);
+use PublicInbox::Git;
use PublicInbox::Spawn qw(popen_rd);
use IO::Handle ();
-
-sub new {
+use PublicInbox::Syscall qw(EPOLLONESHOT);
+use PublicInbox::DS qw(dwaitpid);
+# fields:
+# async_cat => GitAsyncCat ref (read-only pipe)
+# sock => writable pipe to Gcf2::loop
+# in => pipe we read from
+# pid => PID of Gcf2::loop process
+# owner_pid => process which spawned {pid}
+sub new {
my ($rdr) = @_;
my $self = bless {}, __PACKAGE__;
+ # ensure the child process has the same @INC we do:
+ my $env = { PERL5LIB => join(':', @INC) };
my ($out_r, $out_w);
- pipe($out_r, $out_w) or $self->fail("pipe failed: $!");
+ pipe($out_r, $out_w) or die "pipe failed: $!";
$rdr //= {};
$rdr->{0} = $out_r;
- @$self{qw(in pid)} = popen_rd(['public-inbox-gcf2'], undef, $rdr);
- $self->{inflight} = [];
- $self->{out} = $out_w;
+ my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
+ $self->{owner_pid} = $$;
+ @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
$out_w->autoflush(1);
- $self;
+ $out_w->blocking(0);
+ $self->{inflight} = [];
+ $self->SUPER::new($out_w, EPOLLONESHOT); # detect errors once
}
-sub add_git_dir {
- my ($self, $git_dir) = @_;
+sub fail {
+ my $self = shift;
+ $self->close; # PublicInbox::DS::close
+ PublicInbox::Git::fail($self, @_);
+}
- # ensure buffers are drained, length($git_dir) may exceed
- # PIPE_BUF on platforms where PIPE_BUF is only 512 bytes
+sub cat_async ($$$;$) {
+ my ($self, $req, $cb, $arg) = @_;
my $inflight = $self->{inflight};
- while (scalar(@$inflight)) {
- $self->cat_async_step($inflight);
+
+ # {wbuf} is rare, I hope:
+ cat_async_step($self, $inflight) if $self->{wbuf};
+
+ if (!$self->write(\"$req\n")) {
+ $self->fail("gcf2c write: $!") if !$self->{sock};
}
- print { $self->{out} } $git_dir, "\n" or
- $self->fail("write error: $!");
+ push @$inflight, $req, $cb, $arg;
}
-# always false, since -gcf2 retries internally
+# ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
sub alternates_changed {}
+# this is the write-only end of a pipe, DS->EventLoop will call this
+sub event_step {
+ my ($self) = @_;
+ $self->flush_write;
+ $self->close if !$self->{in}; # process died
+}
+
+no warnings 'once';
+
+sub DESTROY {
+ my ($self) = @_;
+ delete $self->{in};
+ # GitAsyncCat::event_step may reap us with WNOHANG, too
+ my $pid = delete $self->{pid} or return;
+ if ($$ == $self->{owner_pid}) {
+ PublicInbox::DS->in_loop ? $self->close : delete($self->{sock});
+ dwaitpid $pid;
+ }
+}
+
+# used by GitAsyncCat
+*cat_async_step = \&PublicInbox::Git::cat_async_step;
+
1;