about summary refs log tree commit homepage
path: root/lib/rainbows/fiber
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-27 03:56:30 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-27 04:05:18 +0000
commit40c22b3f0a2ace6e4b51a39624ac5ed5ee3d0162 (patch)
treeb4a5d4b5aa8bbec9851bbe77a5e524d614e30856 /lib/rainbows/fiber
parentabb3f7e057bd5fb0aa97cae1410ce2f55ba12b9c (diff)
downloadrainbows-40c22b3f0a2ace6e4b51a39624ac5ed5ee3d0162.tar.gz
We can use the same interface as Rainbows::Fiber.sleep to avoid
blindly waking up readers at ever scheduler invocation.
Diffstat (limited to 'lib/rainbows/fiber')
-rw-r--r--lib/rainbows/fiber/base.rb13
-rw-r--r--lib/rainbows/fiber/io.rb4
-rw-r--r--lib/rainbows/fiber/io/methods.rb6
3 files changed, 14 insertions, 9 deletions
diff --git a/lib/rainbows/fiber/base.rb b/lib/rainbows/fiber/base.rb
index ccfda96..b693451 100644
--- a/lib/rainbows/fiber/base.rb
+++ b/lib/rainbows/fiber/base.rb
@@ -18,11 +18,10 @@ module Rainbows::Fiber::Base
   # for one second (returned by the schedule_sleepers method) which
   # will cause it.
   def schedule(&block)
-    ret = begin
+    begin
       G.tick
-      RD.compact.each { |c| c.f.resume } # attempt to time out idle clients
       t = schedule_sleepers
-      select(RD.compact.concat(LISTENERS), WR.compact, nil, t) or return
+      ret = select(RD.compact.concat(LISTENERS), WR.compact, nil, t)
     rescue Errno::EINTR
       retry
     rescue Errno::EBADF, TypeError
@@ -30,15 +29,15 @@ module Rainbows::Fiber::Base
       raise
     end or return
 
-    # active writers first, then _all_ readers for keepalive timeout
-    ret[1].concat(RD.compact).each { |c| c.f.resume }
+    # active writers first, then readers
+    ret[1].concat(RD.compact & ret[0]).each { |c| c.f.resume }
 
     # accept is an expensive syscall, filter out listeners we don't want
     (ret[0] & LISTENERS).each(&block)
   end
 
-  # wakes up any sleepers that need to be woken and
-  # returns an interval to IO.select on
+  # wakes up any sleepers or keepalive-timeout violators that need to be
+  # woken and returns an interval to IO.select on
   def schedule_sleepers
     max = nil
     now = Time.now
diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb
index e96f4de..94996ee 100644
--- a/lib/rainbows/fiber/io.rb
+++ b/lib/rainbows/fiber/io.rb
@@ -82,7 +82,7 @@ class Rainbows::Fiber::IO
         case rv = @to_io.kgio_tryread(16384, buf)
         when :wait_readable
           return if expire && expire < Time.now
-          expire ||= Time.now + G.kato
+          expire ||= read_expire
           kgio_wait_readable
         else
           return rv
@@ -93,7 +93,7 @@ class Rainbows::Fiber::IO
         return @to_io.read_nonblock(16384, buf)
       rescue Errno::EAGAIN
         return if expire && expire < Time.now
-        expire ||= Time.now + G.kato
+        expire ||= read_expire
         kgio_wait_readable
       end while true
     end
diff --git a/lib/rainbows/fiber/io/methods.rb b/lib/rainbows/fiber/io/methods.rb
index 6c4d44d..29c9854 100644
--- a/lib/rainbows/fiber/io/methods.rb
+++ b/lib/rainbows/fiber/io/methods.rb
@@ -8,8 +8,13 @@
 module Rainbows::Fiber::IO::Methods
   RD = Rainbows::Fiber::RD
   WR = Rainbows::Fiber::WR
+  ZZ = Rainbows::Fiber::ZZ
   attr_accessor :f
 
+  def read_expire
+    ZZ[Fiber.current] = super
+  end
+
   # for wrapping output response bodies
   def each(&block)
     if buf = kgio_read(16384)
@@ -30,6 +35,7 @@ module Rainbows::Fiber::IO::Methods
     @f = Fiber.current
     RD[fd] = self
     Fiber.yield
+    ZZ.delete @f
     RD[fd] = nil
   end