]> Sergey Matveev's repositories - public-inbox.git/commitdiff
ds: improve add_timer usability
authorEric Wong <e@80x24.org>
Mon, 8 Feb 2021 09:05:14 +0000 (23:05 -1000)
committerEric Wong <e@80x24.org>
Mon, 8 Feb 2021 22:07:44 +0000 (22:07 +0000)
Packing args into an arrayref is awkward and we may be using
this API more in lei.

lib/PublicInbox/DS.pm
lib/PublicInbox/ExtSearchIdx.pm
lib/PublicInbox/FakeInotify.pm
lib/PublicInbox/Watch.pm

index ec965abe89d4916da8aab84d6e72cb376d969ce1..e5f36bc5436434f006b081592e649b480513d273 100644 (file)
@@ -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:
index 9b7340df6806dd2a9ca1179d2fb214a01d0c0ebf..a4b3bbd5e57c3255920e89e21b5e95e5a3d6116d 100644 (file)
@@ -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);
 }
 
index 326b23913a79b433ff3d9ada460131a6f7dddd48..25818e071b3a15be5a9c145df802638e163f437c 100644 (file)
@@ -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;
index 185e5da8bd16842ececa54dbfd87855a2758faea..1835fa0eced4b45237113d1e1a84edeca230b282 100644 (file)
@@ -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 });