about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-06-27 10:03:44 +0000
committerEric Wong <e@yhbt.net>2020-06-28 22:27:20 +0000
commit62bb26d307a106d6c9b42e36b583fd04d90a4687 (patch)
tree5f647bcc38c4b05d842570fffaabb97b7be5a4bf
parentbb7b738bc49558f869a14a2eaecbc5e7f401de51 (diff)
downloadpublic-inbox-62bb26d307a106d6c9b42e36b583fd04d90a4687.tar.gz
This allows callers to avoid creating expensive closures.
We no longer pass the `$now' value to callers, as none of
the callers used it.
-rw-r--r--lib/PublicInbox/DS.pm10
-rw-r--r--lib/PublicInbox/FakeInotify.pm10
-rw-r--r--lib/PublicInbox/WatchMaildir.pm15
3 files changed, 15 insertions, 20 deletions
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index c46b20cb..a3f2e76c 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -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:
diff --git a/lib/PublicInbox/FakeInotify.pm b/lib/PublicInbox/FakeInotify.pm
index df63173f..debd2d39 100644
--- a/lib/PublicInbox/FakeInotify.pm
+++ b/lib/PublicInbox/FakeInotify.pm
@@ -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;
 }
 
diff --git a/lib/PublicInbox/WatchMaildir.pm b/lib/PublicInbox/WatchMaildir.pm
index b82b5102..f36aa20a 100644
--- a/lib/PublicInbox/WatchMaildir.pm
+++ b/lib/PublicInbox/WatchMaildir.pm
@@ -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]);
         }
 }