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-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 6BE341FB0E for ; Mon, 8 Feb 2021 09:05:22 +0000 (UTC) From: Eric Wong To: meta@public-inbox.org Subject: [PATCH 06/13] ds: improve add_timer usability Date: Sun, 7 Feb 2021 23:05:14 -1000 Message-Id: <20210208090521.28909-7-e@80x24.org> In-Reply-To: <20210208090521.28909-1-e@80x24.org> References: <20210208090521.28909-1-e@80x24.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Packing args into an arrayref is awkward and we may be using this API more in lei. --- lib/PublicInbox/DS.pm | 10 +++++----- lib/PublicInbox/ExtSearchIdx.pm | 5 ++--- lib/PublicInbox/FakeInotify.pm | 4 ++-- lib/PublicInbox/Watch.pm | 19 ++++++++----------- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm index ec965abe..e5f36bc5 100644 --- a/lib/PublicInbox/DS.pm +++ b/lib/PublicInbox/DS.pm @@ -33,7 +33,7 @@ use PublicInbox::Syscall qw(:epoll); use PublicInbox::Tmpfile; use Errno qw(EAGAIN EINVAL); use Carp qw(carp); -our @EXPORT_OK = qw(now msg_more dwaitpid); +our @EXPORT_OK = qw(now msg_more dwaitpid add_timer); my $nextq; # queue for next_tick my $wait_pids; # list of [ pid, callback, callback_arg ] @@ -96,12 +96,12 @@ Add a timer to occur $seconds from now. $seconds may be fractional, but timers are not guaranteed to fire at the exact time you ask for. =cut -sub add_timer ($$;$) { - my ($secs, $coderef, $arg) = @_; +sub add_timer ($$;@) { + my ($secs, $coderef, @args) = @_; my $fire_time = now() + $secs; - my $timer = [$fire_time, $coderef, $arg]; + my $timer = [$fire_time, $coderef, @args]; if (!@Timers || $fire_time >= $Timers[-1][0]) { push @Timers, $timer; @@ -184,7 +184,7 @@ sub RunTimers { # Run expired timers while (@Timers && $Timers[0][0] <= $now) { my $to_run = shift(@Timers); - $to_run->[1]->($to_run->[2]); + $to_run->[1]->(@$to_run[2..$#$to_run]); } # timers may enqueue into nextq: diff --git a/lib/PublicInbox/ExtSearchIdx.pm b/lib/PublicInbox/ExtSearchIdx.pm index 9b7340df..a4b3bbd5 100644 --- a/lib/PublicInbox/ExtSearchIdx.pm +++ b/lib/PublicInbox/ExtSearchIdx.pm @@ -29,7 +29,7 @@ use PublicInbox::V2Writable; use PublicInbox::InboxWritable; use PublicInbox::ContentHash qw(content_hash); use PublicInbox::Eml; -use PublicInbox::DS qw(now); +use PublicInbox::DS qw(now add_timer); use DBI qw(:sql_types); # SQL_BLOB sub new { @@ -1027,8 +1027,7 @@ sub on_inbox_unlock { # called by PublicInbox::InboxIdle $pr->("indexing $ekey\n") if $pr; $self->idx_init($opt); sync_inbox($self, $self->{-watch_sync}, $ibx); - $self->{-commit_timer} //= PublicInbox::DS::add_timer( - $opt->{'commit-interval'} // 10, + $self->{-commit_timer} //= add_timer($opt->{'commit-interval'} // 10, \&_watch_commit, $self); } diff --git a/lib/PublicInbox/FakeInotify.pm b/lib/PublicInbox/FakeInotify.pm index 326b2391..25818e07 100644 --- a/lib/PublicInbox/FakeInotify.pm +++ b/lib/PublicInbox/FakeInotify.pm @@ -6,7 +6,7 @@ package PublicInbox::FakeInotify; use strict; use Time::HiRes qw(stat); -use PublicInbox::DS; +use PublicInbox::DS qw(add_timer); sub IN_MODIFY () { 0x02 } # match Linux inotify # my $IN_MOVED_TO = 0x80; # my $IN_CREATE = 0x100; @@ -66,7 +66,7 @@ sub read { sub poll_once { my ($obj) = @_; $obj->event_step; # PublicInbox::InboxIdle::event_step - PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $obj); + add_timer($poll_intvl, \&poll_once, $obj); } package PublicInbox::FakeInotify::Watch; diff --git a/lib/PublicInbox/Watch.pm b/lib/PublicInbox/Watch.pm index 185e5da8..1835fa0e 100644 --- a/lib/PublicInbox/Watch.pm +++ b/lib/PublicInbox/Watch.pm @@ -11,7 +11,7 @@ use PublicInbox::InboxWritable qw(eml_from_path); use PublicInbox::Filter::Base qw(REJECT); use PublicInbox::Spamcheck; use PublicInbox::Sigfd; -use PublicInbox::DS qw(now); +use PublicInbox::DS qw(now add_timer); use PublicInbox::MID qw(mids); use PublicInbox::ContentHash qw(content_hash); use PublicInbox::EOFpipe; @@ -590,8 +590,8 @@ sub watch_atfork_parent ($) { PublicInbox::DS::block_signals(); } -sub imap_idle_requeue ($) { # DS::add_timer callback - my ($self, $url_intvl) = @{$_[0]}; +sub imap_idle_requeue { # DS::add_timer callback + my ($self, $url_intvl) = @_; return if $self->{quit}; push @{$self->{idle_todo}}, $url_intvl; event_step($self); @@ -605,8 +605,7 @@ sub imap_idle_reap { # PublicInbox::DS::dwaitpid callback my ($url, $intvl) = @$url_intvl; return if $self->{quit}; warn "W: PID=$pid on $url died: \$?=$?\n" if $?; - PublicInbox::DS::add_timer(60, - \&imap_idle_requeue, [ $self, $url_intvl ]); + add_timer(60, \&imap_idle_requeue, $self, $url_intvl); } sub reap { # callback for EOFpipe @@ -700,8 +699,8 @@ sub watch_nntp_fetch_all ($$) { } } -sub poll_fetch_fork ($) { # DS::add_timer callback - my ($self, $intvl, $urls) = @{$_[0]}; +sub poll_fetch_fork { # DS::add_timer callback + my ($self, $intvl, $urls) = @_; return if $self->{quit}; pipe(my ($r, $w)) or die "pipe: $!"; my $oldset = watch_atfork_parent($self); @@ -736,8 +735,7 @@ sub poll_fetch_reap { warn "W: PID=$pid died: \$?=$?\n", map { "$_\n" } @$urls; } warn("I: will check $_ in ${intvl}s\n") for @$urls; - PublicInbox::DS::add_timer($intvl, \&poll_fetch_fork, - [$self, $intvl, $urls]); + add_timer($intvl, \&poll_fetch_fork, $self, $intvl, $urls); } sub watch_imap_init ($$) { @@ -1013,8 +1011,7 @@ sub watch { # main entry point watch_nntp_init($self, $poll) if $self->{nntp}; while (my ($intvl, $urls) = each %$poll) { # poll all URLs for a given interval sequentially - PublicInbox::DS::add_timer(0, \&poll_fetch_fork, - [$self, $intvl, $urls]); + add_timer(0, \&poll_fetch_fork, $self, $intvl, $urls); } watch_fs_init($self) if $self->{mdre}; PublicInbox::DS->SetPostLoopCallback(sub { !$self->quit_done });