]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2Client.pm
397774f90bf212f9c74351b8d80e0807636bff7a
[public-inbox.git] / lib / PublicInbox / Gcf2Client.pm
1 # Copyright (C) 2020-2021 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::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 # fields:
14 #       sock => socket to Gcf2::loop
15 # The rest of these fields are compatible with what PublicInbox::Git
16 # uses code-sharing
17 #       pid => PID of Gcf2::loop process
18 #       pid.owner => process which spawned {pid}
19 #       in => same as {sock}, for compatibility with PublicInbox::Git
20 #       inflight => array (see PublicInbox::Git)
21 #       cat_rbuf => scalarref, may be non-existent or empty
22 sub new  {
23         my ($rdr) = @_;
24         my $self = bless {}, __PACKAGE__;
25         # ensure the child process has the same @INC we do:
26         my $env = { PERL5LIB => join(':', @INC) };
27         my ($s1, $s2);
28         socketpair($s1, $s2, AF_UNIX, SOCK_STREAM, 0) or die "socketpair $!";
29         $rdr //= {};
30         $rdr->{0} = $rdr->{1} = $s2;
31         my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop]];
32         $self->{'pid.owner'} = $$;
33         $self->{pid} = spawn($cmd, $env, $rdr);
34         $s1->blocking(0);
35         $self->{inflight} = [];
36         $self->{in} = $s1;
37         $self->SUPER::new($s1, EPOLLIN|EPOLLET);
38 }
39
40 sub fail {
41         my $self = shift;
42         $self->close; # PublicInbox::DS::close
43         PublicInbox::Git::fail($self, @_);
44 }
45
46 sub gcf2_async ($$$;$) {
47         my ($self, $req, $cb, $arg) = @_;
48         my $inflight = $self->{inflight} or return $self->close;
49
50         # {wbuf} is rare, I hope:
51         cat_async_step($self, $inflight) if $self->{wbuf};
52
53         $self->fail("gcf2c write: $!") if !$self->write($req) && !$self->{sock};
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 # DS->EventLoop will call this
61 sub event_step {
62         my ($self) = @_;
63         $self->flush_write;
64         $self->close if !$self->{in} || !$self->{sock}; # process died
65         my $inflight = $self->{inflight};
66         if ($inflight && @$inflight) {
67                 cat_async_step($self, $inflight);
68                 return $self->close unless $self->{in}; # process died
69
70                 # ok, more to do, requeue for fairness
71                 $self->requeue if @$inflight || exists($self->{cat_rbuf});
72         }
73 }
74
75 sub DESTROY {
76         my ($self) = @_;
77         delete $self->{sock}; # if outside EventLoop
78         PublicInbox::Git::DESTROY($self);
79 }
80
81 no warnings 'once';
82
83 *cat_async_step = \&PublicInbox::Git::cat_async_step;
84
85 1;