]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitAsyncCat.pm
tls: epollbit: account for miscellaneous OpenSSL errors
[public-inbox.git] / lib / PublicInbox / GitAsyncCat.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 # internal class used by PublicInbox::Git + PublicInbox::DS
5 # This parses the output pipe of "git cat-file --batch"
6 #
7 # Note: this does NOT set the non-blocking flag, we expect `git cat-file'
8 # to be a local process, and git won't start writing a blob until it's
9 # fully read.  So minimize context switching and read as much as possible
10 # and avoid holding a buffer in our heap any longer than it has to live.
11 package PublicInbox::GitAsyncCat;
12 use strict;
13 use parent qw(PublicInbox::DS Exporter);
14 use POSIX qw(WNOHANG);
15 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
16 our @EXPORT = qw(git_async_cat git_async_prefetch);
17 use PublicInbox::Git ();
18
19 our $GCF2C; # singleton PublicInbox::Gcf2Client
20
21 sub close {
22         my ($self) = @_;
23
24         if (my $gitish = delete $self->{gitish}) {
25                 PublicInbox::Git::cat_async_abort($gitish);
26         }
27         $self->SUPER::close; # PublicInbox::DS::close
28 }
29
30 sub event_step {
31         my ($self) = @_;
32         my $gitish = $self->{gitish} or return;
33         return $self->close if ($gitish->{in} // 0) != ($self->{sock} // 1);
34         my $inflight = $gitish->{inflight};
35         if ($inflight && @$inflight) {
36                 $gitish->cat_async_step($inflight);
37
38                 # child death?
39                 if (($gitish->{in} // 0) != ($self->{sock} // 1)) {
40                         $self->close;
41                 } elsif (@$inflight || exists $gitish->{cat_rbuf}) {
42                         # ok, more to do, requeue for fairness
43                         $self->requeue;
44                 }
45         } elsif ((my $pid = waitpid($gitish->{pid}, WNOHANG)) > 0) {
46                 # May happen if the child process is killed by a BOFH
47                 # (or segfaults)
48                 delete $gitish->{pid};
49                 warn "E: gitish $pid exited with \$?=$?\n";
50                 $self->close;
51         }
52 }
53
54 sub git_async_cat ($$$$) {
55         my ($git, $oid, $cb, $arg) = @_;
56         my $gitish = $GCF2C //= eval {
57                 require PublicInbox::Gcf2;
58                 require PublicInbox::Gcf2Client;
59                 PublicInbox::Gcf2Client::new();
60         } // 0; # 0: do not retry if libgit2 or Inline::C are missing
61         if ($gitish) { # Gcf2 active, {inflight} may be unset due to errors
62                 $GCF2C->{inflight} or
63                         $gitish = $GCF2C = PublicInbox::Gcf2Client::new();
64                 $oid .= " $git->{git_dir}";
65         } else {
66                 $gitish = $git;
67         }
68         $gitish->cat_async($oid, $cb, $arg);
69         $gitish->{async_cat} //= do {
70                 # read-only end of pipe (Gcf2Client is write-only end)
71                 my $self = bless { gitish => $gitish }, __PACKAGE__;
72                 $self->SUPER::new($gitish->{in}, EPOLLIN|EPOLLET);
73                 \undef; # this is a true ref()
74         };
75 }
76
77 # this is safe to call inside $cb, but not guaranteed to enqueue
78 # returns true if successful, undef if not.
79 sub git_async_prefetch {
80         my ($git, $oid, $cb, $arg) = @_;
81         if ($GCF2C) {
82                 if ($GCF2C->{async_cat} && !$GCF2C->{wbuf}) {
83                         $oid .= " $git->{git_dir}";
84                         return $GCF2C->cat_async($oid, $cb, $arg);
85                 }
86         } elsif ($git->{async_cat} && (my $inflight = $git->{inflight})) {
87                 # we could use MAX_INFLIGHT here w/o the halving,
88                 # but lets not allow one client to monopolize a git process
89                 if (@$inflight < int(PublicInbox::Git::MAX_INFLIGHT/2)) {
90                         print { $git->{out} } $oid, "\n" or
91                                                 $git->fail("write error: $!");
92                         return push(@$inflight, $oid, $cb, $arg);
93                 }
94         }
95         undef;
96 }
97
98 1;