dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <e@80x24.org>
To: spew@80x24.org
Subject: [PATCH] lib/*: use monotonic clock for timeouts
Date: Wed,  6 May 2015 22:29:32 +0000	[thread overview]
Message-ID: <1430951372-28813-1-git-send-email-e@80x24.org> (raw)

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


             reply	other threads:[~2015-05-06 22:29 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-06 22:29 Eric Wong [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-05-27 21:11 [PATCH] lib/*: use monotonic clock for timeouts Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1430951372-28813-1-git-send-email-e@80x24.org \
    --to=e@80x24.org \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).