1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # internal class used by PublicInbox::Git + PublicInbox::DS
5 # This parses the output pipe of "git cat-file --batch"
6 package PublicInbox::GitAsyncCat;
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 ();
15 our $GCF2C; # singleton PublicInbox::Gcf2Client
19 if (my $git = delete $self->{git}) {
22 $self->SUPER::close; # PublicInbox::DS::close
25 sub aclose { $_[1]->close } # ignore PID ($_[0])
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);
36 if (($git->{in} // 0) != ($self->{sock} // 1)) {
38 } elsif (@$inflight || exists $git->{rbuf}) {
39 # ok, more to do, requeue for fairness
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()
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}) &&
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);
69 } else { # read-only end of git-cat-file pipe
70 $git->cat_async($oid, $cb, $arg);
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()
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) = @_;
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
100 } elsif ($git->{async_cat}) {
101 return $git->async_prefetch($oid, $cb, $arg);
107 package PublicInbox::GitAsyncCheck;
109 our @ISA = qw(PublicInbox::GitAsyncCat);
110 use POSIX qw(WNOHANG);
111 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
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);
122 if (($git->{in_c} // 0) != ($self->{sock} // 1)) {
124 } elsif (@$inflight || exists $git->{rbuf_c}) {
125 # ok, more to do, requeue for fairness