user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 1/2] ds: simplify idle time expiry, slightly
Date: Wed, 29 Sep 2021 07:40:46 -0500	[thread overview]
Message-ID: <20210929124047.2128-2-e@80x24.org> (raw)
In-Reply-To: <20210929124047.2128-1-e@80x24.org>

While it doesn't look like $EXPMAP can be populated in
non-obvious ways via ->DESTROY, it still makes sense to keep it
close to some of our other code around cleanup to reduce
the likelyhood of subtle bugs in case semantics change..
---
 lib/PublicInbox/DS.pm | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index c89c7b8b40f4..a25e3fe84857 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -40,7 +40,7 @@ my $wait_pids; # list of [ pid, callback, callback_arg ]
 my $later_q; # list of callbacks to run at some later interval
 my $EXPMAP; # fd -> idle_time
 our $EXPTIME = 180; # 3 minutes
-my ($reap_armed, $exp_timer);
+my ($reap_armed);
 my $ToClose; # sockets to close when event loop is done
 our (
      %DescriptorMap,             # fd (num) -> PublicInbox::DS object
@@ -77,7 +77,7 @@ sub Reset {
 		# we may be iterating inside one of these on our stack
 		my @q = delete @Stack{keys %Stack};
 		for my $q (@q) { @$q = () }
-		$EXPMAP = {};
+		$EXPMAP = undef;
 		$wait_pids = $later_q = $nextq = $ToClose = undef;
 		$_io = undef; # closes real $Epoll FD
 		$Epoll = undef; # may call DSKQXS::DESTROY
@@ -85,7 +85,7 @@ sub Reset {
 		$later_q || $ToClose || keys(%DescriptorMap) ||
 		$PostLoopCallback || keys(%UniqTimer));
 
-	$reap_armed = $exp_timer = undef;
+	$reap_armed = undef;
 	$LoopTimeout = -1;  # no timeout by default
 }
 
@@ -672,27 +672,25 @@ sub later ($) {
 }
 
 sub expire_old () {
-	my $now = now();
-	my $exp = $EXPTIME;
-	my $old = $now - $exp;
-	my %new;
-	while (my ($fd, $idle_at) = each %$EXPMAP) {
+	my $cur = $EXPMAP or return;
+	$EXPMAP = undef;
+	my $old = now() - $EXPTIME;
+	while (my ($fd, $idle_at) = each %$cur) {
 		if ($idle_at < $old) {
 			my $ds_obj = $DescriptorMap{$fd};
-			$new{$fd} = $idle_at if !$ds_obj->shutdn;
+			$EXPMAP->{$fd} = $idle_at if !$ds_obj->shutdn;
 		} else {
-			$new{$fd} = $idle_at;
+			$EXPMAP->{$fd} = $idle_at;
 		}
 	}
-	$EXPMAP = \%new;
-	$exp_timer = scalar(keys %new) ? later(\&expire_old) : undef;
+	add_uniq_timer('expire', 60, \&expire_old) if $EXPMAP;
 }
 
 sub update_idle_time {
 	my ($self) = @_;
 	my $sock = $self->{sock} or return;
 	$EXPMAP->{fileno($sock)} = now();
-	$exp_timer //= later(\&expire_old);
+	add_uniq_timer('expire', 60, \&expire_old);
 }
 
 sub not_idle_long {

  reply	other threads:[~2021-09-29 12:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-29 12:40 [PATCH 0/2] ds: some more timer/cleanup stuff Eric Wong
2021-09-29 12:40 ` Eric Wong [this message]
2021-09-29 12:40 ` [PATCH 2/2] ds: drop ::later support Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://public-inbox.org/README

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210929124047.2128-2-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    --subject='Re: [PATCH 1/2] ds: simplify idle time expiry, slightly' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Code repositories for project(s) associated with this inbox:

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).