From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH] http: support async_pass for Danga::Socket
Date: Sat, 21 May 2016 23:45:27 +0000 [thread overview]
Message-ID: <20160521234527.21133-1-e@80x24.org> (raw)
This will allow us to minimize buffering after we wait
(possibly a long time) for readability. This also greatly
reduces the amount of Danga::Socket-specific knowledge we
have in our PSGI code, making it easier for others to
understand.
---
lib/PublicInbox/HTTP.pm | 18 ++++++++++++++++++
lib/PublicInbox/HTTPD/Async.pm | 2 ++
2 files changed, 20 insertions(+)
diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index f69056f..d523bd4 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -219,6 +219,24 @@ sub response_write {
if (ref $body eq 'ARRAY') {
$write->($_) foreach @$body;
$close->();
+ } elsif ($body->can('async_pass')) { # HTTPD::Async
+ # prevent us from reading the body faster than we
+ # can write to the client
+ my $restart_read = sub { $body->watch_read(1) };
+ $body->async_pass(sub {
+ local $/ = \8192;
+ my $buf = $body->getline;
+ if (defined $buf) {
+ $write->($buf);
+ if ($self->{write_buf}) {
+ $body->watch_read(0);
+ $self->write($restart_read);
+ }
+ return; # continue waiting
+ }
+ $body->close;
+ $close->();
+ });
} else {
my $pull;
$pull = sub {
diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm
index bedb397..ceba738 100644
--- a/lib/PublicInbox/HTTPD/Async.pm
+++ b/lib/PublicInbox/HTTPD/Async.pm
@@ -21,10 +21,12 @@ sub new {
$self;
}
+sub async_pass { $_[0]->{cb} = $_[1] }
sub event_read { $_[0]->{cb}->() }
sub event_hup { $_[0]->{cb}->() }
sub event_err { $_[0]->{cb}->() }
sub sysread { shift->{sock}->sysread(@_) }
+sub getline { $_[0]->{sock}->getline };
sub close {
my $self = shift;
--
2.8.0.rc2.38.gfdf5e7f
next reply other threads:[~2016-05-21 23:45 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-21 23:45 Eric Wong [this message]
2016-05-22 6:17 ` [PATCH 1/2] http: fix typo: write_buf => write_buf_size Eric Wong
2016-05-22 6:17 ` [PATCH 2/2] http: pass reference to Danga::Socket::write 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=20160521234527.21133-1-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).