]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitAsyncCat.pm
ds: remove fields.pm usage
[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 + Danga::Socket
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 PublicInbox::Syscall qw(EPOLLIN EPOLLET);
15 our @EXPORT = qw(git_async_cat);
16
17 sub _add {
18         my ($class, $git) = @_;
19         $git->batch_prepare;
20         my $self = bless { git => $git }, $class;
21         $self->SUPER::new($git->{in}, EPOLLIN|EPOLLET);
22         \undef; # this is a true ref()
23 }
24
25 sub event_step {
26         my ($self) = @_;
27         my $git = $self->{git} or return; # ->close-ed
28         my $inflight = $git->{inflight};
29         if ($inflight && @$inflight) {
30                 $git->cat_async_step($inflight);
31                 $self->requeue if @$inflight || exists $git->{cat_rbuf};
32         }
33 }
34
35 sub close {
36         my ($self) = @_;
37         if (my $git = delete $self->{git}) {
38                 delete $git->{async_cat};
39         }
40         $self->SUPER::close; # PublicInbox::DS::close
41 }
42
43 sub git_async_cat ($$$$) {
44         my ($git, $oid, $cb, $arg) = @_;
45         $git->cat_async($oid, $cb, $arg);
46         $git->{async_cat} //= _add(__PACKAGE__, $git);
47 }
48
49 1;