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 15E881F46D for ; Wed, 25 Dec 2019 07:51:05 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 02/30] httpd/async: support passing arg to callbacks Date: Wed, 25 Dec 2019 07:50:36 +0000 Message-Id: <20191225075104.22184-3-e@80x24.org> In-Reply-To: <20191225075104.22184-1-e@80x24.org> References: <20191225075104.22184-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Another step towards removing anonymous subs to eliminate a possible source of memory leaks and high memory use. --- lib/PublicInbox/HTTPD/Async.pm | 6 ++++-- lib/PublicInbox/Qspawn.pm | 4 ++-- lib/PublicInbox/SolverGit.pm | 14 ++++---------- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/PublicInbox/HTTPD/Async.pm b/lib/PublicInbox/HTTPD/Async.pm index d5628ee8..70769bba 100644 --- a/lib/PublicInbox/HTTPD/Async.pm +++ b/lib/PublicInbox/HTTPD/Async.pm @@ -10,7 +10,7 @@ package PublicInbox::HTTPD::Async; use strict; use warnings; use base qw(PublicInbox::DS); -use fields qw(cb end); +use fields qw(cb arg end end_arg); use Errno qw(EAGAIN); use PublicInbox::Syscall qw(EPOLLIN EPOLLET); @@ -18,7 +18,7 @@ use PublicInbox::Syscall qw(EPOLLIN EPOLLET); # $io is a read-only pipe ($rpipe) for now, but may be a # bidirectional socket in the future. sub new { - my ($class, $io, $cb, $end) = @_; + my ($class, $io, $cb, $arg, $end, $end_arg) = @_; # no $io? call $cb at the top of the next event loop to # avoid recursion: @@ -32,7 +32,9 @@ sub new { IO::Handle::blocking($io, 0); $self->SUPER::new($io, EPOLLIN | EPOLLET); $self->{cb} = $cb; # initial read callback, later replaced by main_cb + $self->{arg} = $arg; # arg for $cb $self->{end} = $end; # like END {}, but only for this object + $self->{end_arg} = $end_arg; # arg for $end $self; } diff --git a/lib/PublicInbox/Qspawn.pm b/lib/PublicInbox/Qspawn.pm index 651fa390..c2856609 100644 --- a/lib/PublicInbox/Qspawn.pm +++ b/lib/PublicInbox/Qspawn.pm @@ -185,7 +185,7 @@ reread: ($rpipe) = @_; # popen_rd result if ($async) { # PublicInbox::HTTPD::Async->new($rpipe, $cb, $end) - $async = $async->($rpipe, $cb, $end); + $async = $async->($rpipe, $cb, undef, $end); # $cb will call ->async_pass or ->close } else { # generic PSGI $cb->() while $qx; @@ -297,7 +297,7 @@ sub psgi_return { ($rpipe) = @_; if ($async) { # PublicInbox::HTTPD::Async->new($rpipe, $cb, $end) - $async = $async->($rpipe, $cb, $end); + $async = $async->($rpipe, $cb, undef, $end); # $cb will call ->async_pass or ->close } else { # generic PSGI $cb->() while $rd_hdr; diff --git a/lib/PublicInbox/SolverGit.pm b/lib/PublicInbox/SolverGit.pm index eea59b6d..d59320bc 100644 --- a/lib/PublicInbox/SolverGit.pm +++ b/lib/PublicInbox/SolverGit.pm @@ -363,18 +363,13 @@ sub do_step ($) { } } -sub step_cb ($) { - my ($self) = @_; - sub { do_step($self) }; -} - sub next_step ($) { my ($self) = @_; # if outside of public-inbox-httpd, caller is expected to be - # looping step_cb, anyways + # looping do_step, anyways my $async = $self->{psgi_env}->{'pi-httpd.async'} or return; # PublicInbox::HTTPD::Async->new - $async->(undef, step_cb($self)); + $async->(undef, \&do_step, $self); } sub mark_found ($$$) { @@ -598,12 +593,11 @@ sub solve ($$$$$) { $self->{tmp} = File::Temp->newdir("solver.$oid_want-XXXXXXXX", TMPDIR => 1); dbg($self, "solving $oid_want ..."); - my $step_cb = step_cb($self); if (my $async = $env->{'pi-httpd.async'}) { # PublicInbox::HTTPD::Async->new - $async->(undef, $step_cb); + $async->(undef, \&do_step, $self); } else { - $step_cb->() while $self->{user_cb}; + do_step($self) while $self->{user_cb}; } }