]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/HTTPD/Async.pm
www: use WwwStream for dumping thread and search views
[public-inbox.git] / lib / PublicInbox / HTTPD / Async.pm
index 47ba27d2e42979a2594aa23829ee0e02582be092..a936d9b5a0c6fc5e3988c88085437a1322b13333 100644 (file)
@@ -10,6 +10,7 @@ use strict;
 use warnings;
 use base qw(Danga::Socket);
 use fields qw(cb cleanup);
+use Scalar::Util qw(weaken);
 require PublicInbox::EvCleanup;
 
 sub new {
@@ -23,13 +24,19 @@ sub new {
        $self;
 }
 
+sub restart_read_cb ($) {
+       my ($self) = @_;
+       sub { $self->watch_read(1) }
+}
+
 sub async_pass {
        my ($self, $io, $fh, $bref) = @_;
-       my $restart_read = sub { $self->watch_read(1) };
        # In case the client HTTP connection ($io) dies, it
        # will automatically close this ($self) object.
        $io->{forward} = $self;
        $fh->write($$bref);
+       my $restart_read = restart_read_cb($self);
+       weaken($self);
        $self->{cb} = sub {
                my $r = sysread($self->{sock}, $$bref, 8192);
                if ($r) {
@@ -38,7 +45,9 @@ sub async_pass {
                                $self->watch_read(0);
                                $io->write($restart_read); # D::S::write
                        }
-                       return; # stay in watch_read
+                       # stay in watch_read, but let other clients
+                       # get some work done, too.
+                       return;
                } elsif (!defined $r) {
                        return if $!{EAGAIN} || $!{EINTR};
                }
@@ -65,7 +74,4 @@ sub close {
        PublicInbox::EvCleanup::asap($cleanup) if $cleanup;
 }
 
-# do not let ourselves be closed during graceful termination
-sub busy () { $_[0]->{cb} }
-
 1;