fiber/base: avoid negative sleep interval
authorEric Wong <normalperson@yhbt.net>
Fri, 24 Aug 2012 23:03:17 +0000 (24 23:03 +0000)
committerEric Wong <normalperson@yhbt.net>
Mon, 27 Aug 2012 20:07:04 +0000 (27 20:07 +0000)
Also clarify the code while we're at it.
Thanks to Lin Jen-Shin for pointing this out.

ref: http://mid.gmane.org/CAA2_N1unOXb7Z4Jr8oKoSLu266O9Ko4o=oWzAcMA1w3=9X74KA@mail.gmail.com

lib/rainbows/fiber/base.rb

index 00af214..e8f5b16 100644 (file)
@@ -51,8 +51,16 @@ module Rainbows::Fiber::Base
       end
     }
     fibs.each { |fib| fib.resume }
-    now = Time.now
-    max.nil? || max > (now + 1) ? 1 : max - now
+
+    max_sleep = 1.0 # wake up semi-frequently to prevent SIGKILL from master
+    if max
+      max -= Time.now
+      return 0 if max < 0.0
+      return max_sleep if max > max_sleep
+      max
+    else
+      max_sleep
+    end
   end
 
   def process(client)