]> Sergey Matveev's repositories - public-inbox.git/commitdiff
ds: add_timer: allow passing arg to callback.
authorEric Wong <e@yhbt.net>
Sat, 27 Jun 2020 10:03:44 +0000 (10:03 +0000)
committerEric Wong <e@yhbt.net>
Sun, 28 Jun 2020 22:27:20 +0000 (22:27 +0000)
This allows callers to avoid creating expensive closures.
We no longer pass the `$now' value to callers, as none of
the callers used it.

lib/PublicInbox/DS.pm
lib/PublicInbox/FakeInotify.pm
lib/PublicInbox/WatchMaildir.pm

index c46b20cba278e33de38b7e3adf4b8e59d3582afc..a3f2e76c16afcfd84901ca38492ad4eebbd1d6a5 100644 (file)
@@ -95,18 +95,18 @@ sub SetLoopTimeout {
     return $LoopTimeout = $_[1] + 0;
 }
 
-=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef ) >>
+=head2 C<< PublicInbox::DS::add_timer( $seconds, $coderef, $arg) >>
 
 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) = @_;
+sub add_timer ($$;$) {
+    my ($secs, $coderef, $arg) = @_;
 
     my $fire_time = now() + $secs;
 
-    my $timer = [$fire_time, $coderef];
+    my $timer = [$fire_time, $coderef, $arg];
 
     if (!@Timers || $fire_time >= $Timers[-1][0]) {
         push @Timers, $timer;
@@ -198,7 +198,7 @@ sub RunTimers {
     # Run expired timers
     while (@Timers && $Timers[0][0] <= $now) {
         my $to_run = shift(@Timers);
-        $to_run->[1]->($now) if $to_run->[1];
+        $to_run->[1]->($to_run->[2]);
     }
 
     # timers may enqueue into nextq:
index df63173f0830b01a73b9553fe051073af5d066c0..debd2d39ae58baf08deea2198547f64f99dc743b 100644 (file)
@@ -16,16 +16,14 @@ my $poll_intvl = 2; # same as Filesys::Notify::Simple
 
 sub poll_once {
        my ($self) = @_;
-       sub {
-               eval { $self->poll };
-               warn "E: FakeInotify->poll: $@\n" if $@;
-               PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
-       };
+       eval { $self->poll };
+       warn "E: FakeInotify->poll: $@\n" if $@;
+       PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self);
 }
 
 sub new {
        my $self = bless { watch => {} }, __PACKAGE__;
-       PublicInbox::DS::add_timer($poll_intvl, poll_once($self));
+       PublicInbox::DS::add_timer($poll_intvl, \&poll_once, $self);
        $self;
 }
 
index b82b51025e6e11c5219fc6287c9c1694197c23ee..f36aa20aa33f6a466bfb2431f0d5641dc8c45300 100644 (file)
@@ -549,8 +549,8 @@ sub watch_imap_fetch_all ($$) {
        }
 }
 
-sub imap_fetch_fork ($$$) {
-       my ($self, $intvl, $uris) = @_;
+sub imap_fetch_fork ($) { # DS::add_timer callback
+       my ($self, $intvl, $uris) = @{$_[0]};
        return if $self->{quit};
        watch_atfork_parent($self);
        defined(my $pid = fork) or die "fork: $!";
@@ -563,11 +563,6 @@ sub imap_fetch_fork ($$$) {
        PublicInbox::DS::dwaitpid($pid, \&imap_fetch_reap, $self);
 }
 
-sub imap_fetch_cb ($$$) {
-       my ($self, $intvl, $uris) = @_;
-       sub { imap_fetch_fork($self, $intvl, $uris) };
-}
-
 sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback
        my ($self, $pid) = @_;
        my $intvl_uris = delete $self->{poll_pids}->{$pid} or
@@ -579,7 +574,8 @@ sub imap_fetch_reap { # PublicInbox::DS::dwaitpid callback
                        map { $_->as_string."\n" } @$uris;
        }
        warn('I: will check ', $_->as_string, " in ${intvl}s\n") for @$uris;
-       PublicInbox::DS::add_timer($intvl, imap_fetch_cb($self, $intvl, $uris));
+       PublicInbox::DS::add_timer($intvl, \&imap_fetch_fork,
+                                       [$self, $intvl, $uris]);
 }
 
 sub watch_imap_init ($) {
@@ -625,7 +621,8 @@ sub watch_imap_init ($) {
 
        # poll all URIs for a given interval sequentially
        while (my ($intvl, $uris) = each %$poll) {
-               PublicInbox::DS::requeue(imap_fetch_cb($self, $intvl, $uris));
+               PublicInbox::DS::add_timer(0, \&imap_fetch_fork,
+                                               [$self, $intvl, $uris]);
        }
 }