From 0179230221231f8f5a3edc2f2836cc7c7e089dda Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 5 Jul 2020 23:27:44 +0000 Subject: view: make /$INBOX/$MSGID/ permalink async This will allow -httpd to handle other requusts if waiting on an HDD seek or git to decode a blob. --- lib/PublicInbox/WwwStream.pm | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) (limited to 'lib/PublicInbox/WwwStream.pm') diff --git a/lib/PublicInbox/WwwStream.pm b/lib/PublicInbox/WwwStream.pm index 42fb183f..eecc2701 100644 --- a/lib/PublicInbox/WwwStream.pm +++ b/lib/PublicInbox/WwwStream.pm @@ -15,9 +15,6 @@ use PublicInbox::Hval qw(ascii_html prurl); our $TOR_URL = 'https://www.torproject.org/'; our $CODE_URL = 'https://public-inbox.org/public-inbox.git'; -# noop for HTTP.pm (and any other PSGI servers) -sub close {} - sub base_url ($) { my $ctx = shift; my $base_url = $ctx->{-inbox}->base_url($ctx->{env}); @@ -40,6 +37,11 @@ sub response { [ $code, $res_hdr, $ctx ] } +sub async_eml { # ->{async_eml} for async_blob_cb + my ($ctx, $eml) = @_; + $ctx->{http_out}->write($ctx->translate($ctx->{cb}->($ctx, $eml))); +} + sub html_top ($) { my ($ctx) = @_; my $ibx = $ctx->{-inbox}; @@ -157,8 +159,14 @@ EOF sub getline { my ($ctx) = @_; my $cb = $ctx->{cb} or return; - if (defined(my $buf = $cb->($ctx))) { - return $ctx->translate($buf); + while (defined(my $x = $cb->($ctx))) { # x = smsg or scalar non-ref + if (ref($x)) { # smsg + my $eml = $ctx->{-inbox}->smsg_eml($x) or next; + $ctx->{smsg} = $x; + return $ctx->translate($cb->($ctx, $eml)); + } else { # scalar + return $ctx->translate($x); + } } delete $ctx->{cb}; $ctx->zflush(_html_end($ctx)); @@ -179,4 +187,26 @@ sub html_oneshot ($$;$) { [ $code, $res_hdr, \@bdy ] } +sub async_next ($) { + my ($http) = @_; # PublicInbox::HTTP + my $ctx = $http->{forward} or return; + eval { + if (my $smsg = $ctx->{smsg} = $ctx->{cb}->($ctx)) { + $ctx->smsg_blob($smsg); + } else { + $ctx->{http_out}->write( + $ctx->translate(_html_end($ctx))); + $ctx->close; # GzipFilter->close + } + }; + warn "E: $@" if $@; +} + +sub aresponse { + my ($ctx, $code, $cb) = @_; + my $res_hdr = [ 'Content-Type' => 'text/html; charset=UTF-8' ]; + init($ctx, $cb); + $ctx->psgi_response($code, $res_hdr, \&async_next, \&async_eml); +} + 1; -- cgit v1.2.3-24-ge0c7