about summary refs log tree commit homepage
path: root/lib/rainbows/fiber
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-02 12:32:23 -0700
committerEric Wong <normalperson@yhbt.net>2010-11-05 18:36:08 -0700
commit42747db815ad668b20849afb2a9dcdd1319713ae (patch)
tree6dcd7cb02f11bcfad40de6c72a9a6570df71c4d7 /lib/rainbows/fiber
parent427ef4a2953a4b2d34f7dd89566a0cb5ee6e734d (diff)
downloadrainbows-42747db815ad668b20849afb2a9dcdd1319713ae.tar.gz
Errno::EAGAIN is still a problem under Ruby 1.9.2, so try harder
to avoid it and use kgio methods.  Even when 1.9.3 is available,
kgio will still be faster as exceptions are slower than normal
return values.
Diffstat (limited to 'lib/rainbows/fiber')
-rw-r--r--lib/rainbows/fiber/base.rb13
-rw-r--r--lib/rainbows/fiber/io.rb29
-rw-r--r--lib/rainbows/fiber/rev/methods.rb19
3 files changed, 27 insertions, 34 deletions
diff --git a/lib/rainbows/fiber/base.rb b/lib/rainbows/fiber/base.rb
index b7c4ce5..69bf5d9 100644
--- a/lib/rainbows/fiber/base.rb
+++ b/lib/rainbows/fiber/base.rb
@@ -56,19 +56,6 @@ module Rainbows::Fiber::Base
     max.nil? || max > (now + 1) ? 1 : max - now
   end
 
-  def wait_headers_readable(client)
-    io = client.to_io
-    expire = nil
-    begin
-      return io.recv_nonblock(1, Socket::MSG_PEEK)
-    rescue Errno::EAGAIN
-      return if expire && expire < Time.now
-      expire ||= Time.now + G.kato
-      client.wait_readable
-      retry
-    end
-  end
-
   def process(client)
     G.cur += 1
     process_client(client)
diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb
index 3028eab..a9803ee 100644
--- a/lib/rainbows/fiber/io.rb
+++ b/lib/rainbows/fiber/io.rb
@@ -75,15 +75,28 @@ class Rainbows::Fiber::IO
   end
 
   # used for reading headers (respecting keepalive_timeout)
-  def read_timeout
+  def timed_read(buf)
     expire = nil
-    begin
-      return @to_io.read_nonblock(16384)
-    rescue Errno::EAGAIN
-      return if expire && expire < Time.now
-      expire ||= Time.now + G.kato
-      wait_readable
-    end while true
+    if @to_io.respond_to?(:kgio_tryread)
+      begin
+        case rv = @to_io.kgio_tryread(16384, buf)
+        when :wait_readable
+          return if expire && expire < Time.now
+          expire ||= Time.now + G.kato
+          wait_readable
+        else
+          return rv
+        end
+      end while true
+    else
+      begin
+        return @to_io.read_nonblock(16384, buf)
+      rescue Errno::EAGAIN
+        return if expire && expire < Time.now
+        expire ||= Time.now + G.kato
+        wait_readable
+      end while true
+    end
   end
 
   def readpartial(length, buf = "")
diff --git a/lib/rainbows/fiber/rev/methods.rb b/lib/rainbows/fiber/rev/methods.rb
index 64108a9..c09268f 100644
--- a/lib/rainbows/fiber/rev/methods.rb
+++ b/lib/rainbows/fiber/rev/methods.rb
@@ -3,7 +3,7 @@
 module Rainbows::Fiber::Rev::Methods
   class Watcher < Rev::IOWatcher
     def initialize(fio, flag)
-      @f = fio.f || Fiber.current
+      @f = Fiber.current
       super(fio, flag)
       attach(Rev::Loop.default)
     end
@@ -15,30 +15,23 @@ module Rainbows::Fiber::Rev::Methods
     alias on_writable on_readable
   end
 
-  def initialize(*args)
-    @f = Fiber.current
-    super(*args)
-    @r = @w = false
-  end
-
   def close
-    @w.detach if @w
-    @r.detach if @r
-    @r = @w = false
+    @w.detach if defined?(@w) && @w.attached?
+    @r.detach if defined?(@r) && @r.attached?
     super
   end
 
   def wait_writable
-    @w ||= Watcher.new(self, :w)
+    @w = Watcher.new(self, :w) unless defined?(@w)
     @w.enable unless @w.enabled?
     Fiber.yield
     @w.disable
   end
 
   def wait_readable
-    @r ||= Watcher.new(self, :r)
+    @r = Watcher.new(self, :r) unless defined?(@r)
     @r.enable unless @r.enabled?
-    KATO << @f
+    KATO << Fiber.current
     Fiber.yield
     @r.disable
   end