From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.9 required=3.0 tests=ALL_TRUSTED,BAYES_00, T_RP_MATCHES_RCVD shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: spew@80x24.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id C9D2C633896 for ; Wed, 27 May 2015 21:11:34 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] lib/*: use monotonic clock for timeouts Date: Wed, 27 May 2015 21:11:34 +0000 Message-Id: <1432761094-16259-1-git-send-email-e@80x24.org> List-Id: 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