From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 2/3] git-http-backend: start refactoring to use callback
Date: Thu, 25 Feb 2016 04:02:36 +0000 [thread overview]
Message-ID: <20160225040237.29014-3-e@80x24.org> (raw)
In-Reply-To: <20160225040237.29014-1-e@80x24.org>
Designing for asynchronous, non-blocking operations makes
adapting for synchronous, blocking operation easy.
Going the other way around is not easy, so do it now and
allow us to be more easily adapted for non-blocking use
in the next commit...
---
lib/PublicInbox/GitHTTPBackend.pm | 73 +++++++++++++++++++++++++--------------
1 file changed, 47 insertions(+), 26 deletions(-)
diff --git a/lib/PublicInbox/GitHTTPBackend.pm b/lib/PublicInbox/GitHTTPBackend.pm
index cba025e..3cf7857 100644
--- a/lib/PublicInbox/GitHTTPBackend.pm
+++ b/lib/PublicInbox/GitHTTPBackend.pm
@@ -178,34 +178,55 @@ sub serve_smart {
my @cmd = qw(git http-backend);
exec(@cmd) or die 'exec `' . join(' ', @cmd). "' failed: $!\n";
}
- $wpipe = undef;
- $in = undef;
- my @h;
- my $code = 200;
- {
- local $/ = "\r\n";
- while (defined(my $line = <$rpipe>)) {
- if ($line =~ /\AStatus:\s*(\d+)/) {
- $code = $1;
- } else {
- chomp $line;
- last if $line eq '';
- push @h, split(/:\s*/, $line, 2);
- }
+ $wpipe = $in = undef;
+ $rpipe->blocking(0);
+ $buf = '';
+ my $vin;
+ vec($vin, fileno($rpipe), 1) = 1;
+ my ($fh, $res);
+ my $fail = sub {
+ my ($e) = @_;
+ if ($e eq 'EAGAIN') {
+ select($vin, undef, undef, undef);
+ } else {
+ $rpipe = undef;
+ $fh->close if $fh;
+ $err->print('git http-backend error: ', $e, "\n");
}
- }
- return if $code == 403;
- sub {
- my ($cb) = @_;
- my $fh = $cb->([ $code, \@h ]);
- while (1) {
- my $r = sysread($rpipe, $buf, 8192);
- die "$!\n" unless defined $r;
- last if ($r == 0);
- $fh->write($buf);
+ };
+ my $cb = sub {
+ my $r = sysread($rpipe, $buf, 8192, length($buf));
+ return $fail->($!{EAGAIN} ? 'EAGAIN' : $!) unless defined $r;
+ if ($r == 0) { # EOF
+ $rpipe = undef;
+ $fh->close if $fh;
+ return;
}
- $fh->close;
- }
+ if ($fh) { # stream body from git-http-backend to HTTP client
+ $fh->write($buf);
+ $buf = '';
+ } elsif ($buf =~ s/\A(.*?)\r?\n\r?\n//s) { # parse headers
+ my $h = $1;
+ my $code = 200;
+ my @h;
+ foreach my $l (split(/\r?\n/, $h)) {
+ my ($k, $v) = split(/:\s*/, $l, 2);
+ if ($k =~ /\AStatus\z/i) {
+ $code = int($v);
+ } else {
+ push @h, $k, $v;
+ }
+ }
+ # write response header:
+ $fh = $res->([ $code, \@h ]);
+ $fh->write($buf);
+ $buf = '';
+ } # else { keep reading ... }
+ };
+ sub {
+ ($res) = @_;
+ while ($rpipe) { $cb->() }
+ };
}
1;
--
EW
next prev parent reply other threads:[~2016-02-25 4:02 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-25 4:02 [PATCH 0/3] migrate git-http-backend to async use Eric Wong
2016-02-25 4:02 ` [PATCH 1/3] use pipe for git-http-backend output Eric Wong
2016-02-25 4:02 ` Eric Wong [this message]
2016-02-25 4:02 ` [PATCH 3/3] git-http-backend: start async API for streaming Eric Wong
2016-02-25 4:30 ` [PATCH v2] " Eric Wong
2016-02-25 4:39 ` [PATCH 4/3] git-http-backend: avoid multi-arg print statemtents Eric Wong
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://public-inbox.org/README
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160225040237.29014-3-e@80x24.org \
--to=e@80x24.org \
--cc=meta@public-inbox.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).