]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2Client.pm
rename {pi_config} fields to {pi_cfg}
[public-inbox.git] / lib / PublicInbox / Gcf2Client.pm
1 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # connects public-inbox processes to PublicInbox::Gcf2::loop()
5 package PublicInbox::Gcf2Client;
6 use strict;
7 use parent qw(PublicInbox::DS);
8 use PublicInbox::Git;
9 use PublicInbox::Spawn qw(popen_rd);
10 use IO::Handle ();
11 use PublicInbox::Syscall qw(EPOLLONESHOT);
12 # fields:
13 #       async_cat => GitAsyncCat ref (read-only pipe)
14 #       sock => writable pipe to Gcf2::loop
15
16
17 sub new  {
18         my ($rdr) = @_;
19         my $self = bless {}, __PACKAGE__;
20         # ensure the child process has the same @INC we do:
21         my $env = { PERL5LIB => join(':', @INC) };
22         my ($out_r, $out_w);
23         pipe($out_r, $out_w) or die "pipe failed: $!";
24         $rdr //= {};
25         $rdr->{0} = $out_r;
26         my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
27         @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
28         fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
29         $out_w->autoflush(1);
30         $out_w->blocking(0);
31         $self->{inflight} = [];
32         $self->SUPER::new($out_w, EPOLLONESHOT); # detect errors once
33 }
34
35 sub fail {
36         my $self = shift;
37         $self->close; # PublicInbox::DS::close
38         PublicInbox::Git::fail($self, @_);
39 }
40
41 sub cat_async ($$$;$) {
42         my ($self, $req, $cb, $arg) = @_;
43         my $inflight = $self->{inflight};
44
45         # {wbuf} is rare, I hope:
46         cat_async_step($self, $inflight) if $self->{wbuf};
47
48         if (!$self->write(\"$req\n")) {
49                 $self->fail("gcf2c write: $!") if !$self->{sock};
50         }
51         push @$inflight, $req, $cb, $arg;
52 }
53
54 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
55 sub alternates_changed {}
56
57 # this is the write-only end of a pipe, DS->EventLoop will call this
58 sub event_step {
59         my ($self) = @_;
60         $self->flush_write;
61         $self->close if !$self->{in}; # process died
62 }
63
64 no warnings 'once';
65
66 # used by GitAsyncCat
67 *cat_async_step = \&PublicInbox::Git::cat_async_step;
68
69 1;