]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Gcf2Client.pm
gcf2: wire up read-only daemons and rm -gcf2 script
[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 EPOLLOUT);
12 # fields:
13 #       async_cat => GitAsyncCat ref (read-only pipe)
14 #       sock => writable pipe to Gcf2::loop
15
16 sub new { bless($_[0] // {}, __PACKAGE__) }
17
18 sub gcf2c_begin ($) {
19         my ($self) = @_;
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         my $rdr = { 0 => $out_r, 2 => $self->{2} };
25         my $cmd = [$^X, qw[-MPublicInbox::Gcf2 -e PublicInbox::Gcf2::loop()]];
26         @$self{qw(in pid)} = popen_rd($cmd, $env, $rdr);
27         fcntl($out_w, 1031, 4096) if $^O eq 'linux'; # 1031: F_SETPIPE_SZ
28         $out_w->autoflush(1);
29         $out_w->blocking(0);
30         $self->SUPER::new($out_w, 0); # EPOLL_CTL_ADD (a bit wasteful :x)
31         $self->{inflight} = [];
32 }
33
34 sub fail {
35         my $self = shift;
36         $self->close; # PublicInbox::DS::close
37         PublicInbox::Git::fail($self, @_);
38 }
39
40 sub cat_async ($$$;$) {
41         my ($self, $req, $cb, $arg) = @_;
42         my $inflight = $self->{inflight} // gcf2c_begin($self);
43
44         # rare, I hope:
45         cat_async_step($self, $inflight) if $self->{wbuf};
46
47         $self->write(\"$req\n") or $self->fail("gcf2c write: $!");
48         push @$inflight, $req, $cb, $arg;
49 }
50
51 # ensure PublicInbox::Git::cat_async_step never calls cat_async_retry
52 sub alternates_changed {}
53
54 no warnings 'once';
55
56 # this is the write-only end of a pipe, DS->EventLoop will call this
57 *event_step = \&PublicInbox::DS::flush_write;
58
59 # used by GitAsyncCat
60 *cat_async_step = \&PublicInbox::Git::cat_async_step;
61
62 1;