dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] lib/*: use monotonic clock for timeouts
@ 2015-05-06 22:29 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-05-06 22:29 UTC (permalink / raw)
  To: spew

The monotonic clock is preferred as it is guaranteed to be
continuous and not subject to jumps due to adjustments.

* lib/net/resolv.rb (now): new method to use monotonic clock
* lib/net/resolv.rb (request): use new now method
* lib/net/http.rb (now): new method to use monotonic clock
* lib/net/http.rb (begin_transport, end_transport): ditto
---
 lib/net/http.rb | 14 ++++++++++++--
 lib/resolv.rb   | 16 +++++++++++++---
 2 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/lib/net/http.rb b/lib/net/http.rb
index 2fc541c..58d4f65 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -1457,10 +1457,20 @@ module Net   #:nodoc:
       raise exception
     end
 
+    if defined?(Process::CLOCK_MONOTONIC)
+      def now
+        Process.clock_gettime(Process::CLOCK_MONOTONIC)
+      end
+    else
+      def now
+        Process.clock_gettime(Process::CLOCK_REALTIME)
+      end
+    end
+
     def begin_transport(req)
       if @socket.closed?
         connect
-      elsif @last_communicated && @last_communicated + @keep_alive_timeout < Time.now
+      elsif @last_communicated && @last_communicated + @keep_alive_timeout < now
         D 'Conn close because of keep_alive_timeout'
         @socket.close
         connect
@@ -1484,7 +1494,7 @@ module Net   #:nodoc:
         @socket.close
       elsif keep_alive?(req, res)
         D 'Conn keep-alive'
-        @last_communicated = Time.now
+        @last_communicated = now
       else
         D 'Conn close'
         @socket.close
diff --git a/lib/resolv.rb b/lib/resolv.rb
index 9083bd0..c56bf32 100644
--- a/lib/resolv.rb
+++ b/lib/resolv.rb
@@ -666,8 +666,18 @@ class Resolv
         @socks = nil
       end
 
+      if defined?(Process::CLOCK_MONOTONIC)
+        def now
+          Process.clock_gettime(Process::CLOCK_MONOTONIC)
+        end
+      else
+        def now
+          Process.clock_gettime(Process::CLOCK_REALTIME)
+        end
+      end
+
       def request(sender, tout)
-        start = Time.now
+        start = now
         timelimit = start + tout
         begin
           sender.send
@@ -676,7 +686,7 @@ class Resolv
           raise ResolvTimeout
         end
         while true
-          before_select = Time.now
+          before_select = now
           timeout = timelimit - before_select
           if timeout <= 0
             raise ResolvTimeout
@@ -687,7 +697,7 @@ class Resolv
             select_result = IO.select(@socks, nil, nil, timeout)
           end
           if !select_result
-            after_select = Time.now
+            after_select = now
             next if after_select < timelimit
             raise ResolvTimeout
           end
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] lib/*: use monotonic clock for timeouts
@ 2015-05-27 21:11 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-05-27 21:11 UTC (permalink / raw)
  To: spew

The monotonic clock is preferred as it is guaranteed to be
continuous and not subject to jumps due to adjustments.

* lib/net/resolv.rb (now): new method to use monotonic clock
* lib/net/resolv.rb (request): use new now method
* lib/net/http.rb (now): new method to use monotonic clock
* lib/net/http.rb (begin_transport, end_transport): ditto
---
 lib/net/http.rb | 4 ++--
 lib/resolv.rb   | 6 +++---
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/net/http.rb b/lib/net/http.rb
index 2fc541c..4c1c327 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -1460,7 +1460,7 @@ module Net   #:nodoc:
     def begin_transport(req)
       if @socket.closed?
         connect
-      elsif @last_communicated && @last_communicated + @keep_alive_timeout < Time.now
+      elsif @last_communicated && @last_communicated + @keep_alive_timeout < Process.clock_gettime(Process::CLOCK_MONOTONIC)
         D 'Conn close because of keep_alive_timeout'
         @socket.close
         connect
@@ -1484,7 +1484,7 @@ module Net   #:nodoc:
         @socket.close
       elsif keep_alive?(req, res)
         D 'Conn keep-alive'
-        @last_communicated = Time.now
+        @last_communicated = Process.clock_gettime(Process::CLOCK_MONOTONIC)
       else
         D 'Conn close'
         @socket.close
diff --git a/lib/resolv.rb b/lib/resolv.rb
index 9083bd0..ce36acf 100644
--- a/lib/resolv.rb
+++ b/lib/resolv.rb
@@ -667,7 +667,7 @@ class Resolv
       end
 
       def request(sender, tout)
-        start = Time.now
+        start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
         timelimit = start + tout
         begin
           sender.send
@@ -676,7 +676,7 @@ class Resolv
           raise ResolvTimeout
         end
         while true
-          before_select = Time.now
+          before_select = Process.clock_gettime(Process::CLOCK_MONOTONIC)
           timeout = timelimit - before_select
           if timeout <= 0
             raise ResolvTimeout
@@ -687,7 +687,7 @@ class Resolv
             select_result = IO.select(@socks, nil, nil, timeout)
           end
           if !select_result
-            after_select = Time.now
+            after_select = Process.clock_gettime(Process::CLOCK_MONOTONIC)
             next if after_select < timelimit
             raise ResolvTimeout
           end
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-05-27 21:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-06 22:29 [PATCH] lib/*: use monotonic clock for timeouts Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2015-05-27 21:11 Eric Wong

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).