about summary refs log tree commit homepage
path: root/lib/PublicInbox/DS.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-06-24 02:52:04 +0000
committerEric Wong <e@80x24.org>2019-06-24 05:26:25 +0000
commit860c7e13d31390dc16c2cce813e4887f12e76731 (patch)
treeb24f266351b82ac70a180a71cb04fa43e84639f3 /lib/PublicInbox/DS.pm
parent8c619bae62a3d468505716d58d3559278883eee7 (diff)
downloadpublic-inbox-860c7e13d31390dc16c2cce813e4887f12e76731.tar.gz
All of our internal timing code should use monotonic clocks
for consistency against system clock adjustments.

This can be shared by our Daemon and NNTP packages.
Diffstat (limited to 'lib/PublicInbox/DS.pm')
-rw-r--r--lib/PublicInbox/DS.pm11
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/PublicInbox/DS.pm b/lib/PublicInbox/DS.pm
index 9e24ed78..e7db2034 100644
--- a/lib/PublicInbox/DS.pm
+++ b/lib/PublicInbox/DS.pm
@@ -17,10 +17,11 @@ package PublicInbox::DS;
 use strict;
 use bytes;
 use POSIX ();
-use Time::HiRes ();
 use IO::Handle qw();
 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
-
+use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
+use parent qw(Exporter);
+our @EXPORT_OK = qw(now);
 use warnings;
 
 use PublicInbox::Syscall qw(:epoll);
@@ -115,7 +116,7 @@ sub AddTimer {
     my $class = shift;
     my ($secs, $coderef) = @_;
 
-    my $fire_time = Time::HiRes::time() + $secs;
+    my $fire_time = now() + $secs;
 
     my $timer = bless [$fire_time, $coderef], "PublicInbox::DS::Timer";
 
@@ -195,11 +196,13 @@ sub FirstTimeEventLoop {
     }
 }
 
+sub now () { clock_gettime(CLOCK_MONOTONIC) }
+
 # runs timers and returns milliseconds for next one, or next event loop
 sub RunTimers {
     return $LoopTimeout unless @Timers;
 
-    my $now = Time::HiRes::time();
+    my $now = now();
 
     # Run expired timers
     while (@Timers && $Timers[0][0] <= $now) {