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 88E281F934 for ; Wed, 29 Sep 2021 12:40:47 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 1/2] ds: simplify idle time expiry, slightly Date: Wed, 29 Sep 2021 07:40:46 -0500 Message-Id: <20210929124047.2128-2-e@80x24.org> In-Reply-To: <20210929124047.2128-1-e@80x24.org> References: <20210929124047.2128-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: 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 {