]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/GitAsyncCat.pm
imap: use git-cat-file asynchronously
[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 fields qw(git);
15 use PublicInbox::Syscall qw(EPOLLIN EPOLLET);
16 our @EXPORT = qw(git_async_msg);
17
18 sub new {
19         my ($class, $git) = @_;
20         my $self = fields::new($class);
21         $git->batch_prepare;
22         $self->SUPER::new($git->{in}, EPOLLIN|EPOLLET);
23         $self->{git} = $git;
24         $self;
25 }
26
27 sub event_step {
28         my ($self) = @_;
29         my $git = $self->{git} or return; # ->close-ed
30         my $inflight = $git->{inflight};
31         if (@$inflight) {
32                 $git->cat_async_step($inflight);
33                 $self->requeue if @$inflight || length(${$git->{'--batch'}});
34         }
35 }
36
37 sub close {
38         my ($self) = @_;
39         delete $self->{git};
40         $self->SUPER::close; # PublicInbox::DS::close
41 }
42
43 sub git_async_msg ($$$$) {
44         my ($ibx, $smsg, $cb, $arg) = @_;
45         $ibx->git->cat_async($smsg->{blob}, $cb, $arg);
46         $ibx->{async_cat} //= new(__PACKAGE__, $ibx->{git});
47 }
48
49 1;