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] http|nntp: avoid recursion inside ->write
Date: Wed, 10 Jul 2019 08:26:13 +0000	[thread overview]
Message-ID: <20190710082613.31816-1-e@80x24.org> (raw)

In HTTP.pm, we can use the same technique NNTP.pm uses with
long_response with the $long_cb callback and avoid storing
$pull in the per-client structure at all.  We can also reuse
the same logic to push the callback into wbuf from NNTP.

This does NOT introduce a new circular reference, but documents
it more clearly.
---
 lib/PublicInbox/HTTP.pm | 64 ++++++++++++++++++++---------------------
 lib/PublicInbox/NNTP.pm |  3 +-
 2 files changed, 34 insertions(+), 33 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 60b287c4..5afe167e 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -11,7 +11,7 @@ package PublicInbox::HTTP;
 use strict;
 use warnings;
 use base qw(PublicInbox::DS);
-use fields qw(httpd env input_left remote_addr remote_port forward pull);
+use fields qw(httpd env input_left remote_addr remote_port forward);
 use bytes (); # only for bytes::length
 use Fcntl qw(:seek);
 use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
@@ -260,48 +260,49 @@ sub response_done_cb ($$) {
 	}
 }
 
-sub getline_cb ($$$) {
+sub getline_response ($$$) {
 	my ($self, $write, $close) = @_;
-	local $/ = \8192;
-	my $forward = $self->{forward};
-	# limit our own running time for fairness with other
-	# clients and to avoid buffering too much:
-	if ($forward) {
-		my $buf = eval { $forward->getline };
+	my $pull; # DANGER: self-referential
+	$pull = sub {
+		my $forward = $self->{forward};
+		# limit our own running time for fairness with other
+		# clients and to avoid buffering too much:
+		my $buf = eval {
+			local $/ = \8192;
+			$forward->getline;
+		} if $forward;
+
 		if (defined $buf) {
 			$write->($buf); # may close in PublicInbox::DS::write
+
 			if ($self->{sock}) {
-				my $next = $self->{pull};
-				if ($self->{wbuf}) {
-					$self->write($next);
-				} else {
-					PublicInbox::DS::requeue($next);
-				}
-				return;
+				my $wbuf = $self->{wbuf} ||= [];
+				push @$wbuf, $pull;
+
+				# wbuf may be populated by $write->($buf),
+				# no need to rearm if so:
+				$self->requeue if scalar(@$wbuf) == 1;
+				return; # likely
 			}
 		} elsif ($@) {
 			err($self, "response ->getline error: $@");
-			$forward = undef;
 			$self->close;
 		}
-	}
 
-	delete @$self{qw(forward pull)};
-	# avoid recursion
-	if ($forward) {
-		eval { $forward->close };
-		if ($@) {
-			err($self, "response ->close error: $@");
-			$self->close; # idempotent
+		$pull = undef; # all done!
+		# avoid recursion
+		if (delete $self->{forward}) {
+			eval { $forward->close };
+			if ($@) {
+				err($self, "response ->close error: $@");
+				$self->close; # idempotent
+			}
 		}
-	}
-	$close->();
-}
+		$forward = undef;
+		$close->(); # call response_done_cb
+	};
 
-sub getline_response ($$$) {
-	my ($self, $write, $close) = @_;
-	my $pull = $self->{pull} = sub { getline_cb($self, $write, $close) };
-	$pull->();
+	$pull->(); # kick-off!
 }
 
 sub response_write {
@@ -453,7 +454,6 @@ sub close {
 	if (my $env = delete $self->{env}) {
 		delete $env->{'psgix.io'}; # prevent circular references
 	}
-	delete $self->{pull};
 	if (my $forward = delete $self->{forward}) {
 		eval { $forward->close };
 		err($self, "forward ->close error: $@") if $@;
diff --git a/lib/PublicInbox/NNTP.pm b/lib/PublicInbox/NNTP.pm
index 6796a3c4..f4208f87 100644
--- a/lib/PublicInbox/NNTP.pm
+++ b/lib/PublicInbox/NNTP.pm
@@ -658,7 +658,8 @@ sub long_response ($$) {
 			$long_cb = undef;
 			res($self, '.');
 			out($self, " deferred[$fd] done - %0.6f", now() - $t0);
-			$self->requeue unless $self->{wbuf};
+			my $wbuf = $self->{wbuf};
+			$self->requeue unless $wbuf && @$wbuf;
 		}
 	};
 	$self->write($long_cb); # kick off!
-- 
EW


                 reply	other threads:[~2019-07-10  8:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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