From 42747db815ad668b20849afb2a9dcdd1319713ae Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 2 Nov 2010 12:32:23 -0700 Subject: avoid Errno::EAGAIN, harder 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. --- lib/rainbows/fiber/io.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'lib/rainbows/fiber/io.rb') 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 = "") -- cgit v1.2.3-24-ge0c7