]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2Client.pm
54957cf38621ec21e90b4cdb747b8ee2c109119f
[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 use PublicInbox::DS qw(dwaitpid);
13 # fields:
14 #       async_cat => GitAsyncCat ref (read-only pipe)
15 #       sock => writable pipe to Gcf2::loop
16 #       in => pipe we read from
17 #       pid => PID of Gcf2::loop process
18 #       owner_pid => process which spawned {pid}
19 sub new  {
20         my ($rdr) = @_;
21         my $self = bless {}, __PACKAGE__;
22         # ensure the child process has the same @INC we do:
23         my $env = { PERL5LIB => join(':', @INC) };
24         my ($out_r, $out_w);
25         pipe($out_r, $out_w) or die "pipe failed: $!";
26         $rdr //= {};
27         $rdr->{0} = $out_r;
28         my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
29         $self->{owner_pid} = $$;
30         @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
31         fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
32         $out_w->autoflush(1);
33         $out_w->blocking(0);
34         $self->{inflight} = [];
35         $self->SUPER::new($out_w, EPOLLONESHOT); # detect errors once
36 }
37
38 sub fail {
39         my $self = shift;
40         $self->close; # PublicInbox::DS::close
41         PublicInbox::Git::fail($self, @_);
42 }
43
44 sub cat_async ($$$;$) {
45         my ($self, $req, $cb, $arg) = @_;
46         my $inflight = $self->{inflight};
47
48         # {wbuf} is rare, I hope:
49         cat_async_step($self, $inflight) if $self->{wbuf};
50
51         if (!$self->write(\"$req\n")) {
52                 $self->fail("gcf2c write: $!") if !$self->{sock};
53         }
54         push @$inflight, $req, $cb, $arg;
55 }
56
57 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
58 sub alternates_changed {}
59
60 # this is the write-only end of a pipe, DS->EventLoop will call this
61 sub event_step {
62         my ($self) = @_;
63         $self->flush_write;
64         $self->close if !$self->{in}; # process died
65 }
66
67 no warnings 'once';
68
69 sub DESTROY {
70         my ($self) = @_;
71         delete $self->{in};
72         # GitAsyncCat::event_step may reap us with WNOHANG, too
73         my $pid = delete $self->{pid} or return;
74         if ($$ == $self->{owner_pid}) {
75                 PublicInbox::DS->in_loop ? $self->close : delete($self->{sock});
76                 dwaitpid $pid;
77         }
78 }
79
80 # used by GitAsyncCat
81 *cat_async_step = \&PublicInbox::Git::cat_async_step;
82
83 1;