From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-4.2 required=3.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF shortcircuit=no autolearn=ham autolearn_force=no version=3.4.6 Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id BDC671F565 for ; Wed, 4 Oct 2023 03:49:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=80x24.org; s=selector1; t=1696391373; bh=+XgTdbMQExkHL0vnDVOns8vR76mVpRPdiJpu5U9iEz0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=n6QidD030Gc51q/Ls4aFrcM2LmEIE0Sq1N8iNh3Ehj76GAIFPo+q9/AzBCkSYKm/Y lfours+ULnuUSfEyHgqs0/rputHz4BsgfVvP3LaYr8sOKxRVb5fiDp66UtAmAcVSgN 4MP0MBQaY55aH7bS+AMX6L5/bwlJ8LPVUzr6Jbk0= From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 02/21] ds: hoist out close_non_busy Date: Wed, 4 Oct 2023 03:49:14 +0000 Message-ID: <20231004034933.3343930-3-e@80x24.org> In-Reply-To: <20231004034933.3343930-1-e@80x24.org> References: <20231004034933.3343930-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: It's shared by both by lei and public-facing daemons in using the ->busy callback. --- lib/PublicInbox/DS.pm | 10 ++++++++++ lib/PublicInbox/Daemon.pm | 10 +--------- lib/PublicInbox/LEI.pm | 12 ++---------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index c476311b..ecfb581d 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -233,6 +233,16 @@ sub enqueue_reap () { $reap_armed //= requeue(\&reap_pids) } sub in_loop () { $in_loop } +# use inside @post_loop_do, returns number of busy clients +sub close_non_busy () { + my $n = 0; + for my $s (values %DescriptorMap) { + # close as much as possible, early as possible + ($s->busy ? ++$n : $s->close) if $s->can('busy'); + } + $n; +} + # Internal function: run the post-event callback, send read events # for pushed-back data, and close pending connections. returns 1 # if event loop should continue, or 0 to shut it all down. diff --git a/lib/PublicInbox/Daemon.pm b/lib/PublicInbox/Daemon.pm index 7546105e..5250610b 100644 --- a/lib/PublicInbox/Daemon.pm +++ b/lib/PublicInbox/Daemon.pm @@ -364,16 +364,8 @@ sub worker_quit { # $_[0] = signal name or number (unused) # drop idle connections and try to quit gracefully @PublicInbox::DS::post_loop_do = (sub { my ($dmap, undef) = @_; - my $n = 0; my $now = now(); - for my $s (values %$dmap) { - $s->can('busy') or next; - if ($s->busy) { - ++$n; - } else { # close as much as possible, early as possible - $s->close; - } - } + my $n = PublicInbox::DS::close_non_busy(); if ($n) { if (($warn + 5) < now()) { warn "$$ quitting, $n client(s) left\n"; diff --git a/lib/PublicInbox/LEI.pm b/lib/PublicInbox/LEI.pm index 368eee26..977a94c6 100644 --- a/lib/PublicInbox/LEI.pm +++ b/lib/PublicInbox/LEI.pm @@ -1367,16 +1367,8 @@ sub lazy_start { $quit->(); } return 1 if defined($path); - my $n = 0; - for my $s (values %$dmap) { - $s->can('busy') or next; - if ($s->busy) { - ++$n; - } else { - $s->close; - } - } - drop_all_stores() if !$n; # drop stores only if no clients + my $n = PublicInbox::DS::close_non_busy() or + drop_all_stores(); # drop stores only if no clients # returns true: continue, false: stop $n + scalar(keys(%$PublicInbox::DS::AWAIT_PIDS)); });