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