user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH] http: support async_pass for Danga::Socket
@ 2016-05-21 23:45 Eric Wong
  2016-05-22  6:17 ` [PATCH 1/2] http: fix typo: write_buf => write_buf_size Eric Wong
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Wong @ 2016-05-21 23:45 UTC (permalink / raw)
  To: meta

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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 1/2] http: fix typo: write_buf => write_buf_size
  2016-05-21 23:45 [PATCH] http: support async_pass for Danga::Socket Eric Wong
@ 2016-05-22  6:17 ` Eric Wong
  2016-05-22  6:17   ` [PATCH 2/2] http: pass reference to Danga::Socket::write Eric Wong
  0 siblings, 1 reply; 3+ messages in thread
From: Eric Wong @ 2016-05-22  6:17 UTC (permalink / raw)
  To: meta

Otherwise, we get deep recursion as we keep calling
recursively on giant responses
---
 lib/PublicInbox/HTTP.pm | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index d523bd4..6576bf6 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -228,7 +228,7 @@ sub response_write {
 				my $buf = $body->getline;
 				if (defined $buf) {
 					$write->($buf);
-					if ($self->{write_buf}) {
+					if ($self->{write_buf_size}) {
 						$body->watch_read(0);
 						$self->write($restart_read);
 					}
@@ -243,7 +243,7 @@ sub response_write {
 				local $/ = \8192;
 				while (defined(my $buf = $body->getline)) {
 					$write->($buf);
-					if ($self->{write_buf}) {
+					if ($self->{write_buf_size}) {
 						$self->write($pull);
 						return;
 					}

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] http: pass reference to Danga::Socket::write
  2016-05-22  6:17 ` [PATCH 1/2] http: fix typo: write_buf => write_buf_size Eric Wong
@ 2016-05-22  6:17   ` Eric Wong
  0 siblings, 0 replies; 3+ messages in thread
From: Eric Wong @ 2016-05-22  6:17 UTC (permalink / raw)
  To: meta

This can avoid an expensive copy for big strings.
---
 lib/PublicInbox/HTTP.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 6576bf6..4eb1448 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -200,7 +200,7 @@ sub response_write {
 	my $alive = response_header_write($self, $env, $res);
 
 	# middlewares such as Deflater may write empty strings
-	my $write = sub { $self->write($_[0]) if $_[0] ne '' };
+	my $write = sub { $self->write(\($_[0])) if $_[0] ne '' };
 	my $close = sub {
 		if ($alive) {
 			$self->event_write; # watch for readability if done

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-05-22  6:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-21 23:45 [PATCH] http: support async_pass for Danga::Socket Eric Wong
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

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