]> git.ipfire.org Git - thirdparty/public-inbox.git/commitdiff
ds: hoist out close_non_busy
authorEric Wong <e@80x24.org>
Wed, 4 Oct 2023 03:49:14 +0000 (03:49 +0000)
committerEric Wong <e@80x24.org>
Wed, 4 Oct 2023 17:46:30 +0000 (17:46 +0000)
It's shared by both by lei and public-facing daemons in using
the ->busy callback.

lib/PublicInbox/DS.pm
lib/PublicInbox/Daemon.pm
lib/PublicInbox/LEI.pm

index c476311b9e1a92a89cf5f94ddeeb74b35c9450f0..ecfb581d47d9cade363565ada0f5c724fba3fe85 100644 (file)
@@ -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.
index 7546105e4d5c801bb4cbd3c753ec03cec8432735..5250610b52e931f65c0117c441ccf779a632d30d 100644 (file)
@@ -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";
index 368eee2665755143d23e59e6689589565f8f9acb..977a94c6df71ab8bdca66580c276630bec26894d 100644 (file)
@@ -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));
        });