From 7246d2f2d1601dbb5486ce7f9ddbebd1bb975b58 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 4 Feb 2011 20:39:41 -0800 Subject: bump required Unicorn dependency for Kgio We want to use the singleton methods in Kgio to reduce conditionals. --- lib/rainbows/epoll/response_pipe.rb | 7 +- lib/rainbows/error.rb | 8 +-- lib/rainbows/event_machine/response_chunk_pipe.rb | 12 ++-- lib/rainbows/event_machine/response_pipe.rb | 13 ++-- lib/rainbows/fiber/io.rb | 82 ++++++----------------- lib/rainbows/revactor/client.rb | 8 +-- rainbows.gemspec | 2 +- 7 files changed, 38 insertions(+), 94 deletions(-) diff --git a/lib/rainbows/epoll/response_pipe.rb b/lib/rainbows/epoll/response_pipe.rb index 08731b1..56d9a47 100644 --- a/lib/rainbows/epoll/response_pipe.rb +++ b/lib/rainbows/epoll/response_pipe.rb @@ -27,11 +27,6 @@ class Rainbows::Epoll::ResponsePipe end def tryread - io = @io - io.respond_to?(:kgio_tryread) and return io.kgio_tryread(16384, RBUF) - io.read_nonblock(16384, RBUF) - rescue Errno::EAGAIN - :wait_readable - rescue EOFError + Kgio.tryread(@io, 16384, RBUF) end end diff --git a/lib/rainbows/error.rb b/lib/rainbows/error.rb index 98de6ba..039af9d 100644 --- a/lib/rainbows/error.rb +++ b/lib/rainbows/error.rb @@ -7,13 +7,7 @@ module Rainbows::Error # if the socket is already closed or broken. We'll always ensure # the socket is closed at the end of this function def self.write(io, e) - if msg = response(e) - if io.respond_to?(:kgio_trywrite) - io.kgio_trywrite(msg) - else - io.write_nonblock(msg) - end - end + msg = response(e) and Kgio.trywrite(io, msg) rescue end diff --git a/lib/rainbows/event_machine/response_chunk_pipe.rb b/lib/rainbows/event_machine/response_chunk_pipe.rb index af9393a..be98086 100644 --- a/lib/rainbows/event_machine/response_chunk_pipe.rb +++ b/lib/rainbows/event_machine/response_chunk_pipe.rb @@ -9,17 +9,15 @@ module Rainbows::EventMachine::ResponseChunkPipe end def notify_readable - begin - data = @io.read_nonblock(16384, RBUF) + case data = Kgio.tryread(@io, 16384, RBUF) + when String @client.write("#{data.size.to_s(16)}\r\n") @client.write(data) @client.write("\r\n") - rescue Errno::EINTR - rescue Errno::EAGAIN - return - rescue EOFError - detach + when :wait_readable return + when nil + return detach end while true end end diff --git a/lib/rainbows/event_machine/response_pipe.rb b/lib/rainbows/event_machine/response_pipe.rb index 0ea1eae..1f7b08a 100644 --- a/lib/rainbows/event_machine/response_pipe.rb +++ b/lib/rainbows/event_machine/response_pipe.rb @@ -10,14 +10,13 @@ module Rainbows::EventMachine::ResponsePipe end def notify_readable - begin - @client.write(@io.read_nonblock(16384, RBUF)) - rescue Errno::EINTR - rescue Errno::EAGAIN - return - rescue EOFError - detach + case data = Kgio.tryread(@io, 16384, RBUF) + when String + @client.write(data) + when :wait_readable return + when nil + return detach end while true end diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb index ac243a4..49aef3d 100644 --- a/lib/rainbows/fiber/io.rb +++ b/lib/rainbows/fiber/io.rb @@ -45,78 +45,36 @@ class Rainbows::Fiber::IO end def write(buf) - if @to_io.respond_to?(:kgio_trywrite) - begin - case rv = @to_io.kgio_trywrite(buf) - when nil - return - when String - buf = rv - when :wait_writable - kgio_wait_writable - end - end while true - else - begin - (rv = @to_io.write_nonblock(buf)) == buf.bytesize and return - buf = byte_slice(buf, rv) - rescue Errno::EAGAIN - kgio_wait_writable - end while true - end - end - - def byte_slice(buf, start) # :nodoc: - buf.encoding == Encoding::BINARY or - buf = buf.dup.force_encoding(Encoding::BINARY) - buf.slice(start, buf.size) + case rv = Kgio.trywrite(buf) + when String + buf = rv + when :wait_writable + kgio_wait_writable + end until nil == rv end # used for reading headers (respecting keepalive_timeout) def timed_read(buf) expire = nil - 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 ||= read_expire - kgio_wait_readable - else - return rv - end - end while true + case rv = Kgio.tryread(@to_io, 16384, buf) + when :wait_readable + return if expire && expire < Time.now + expire ||= read_expire + kgio_wait_readable else - begin - return @to_io.read_nonblock(16384, buf) - rescue Errno::EAGAIN - return if expire && expire < Time.now - expire ||= read_expire - kgio_wait_readable - end while true - end + return rv + end while true end def readpartial(length, buf = "") - if @to_io.respond_to?(:kgio_tryread) - begin - rv = @to_io.kgio_tryread(length, buf) - case rv - when nil - raise EOFError, "end of file reached", [] - when :wait_readable - kgio_wait_readable - else - return rv - end - end while true + case rv = Kgio.tryread(@to_io, length, buf) + when nil + raise EOFError, "end of file reached", [] + when :wait_readable + kgio_wait_readable else - begin - return @to_io.read_nonblock(length, buf) - rescue Errno::EAGAIN - kgio_wait_readable - end while true - end + return rv + end while true end def kgio_read(*args) diff --git a/lib/rainbows/revactor/client.rb b/lib/rainbows/revactor/client.rb index 1aabf93..c587589 100644 --- a/lib/rainbows/revactor/client.rb +++ b/lib/rainbows/revactor/client.rb @@ -28,10 +28,6 @@ class Rainbows::Revactor::Client @client.write(buf) end - def write_nonblock(buf) # only used for errors - @client.instance_variable_get(:@_io).write_nonblock(buf) - end - def timed_read(buf2) buf2.replace(@client.read(*@rd_args)) end @@ -41,6 +37,10 @@ class Rainbows::Revactor::Client NULL_IO : IC.new(@ts = TeeSocket.new(@client), hp) end + def to_io + @client.instance_variable_get(:@_io) + end + def close @client.close @client = nil diff --git a/rainbows.gemspec b/rainbows.gemspec index 00bed2d..6e3ab5d 100644 --- a/rainbows.gemspec +++ b/rainbows.gemspec @@ -27,7 +27,7 @@ Gem::Specification.new do |s| s.add_dependency(%q, ['~> 1.1']) # we need Unicorn for the HTTP parser and process management - s.add_dependency(%q, ["~> 3.3"]) + s.add_dependency(%q, ["~> 3.4"]) s.add_development_dependency(%q, "~> 3.0.0") s.add_development_dependency(%q, "~> 1.5") -- cgit v1.2.3-24-ge0c7