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