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 3/3] http: remove weaken usage, reduce anonsub capture scope
Date: Wed,  4 Jan 2017 11:20:51 +0000	[thread overview]
Message-ID: <20170104112051.6804-4-e@80x24.org> (raw)
In-Reply-To: <20170104112051.6804-1-e@80x24.org>

Avoiding weaken here is no more dangerous than the existing
circular refs (e.g. psgix.io) we create and manage throughout
the lifetime of the connection.  So, trust ourselves to maintain
the data structure properly and avoid triggering extra memory
usage.

While we're at it, avoid having anonymous subroutines capture
more variables than necessary to simplify reference auditing.
---
 lib/PublicInbox/HTTP.pm | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm
index 03ce4fe..3530f8b 100644
--- a/lib/PublicInbox/HTTP.pm
+++ b/lib/PublicInbox/HTTP.pm
@@ -16,7 +16,6 @@ use Fcntl qw(:seek);
 use Plack::HTTPParser qw(parse_http_request); # XS or pure Perl
 use HTTP::Status qw(status_message);
 use HTTP::Date qw(time2str);
-use Scalar::Util qw(weaken);
 use IO::Handle;
 use constant {
 	CHUNK_START => -1,   # [a-f0-9]+\r\n
@@ -237,12 +236,14 @@ sub next_request ($) {
 	}
 }
 
-sub response_done ($$) {
+sub response_done_cb ($$) {
 	my ($self, $alive) = @_;
-	my $env = $self->{env};
-	$self->{env} = undef;
-	$self->write("0\r\n\r\n") if $alive == 2;
-	$self->write(sub { $alive ? next_request($self) : $self->close });
+	sub {
+		my $env = $self->{env};
+		$self->{env} = undef;
+		$self->write("0\r\n\r\n") if $alive == 2;
+		$self->write(sub{$alive ? next_request($self) : $self->close});
+	}
 }
 
 sub getline_cb ($$$) {
@@ -283,10 +284,8 @@ sub getline_cb ($$$) {
 	$close->();
 }
 
-sub getline_response {
-	my ($self, $body, $write, $close) = @_;
-	$self->{forward} = $body;
-	weaken($self);
+sub getline_response ($$$) {
+	my ($self, $write, $close) = @_;
 	my $pull = $self->{pull} = sub { getline_cb($self, $write, $close) };
 	$pull->();
 }
@@ -294,15 +293,15 @@ sub getline_response {
 sub response_write {
 	my ($self, $env, $res) = @_;
 	my $alive = response_header_write($self, $env, $res);
-
+	my $close = response_done_cb($self, $alive);
 	my $write = $alive == 2 ? chunked_wcb($self) : identity_wcb($self);
-	my $close = sub { response_done($self, $alive) };
 	if (defined(my $body = $res->[2])) {
 		if (ref $body eq 'ARRAY') {
 			$write->($_) foreach @$body;
 			$close->();
 		} else {
-			getline_response($self, $body, $write, $close);
+			$self->{forward} = $body;
+			getline_response($self, $write, $close);
 		}
 	} else {
 		# this is returned to the calling application:
-- 
EW


      parent reply	other threads:[~2017-01-04 11:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-04 11:20 [PATCH 0/3] http: circular reference avoidance cleanups Eric Wong
2017-01-04 11:20 ` [PATCH 1/3] http: fix spelling error Eric Wong
2017-01-04 11:20 ` [PATCH 2/3] httpd/async: remove weaken usage Eric Wong
2017-01-04 11:20 ` Eric Wong [this message]

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=20170104112051.6804-4-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).