user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
* [PATCH 0/2] ds: some more timer/cleanup stuff
@ 2021-09-29 12:40 Eric Wong
  2021-09-29 12:40 ` [PATCH 1/2] ds: simplify idle time expiry, slightly Eric Wong
  2021-09-29 12:40 ` [PATCH 2/2] ds: drop ::later support Eric Wong
  0 siblings, 2 replies; 3+ messages in thread
From: Eric Wong @ 2021-09-29 12:40 UTC (permalink / raw)
  To: meta

deleting code makes me happy

Eric Wong (2):
  ds: simplify idle time expiry, slightly
  ds: drop ::later support

 lib/PublicInbox/DS.pm   | 43 +++++++++++++----------------------------
 lib/PublicInbox/IMAP.pm | 13 ++++++-------
 2 files changed, 19 insertions(+), 37 deletions(-)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH 1/2] ds: simplify idle time expiry, slightly
  2021-09-29 12:40 [PATCH 0/2] ds: some more timer/cleanup stuff Eric Wong
@ 2021-09-29 12:40 ` Eric Wong
  2021-09-29 12:40 ` [PATCH 2/2] ds: drop ::later support Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2021-09-29 12:40 UTC (permalink / raw)
  To: meta

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 {

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH 2/2] ds: drop ::later support
  2021-09-29 12:40 [PATCH 0/2] ds: some more timer/cleanup stuff Eric Wong
  2021-09-29 12:40 ` [PATCH 1/2] ds: simplify idle time expiry, slightly Eric Wong
@ 2021-09-29 12:40 ` Eric Wong
  1 sibling, 0 replies; 3+ messages in thread
From: Eric Wong @ 2021-09-29 12:40 UTC (permalink / raw)
  To: meta

add_uniq_timer seems sufficient, and we'll drop the last
user of ::later (IMAP) and switch to unique timers.
---
 lib/PublicInbox/DS.pm   | 19 ++-----------------
 lib/PublicInbox/IMAP.pm | 13 ++++++-------
 2 files changed, 8 insertions(+), 24 deletions(-)

diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index a25e3fe84857..37cd6087cafb 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -37,7 +37,6 @@ our @EXPORT_OK = qw(now msg_more dwaitpid add_timer add_uniq_timer);
 my %Stack;
 my $nextq; # queue for next_tick
 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);
@@ -78,11 +77,11 @@ sub Reset {
 		my @q = delete @Stack{keys %Stack};
 		for my $q (@q) { @$q = () }
 		$EXPMAP = undef;
-		$wait_pids = $later_q = $nextq = $ToClose = undef;
+		$wait_pids = $nextq = $ToClose = undef;
 		$_io = undef; # closes real $Epoll FD
 		$Epoll = undef; # may call DSKQXS::DESTROY
 	} while (@Timers || keys(%Stack) || $nextq || $wait_pids ||
-		$later_q || $ToClose || keys(%DescriptorMap) ||
+		$ToClose || keys(%DescriptorMap) ||
 		$PostLoopCallback || keys(%UniqTimer));
 
 	$reap_armed = undef;
@@ -298,7 +297,6 @@ sub EventLoop {
             $obj->event_step;
         }
     } while (PostEventLoop());
-    _run_later();
 }
 
 =head2 C<< CLASS->SetPostLoopCallback( CODEREF ) >>
@@ -658,19 +656,6 @@ sub dwaitpid ($;$$) {
 	}
 }
 
-sub _run_later () {
-	my $q = $later_q or return;
-	$later_q = undef;
-	$Stack{later_q} = $q;
-	$_->() for @$q;
-	delete $Stack{later_q};
-}
-
-sub later ($) {
-	push @$later_q, $_[0]; # autovivifies @$later_q
-	add_uniq_timer('later', 60, \&_run_later);
-}
-
 sub expire_old () {
 	my $cur = $EXPMAP or return;
 	$EXPMAP = undef;
diff --git a/lib/PublicInbox/IMAP.pm b/lib/PublicInbox/IMAP.pm
index 27013ea563c1..bc34f4feea1e 100644
--- a/lib/PublicInbox/IMAP.pm
+++ b/lib/PublicInbox/IMAP.pm
@@ -316,20 +316,19 @@ sub on_inbox_unlock {
 	}
 }
 
-# called every X minute(s) or so by PublicInbox::DS::later
-my $IDLERS = {};
-my $idle_timer;
+# called every minute or so by PublicInbox::DS::later
+my $IDLERS; # fileno($obj->{sock}) => PublicInbox::IMAP
 sub idle_tick_all {
 	my $old = $IDLERS;
-	$IDLERS = {};
+	$IDLERS = undef;
 	for my $i (values %$old) {
 		next if ($i->{wbuf} || !exists($i->{-idle_tag}));
 		$i->update_idle_time or next;
 		$IDLERS->{fileno($i->{sock})} = $i;
 		$i->write(\"* OK Still here\r\n");
 	}
-	$idle_timer = scalar keys %$IDLERS ?
-			PublicInbox::DS::later(\&idle_tick_all) : undef;
+	$IDLERS and
+		PublicInbox::DS::add_uniq_timer('idle', 60, \&idle_tick_all);
 }
 
 sub cmd_idle ($$) {
@@ -346,7 +345,7 @@ sub cmd_idle ($$) {
 		$ibx->subscribe_unlock($fd, $self);
 		$self->{imapd}->idler_start;
 	}
-	$idle_timer //= PublicInbox::DS::later(\&idle_tick_all);
+	PublicInbox::DS::add_uniq_timer('idle', 60, \&idle_tick_all);
 	$IDLERS->{$fd} = $self;
 	\"+ idling\r\n"
 }

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-29 12:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-29 12:40 [PATCH 0/2] ds: some more timer/cleanup stuff Eric Wong
2021-09-29 12:40 ` [PATCH 1/2] ds: simplify idle time expiry, slightly Eric Wong
2021-09-29 12:40 ` [PATCH 2/2] ds: drop ::later support Eric Wong

Code repositories for project(s) associated with this public 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).