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/revactor/client/methods.rb | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'lib/rainbows/revactor') diff --git a/lib/rainbows/revactor/client/methods.rb b/lib/rainbows/revactor/client/methods.rb index e9b39a3..b2e1847 100644 --- a/lib/rainbows/revactor/client/methods.rb +++ b/lib/rainbows/revactor/client/methods.rb @@ -1,7 +1,7 @@ # -*- encoding: binary -*- # :enddoc: module Rainbows::Revactor::Client::Methods - if IO.method_defined?(:sendfile_nonblock) + if IO.method_defined?(:trysendfile) def write_body_file(body, range) body, client = body_to_io(body), @client sock = @client.instance_variable_get(:@_io) @@ -9,9 +9,11 @@ module Rainbows::Revactor::Client::Methods write_complete = T[:"#{pfx}_write_complete", client] closed = T[:"#{pfx}_closed", client] 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 # The @_write_buffer is empty at this point, trigger the # on_readable method which in turn triggers on_write_complete # even though nothing was written @@ -21,10 +23,9 @@ module Rainbows::Revactor::Client::Methods filter.when(write_complete) {} filter.when(closed) { raise Errno::EPIPE } end - 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