about summary refs log tree commit homepage
path: root/lib/rainbows/fiber
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-09-28 17:40:01 -0700
committerEric Wong <normalperson@yhbt.net>2010-09-28 17:40:01 -0700
commitad821f70a2488a91f2be1ac53cb2e64f50743989 (patch)
tree6db4d334106360305ba3b66b643e8694f232fa10 /lib/rainbows/fiber
parent11c75ec06ce72cea0c760161dc01a196500aa293 (diff)
downloadrainbows-ad821f70a2488a91f2be1ac53cb2e64f50743989.tar.gz
It removes the burden of byte slicing and setting file
descriptor flags.  In some cases, we can remove unnecessary
peeraddr calls, too.
Diffstat (limited to 'lib/rainbows/fiber')
-rw-r--r--lib/rainbows/fiber/io.rb22
-rw-r--r--lib/rainbows/fiber/rev.rb6
2 files changed, 14 insertions, 14 deletions
diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb
index 4175eb0..f83b8b7 100644
--- a/lib/rainbows/fiber/io.rb
+++ b/lib/rainbows/fiber/io.rb
@@ -6,9 +6,9 @@ module Rainbows
     # #to_io method and gives users the illusion of a synchronous
     # interface that yields away from the current Fiber whenever
     # the underlying IO object cannot read or write
+    #
+    # TODO: subclass off IO and include Kgio::SocketMethods instead
     class IO < Struct.new(:to_io, :f)
-      include Rainbows::ByteSlice
-
       # :stopdoc:
       LOCALHOST = Unicorn::HttpRequest::LOCALHOST
 
@@ -17,9 +17,8 @@ module Rainbows
         to_io.write_nonblock(buf)
       end
 
-      # enough for Rainbows.addr
-      def peeraddr
-        to_io.respond_to?(:peeraddr) ? to_io.peeraddr : [ LOCALHOST ]
+      def kgio_addr
+        to_io.kgio_addr
       end
 
       # for wrapping output response bodies
@@ -58,11 +57,14 @@ module Rainbows
 
       def write(buf)
         begin
-          (w = to_io.write_nonblock(buf)) == buf.bytesize and return
-          buf = byte_slice(buf, w..-1)
-        rescue Errno::EAGAIN
-          wait_writable
-          retry
+          case rv = to_io.kgio_trywrite(buf)
+          when nil
+            return
+          when String
+            buf = rv
+          when Kgio::WaitWritable
+            wait_writable
+          end
         end while true
       end
 
diff --git a/lib/rainbows/fiber/rev.rb b/lib/rainbows/fiber/rev.rb
index 632b562..2c1abb7 100644
--- a/lib/rainbows/fiber/rev.rb
+++ b/lib/rainbows/fiber/rev.rb
@@ -54,7 +54,6 @@ module Rainbows::Fiber
       include Rainbows
       include Rainbows::Const
       include Rainbows::Response
-      include Rainbows::Acceptor
       FIO = Rainbows::Fiber::IO
 
       def to_io
@@ -73,7 +72,7 @@ module Rainbows::Fiber
 
       def on_readable
         return if G.cur >= MAX
-        c = accept(@io) and ::Fiber.new { process(c) }.resume
+        c = @io.kgio_tryaccept and ::Fiber.new { process(c) }.resume
       end
 
       def process(io)
@@ -82,7 +81,6 @@ module Rainbows::Fiber
         buf = client.read_timeout or return
         hp = HttpParser.new
         env = {}
-        remote_addr = Rainbows.addr(io)
 
         begin # loop
           buf << (client.read_timeout or return) until hp.headers(env, buf)
@@ -90,7 +88,7 @@ module Rainbows::Fiber
           env[CLIENT_IO] = client
           env[RACK_INPUT] = 0 == hp.content_length ?
                     HttpRequest::NULL_IO : TeeInput.new(client, env, hp, buf)
-          env[REMOTE_ADDR] = remote_addr
+          env[REMOTE_ADDR] = io.kgio_addr
           status, headers, body = APP.call(env.update(RACK_DEFAULTS))
 
           if 100 == status.to_i