From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.0 required=3.0 tests=ALL_TRUSTED,BAYES_00 shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id CFB131F4BE for ; Mon, 24 Jun 2019 02:55:34 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 28/57] ds: pass $self to code references Date: Mon, 24 Jun 2019 02:52:29 +0000 Message-Id: <20190624025258.25592-29-e@80x24.org> In-Reply-To: <20190624025258.25592-1-e@80x24.org> References: <20190624025258.25592-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: We can reduce the amount of short-lived anonymous subs we create by passing $self to code references. --- lib/PublicInbox/DS.pm | 4 ++-- lib/PublicInbox/HTTP.pm | 9 ++++++++- lib/PublicInbox/HTTPD/Async.pm | 17 +++++++++-------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index 482710f7..7b87cd56 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -468,7 +468,7 @@ next_buf: } } else { #($ref eq 'CODE') { shift @$wbuf; - $bref->(); + $bref->($self); } } # while @$wbuf @@ -535,7 +535,7 @@ sub write { } return 0; } elsif ($ref eq 'CODE') { - $bref->(); + $bref->($self); return 1; } else { my $to_write = bytes::length($$bref); diff --git a/lib/PublicInbox/HTTP.pm b/lib/PublicInbox/HTTP.pm index c81aeacd..e132c610 100644 --- a/lib/PublicInbox/HTTP.pm +++ b/lib/PublicInbox/HTTP.pm @@ -247,7 +247,7 @@ sub response_done_cb ($$) { sub { my $env = delete $self->{env}; $self->write(\"0\r\n\r\n") if $alive == 2; - $self->write(sub{$alive ? next_request($self) : $self->close}); + $self->write($alive ? \&next_request : \&close); } } @@ -456,4 +456,11 @@ sub busy () { ($self->{rbuf} ne '' || $self->{env} || $self->{wbuf}); } +# fires after pending writes are complete: +sub restart_pass ($) { + $_[0]->{forward}->restart_read; # see PublicInbox::HTTPD::Async +} + +sub enqueue_restart_pass ($) { $_[0]->write(\&restart_pass) } + 1; diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm index e6df58eb..b46baeb2 100644 --- a/lib/PublicInbox/HTTPD/Async.pm +++ b/lib/PublicInbox/HTTPD/Async.pm @@ -34,23 +34,24 @@ sub new { sub restart_read ($) { $_[0]->watch(PublicInbox::DS::EPOLLIN()) } -# fires after pending writes are complete: -sub restart_read_cb ($) { - my ($self) = @_; - sub { restart_read($self) } -} - sub main_cb ($$$) { my ($http, $fh, $bref) = @_; sub { my ($self) = @_; my $r = sysread($self->{sock}, $$bref, 8192); if ($r) { - $fh->write($$bref); + $fh->write($$bref); # may call $http->close + if ($http->{sock}) { # !closed if ($http->{wbuf}) { + # HTTP client could not keep up, so + # stop reading and buffering. $self->watch(0); - $http->write(restart_read_cb($self)); + + # Tell the HTTP socket to restart us + # when HTTP client is done draining + # $http->{wbuf}: + $http->enqueue_restart_pass; } # stay in EPOLLIN, but let other clients # get some work done, too. -- EW