user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] http: reduce over-buffering for getline responses
Date: Sat, 21 May 2016 03:03:16 +0000	[thread overview]
Message-ID: <20160521030317.24152-2-e@80x24.org> (raw)
In-Reply-To: <20160521030317.24152-1-e@80x24.org>

By switching to a "pull"-based I/O model for reading
application responses, we should be able to throttle
buffering to slow clients more effectively and avoid
wasting precious RAM.

This will also allow us to more Danga::Socket-specific
knowledge out of the PSGI application and keep it
confined to PublicInbox::HTTP.
---
 lib/PublicInbox/HTTP.pm | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 1ef3fb3..f69056f 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -205,7 +205,7 @@ sub response_write {
 		if ($alive) {
 			$self->event_write; # watch for readability if done
 		} else {
-			$self->write(sub { $self->close });
+			Danga::Socket::write($self, sub { $self->close });
 		}
 		if (my $obj = $env->{'pi-httpd.inbox'}) {
 			# grace period for reaping resources
@@ -215,9 +215,27 @@ sub response_write {
 		$self->{env} = undef;
 	};
 
-	if (defined $res->[2]) {
-		Plack::Util::foreach($res->[2], $write);
-		$close->();
+	if (defined(my $body = $res->[2])) {
+		if (ref $body eq 'ARRAY') {
+			$write->($_) foreach @$body;
+			$close->();
+		} else {
+			my $pull;
+			$pull = sub {
+				local $/ = \8192;
+				while (defined(my $buf = $body->getline)) {
+					$write->($buf);
+					if ($self->{write_buf}) {
+						$self->write($pull);
+						return;
+					}
+				}
+				$pull = undef;
+				$body->close();
+				$close->();
+			};
+			$pull->();
+		}
 	} else {
 		# this is returned to the calling application:
 		Plack::Util::inline_object(write => $write, close => $close);
-- 
EW


  reply	other threads:[~2016-05-21  3:03 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-05-21  3:03 [PATCH 0/2] http: start migrating to pull-based I/O Eric Wong
2016-05-21  3:03 ` Eric Wong [this message]
2016-05-21  3:03 ` [PATCH 2/2] mbox: switch generation over to pull model Eric Wong
2016-05-21  4:43 ` [PATCH 4/1] unsubscribe: prevent decrypt from showing random crap Eric Wong
2016-05-21  5:31 ` [PATCH] localize $/ in more places to avoid potential problems 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=20160521030317.24152-2-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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

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).