From cd8a874d18fe01e11bb57b91186b6c9f712a4b3f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 10 Mar 2011 15:06:10 -0800 Subject: switch from IO#sendfile_nonblock to IO#trysendfile IO#trysendfile does not raise exceptions for common EAGAIN errors, making it far less expensive to use with the following concurrency models: * Coolio * CoolioFiberSpawn * Revactor * FiberSpawn * FiberPool This requires the new sendfile 1.1.0 RubyGem and removes support for the sendfile 1.0.0. All sendfile users must upgrade or be left without sendfile(2) support. IO#sendfile behaves the same if you're using a multi-threaded concurrency option, but we don't detect nor use it unless IO#trysendfile exists. --- lib/rainbows/fiber/body.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'lib/rainbows/fiber') diff --git a/lib/rainbows/fiber/body.rb b/lib/rainbows/fiber/body.rb index 872b1df..5b2c74b 100644 --- a/lib/rainbows/fiber/body.rb +++ b/lib/rainbows/fiber/body.rb @@ -5,19 +5,20 @@ # this is meant to be included _after_ Rainbows::Response::Body module Rainbows::Fiber::Body # :nodoc: - # the sendfile 1.0.0+ gem includes IO#sendfile_nonblock - if IO.method_defined?(:sendfile_nonblock) + # the sendfile 1.1.0+ gem includes IO#trysendfile + if IO.method_defined?(:trysendfile) def write_body_file(body, range) sock, n, body = to_io, nil, body_to_io(body) offset, count = range ? range : [ 0, body.stat.size ] - begin - offset += (n = sock.sendfile_nonblock(body, offset, count)) - rescue Errno::EAGAIN + case n = sock.trysendfile(body, offset, count) + when Integer + offset += n + return if 0 == (count -= n) + when :wait_writable kgio_wait_writable - retry - rescue EOFError - break - end while (count -= n) > 0 + else # nil + return + end while true ensure close_if_private(body) end -- cgit v1.2.3-24-ge0c7