unofficial mirror of meta@public-inbox.org
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: meta@public-inbox.org
Subject: [PATCH 06/13] ds: improve add_timer usability
Date: Sun,  7 Feb 2021 23:05:14 -1000	[thread overview]
Message-ID: <20210208090521.28909-7-e@80x24.org> (raw)
In-Reply-To: <20210208090521.28909-1-e@80x24.org>

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 });

  parent reply	other threads:[~2021-02-08  9:05 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-08  9:05 [PATCH 00/13] lei approxidate, startup fix, --alert Eric Wong
2021-02-08  9:05 ` [PATCHv2 01/13] lei q: improve remote mboxrd UX + MUA Eric Wong
2021-02-08  9:05 ` [PATCH 02/13] lei_xsearch: quiet Eml warnings from remote mboxrds Eric Wong
2021-02-08  9:05 ` [PATCH 03/13] lei q: SIGWINCH process group with the terminal Eric Wong
2021-02-08  9:05 ` [PATCH 04/13] lei q: support --alert=CMD for early MUA users Eric Wong
2021-02-08  9:05 ` [PATCH 05/13] tests: favor IPv6 Eric Wong
2021-02-08  9:05 ` Eric Wong [this message]
2021-02-08  9:05 ` [PATCH 07/13] lei: start_pager: drop COLUMNS default Eric Wong
2021-02-08  9:05 ` [PATCH 08/13] lei: avoid racing on unlink + bind + listen Eric Wong
2021-02-08  9:05 ` [PATCH 09/13] lei: drop BSD::Resource usage Eric Wong
2021-02-08  9:05 ` [PATCH 10/13] git: implement date_parse method Eric Wong
2021-02-08  9:05 ` [PATCH 11/13] lei q: use git approxidate with d:, dt: and rt: ranges Eric Wong
2021-02-10  9:59   ` [PATCH] search: fix argv handling of quoted phrases Eric Wong
2021-02-08  9:05 ` [PATCH 12/13] search: use one git-rev-parse process for all dates Eric Wong
2021-02-08  9:05 ` [PATCH 13/13] spawnpp: raise exception on E2BIG errors 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=20210208090521.28909-7-e@80x24.org \
    --to=e@80x24.org \
    --cc=meta@public-inbox.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).