diff options
author | Eric Wong <normalperson@yhbt.net> | 2010-11-02 12:32:23 -0700 |
---|---|---|
committer | Eric Wong <normalperson@yhbt.net> | 2010-11-05 18:36:08 -0700 |
commit | 42747db815ad668b20849afb2a9dcdd1319713ae (patch) | |
tree | 6dcd7cb02f11bcfad40de6c72a9a6570df71c4d7 /lib/rainbows/timed_read.rb | |
parent | 427ef4a2953a4b2d34f7dd89566a0cb5ee6e734d (diff) | |
download | rainbows-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/timed_read.rb')
-rw-r--r-- | lib/rainbows/timed_read.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/rainbows/timed_read.rb b/lib/rainbows/timed_read.rb new file mode 100644 index 0000000..4a4e027 --- /dev/null +++ b/lib/rainbows/timed_read.rb @@ -0,0 +1,24 @@ +# -*- encoding: binary -*- +# :enddoc: +module Rainbows::TimedRead + G = Rainbows::G # :nodoc: + + def wait_readable + IO.select([self], nil, nil, G.kato) + end + + # used for reading headers (respecting keepalive_timeout) + def timed_read(buf) + expire = nil + begin + case rv = 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 + end +end |