From ad821f70a2488a91f2be1ac53cb2e64f50743989 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 28 Sep 2010 17:40:01 -0700 Subject: start using kgio library It removes the burden of byte slicing and setting file descriptor flags. In some cases, we can remove unnecessary peeraddr calls, too. --- lib/rainbows/fiber/io.rb | 22 ++++++++++++---------- lib/rainbows/fiber/rev.rb | 6 ++---- 2 files changed, 14 insertions(+), 14 deletions(-) (limited to 'lib/rainbows/fiber') 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 -- cgit v1.2.3-24-ge0c7