about summary refs log tree commit homepage
path: root/lib/rainbows.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2015-11-18 03:58:05 +0000
committerEric Wong <e@80x24.org>2015-11-21 01:43:29 +0000
commit0a10915fff80ff90bdef2a905dfdc589e815aefd (patch)
tree8692e5af9d367f57732dc985ba4c3d44af723fa6 /lib/rainbows.rb
parentdf169eb21fd571853bd388079d04e86a59298e9c (diff)
downloadrainbows-0a10915fff80ff90bdef2a905dfdc589e815aefd.tar.gz
The timeout (mis)feature in unicorn uses the monotonic clock
if available.  We must follow suit to avoid having our timeout
functionality completely broken.
Diffstat (limited to 'lib/rainbows.rb')
-rw-r--r--lib/rainbows.rb19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/rainbows.rb b/lib/rainbows.rb
index 0af5620..f23b387 100644
--- a/lib/rainbows.rb
+++ b/lib/rainbows.rb
@@ -75,8 +75,8 @@ module Rainbows
   end
 
   def self.tick
-    @worker.tick = Time.now.to_i
-    exit!(2) if @expire && Time.now >= @expire
+    @worker.tick = now.to_i
+    exit!(2) if @expire && now >= @expire
     @alive && @server.master_pid == Process.ppid or quit!
   end
 
@@ -88,7 +88,7 @@ module Rainbows
     unless @expire
       @alive = false
       Rainbows::HttpParser.quit
-      @expire = Time.now + (@server.timeout * 2.0)
+      @expire = now + (@server.timeout * 2.0)
       tmp = @readers.dup
       @readers.clear
       tmp.each { |s| s.close rescue nil }.clear
@@ -100,6 +100,19 @@ module Rainbows
     false
   end
 
+  # try to use the monotonic clock in Ruby >= 2.1, it is immune to clock
+  # offset adjustments and generates less garbage (Float vs Time object)
+  begin
+    Process.clock_gettime(Process::CLOCK_MONOTONIC)
+    def self.now
+      Process.clock_gettime(Process::CLOCK_MONOTONIC)
+    end
+  rescue NameError, NoMethodError
+    def self.now # Ruby <= 2.0
+      Rainbows.now
+    end
+  end
+
   autoload :Base, "rainbows/base"
   autoload :WriterThreadPool, "rainbows/writer_thread_pool"
   autoload :WriterThreadSpawn, "rainbows/writer_thread_spawn"