]> Sergey Matveev's repositories - public-inbox.git/commitdiff
http|nntp: avoid recursion inside ->write
authorEric Wong <e@80x24.org>
Wed, 10 Jul 2019 06:13:59 +0000 (06:13 +0000)
committerEric Wong <e@80x24.org>
Wed, 10 Jul 2019 08:25:06 +0000 (08:25 +0000)
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
lib/PublicInbox/NNTP.pm

index 60b287c434274fc5ec4a8cc1732d179c1e1e007d..5afe167e7578f140fb568373864d1e30d58c985d 100644 (file)
@@ -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 $@;
index 6796a3c40396e542f088f89e654db4d60717eba5..f4208f878d9cd7e8a091609b25f22badfd001f65 100644 (file)
@@ -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!