]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitAsyncCat.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / GitAsyncCat.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 # internal class used by PublicInbox::Git + PublicInbox::DS
5 # This parses the output pipe of "git cat-file --batch"
6 package PublicInbox::GitAsyncCat;
7 use v5.12;
8 use parent qw(PublicInbox::DS Exporter);
9 use PublicInbox::DS qw(awaitpid);
10 use POSIX qw(WNOHANG);
11 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
12 our @EXPORT = qw(ibx_async_cat ibx_async_prefetch async_check);
13 use PublicInbox::Git ();
14
15 our $GCF2C; # singleton PublicInbox::Gcf2Client
16
17 sub close {
18         my ($self) = @_;
19         if (my $git = delete $self->{git}) {
20                 $git->async_abort;
21         }
22         $self->SUPER::close; # PublicInbox::DS::close
23 }
24
25 sub aclose { $_[1]->close } # ignore PID ($_[0])
26
27 sub event_step {
28         my ($self) = @_;
29         my $git = $self->{git} or return;
30         return $self->close if ($git->{in} // 0) != ($self->{sock} // 1);
31         my $inflight = $git->{inflight};
32         if ($inflight && @$inflight) {
33                 $git->cat_async_step($inflight);
34
35                 # child death?
36                 if (($git->{in} // 0) != ($self->{sock} // 1)) {
37                         $self->close;
38                 } elsif (@$inflight || exists $git->{rbuf}) {
39                         # ok, more to do, requeue for fairness
40                         $self->requeue;
41                 }
42         }
43 }
44
45 sub watch_cat {
46         my ($git) = @_;
47         $git->{async_cat} //= do {
48                 my $self = bless { git => $git }, __PACKAGE__;
49                 $git->{in}->blocking(0);
50                 $self->SUPER::new($git->{in}, EPOLLIN|EPOLLET);
51                 awaitpid($git->{pid}, \&aclose, $self);
52                 \undef; # this is a true ref()
53         };
54 }
55
56 sub ibx_async_cat ($$$$) {
57         my ($ibx, $oid, $cb, $arg) = @_;
58         my $git = $ibx->{git} // $ibx->git;
59         # {topdir} means ExtSearch (likely [extindex "all"]) with potentially
60         # 100K alternates.  git(1) has a proposed patch for 100K alternates:
61         # <https://lore.kernel.org/git/20210624005806.12079-1-e@80x24.org/>
62         if (!defined($ibx->{topdir}) && !defined($git->{-tmp}) &&
63                 ($GCF2C //= eval {
64                 require PublicInbox::Gcf2Client;
65                 PublicInbox::Gcf2Client::new();
66         } // 0)) { # 0: do not retry if libgit2 or Inline::C are missing
67                 $GCF2C->gcf2_async(\"$oid $git->{git_dir}\n", $cb, $arg);
68                 \undef;
69         } else { # read-only end of git-cat-file pipe
70                 $git->cat_async($oid, $cb, $arg);
71                 watch_cat($git);
72         }
73 }
74
75 sub async_check ($$$$) {
76         my ($ibx, $oidish, $cb, $arg) = @_; # $ibx may be $ctx
77         my $git = $ibx->{git} // $ibx->git;
78         $git->check_async($oidish, $cb, $arg);
79         return watch_cat($git) if $git->{-bc}; # --batch-command
80         $git->{async_chk} //= do {
81                 my $self = bless { git => $git }, 'PublicInbox::GitAsyncCheck';
82                 $git->{in_c}->blocking(0);
83                 $self->SUPER::new($git->{in_c}, EPOLLIN|EPOLLET);
84                 awaitpid($git->{pid_c}, \&aclose, $self);
85                 \undef; # this is a true ref()
86         };
87 }
88
89 # this is safe to call inside $cb, but not guaranteed to enqueue
90 # returns true if successful, undef if not.  For fairness, we only
91 # prefetch if there's no in-flight requests.
92 sub ibx_async_prefetch {
93         my ($ibx, $oid, $cb, $arg) = @_;
94         my $git = $ibx->git;
95         if (!defined($ibx->{topdir}) && $GCF2C) {
96                 if (!@{$GCF2C->{inflight} // []}) {
97                         $oid .= " $git->{git_dir}\n";
98                         return $GCF2C->gcf2_async(\$oid, $cb, $arg); # true
99                 }
100         } elsif ($git->{async_cat}) {
101                 return $git->async_prefetch($oid, $cb, $arg);
102         }
103         undef;
104 }
105
106 1;
107 package PublicInbox::GitAsyncCheck;
108 use v5.12;
109 our @ISA = qw(PublicInbox::GitAsyncCat);
110 use POSIX qw(WNOHANG);
111 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
112
113 sub event_step {
114         my ($self) = @_;
115         my $git = $self->{git} or return;
116         return $self->close if ($git->{in_c} // 0) != ($self->{sock} // 1);
117         my $inflight = $git->{inflight_c};
118         if ($inflight && @$inflight) {
119                 $git->check_async_step($inflight);
120
121                 # child death?
122                 if (($git->{in_c} // 0) != ($self->{sock} // 1)) {
123                         $self->close;
124                 } elsif (@$inflight || exists $git->{rbuf_c}) {
125                         # ok, more to do, requeue for fairness
126                         $self->requeue;
127                 }
128         }
129 }
130
131 1;