From 3a96720e4e0f1d14599b9fae10e210110976e0c5 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 2 Aug 2010 01:24:25 +0000 Subject: revactor: implement sendfile and range support Due to the synchronous nature of Revactor, we can be certain sendfile won't overstep the userspace output buffering done by Rev. --- lib/rainbows/response.rb | 2 +- lib/rainbows/revactor.rb | 14 ++++++-------- lib/rainbows/revactor/body.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ t/t0023-sendfile-byte-range.sh | 2 +- 4 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 lib/rainbows/revactor/body.rb diff --git a/lib/rainbows/response.rb b/lib/rainbows/response.rb index 36ecbe2..3e196d1 100644 --- a/lib/rainbows/response.rb +++ b/lib/rainbows/response.rb @@ -39,7 +39,7 @@ module Rainbows::Response when :WriterThreadSpawn body_class = Rainbows::WriterThreadSpawn::MySocket range_class = Rainbows::HttpServer - when :EventMachine, :NeverBlock, :Revactor + when :EventMachine, :NeverBlock range_class = nil # :< end return if body_class.included_modules.include?(Body) diff --git a/lib/rainbows/revactor.rb b/lib/rainbows/revactor.rb index 8ec791d..10b7d3c 100644 --- a/lib/rainbows/revactor.rb +++ b/lib/rainbows/revactor.rb @@ -61,15 +61,12 @@ module Rainbows::Revactor if hp.headers? headers = HH.new(headers) - headers[CONNECTION] = if hp.keepalive? && G.alive - KEEP_ALIVE - else - env = false - CLOSE - end + range = make_range!(env, status, headers) and status = range.shift + env = false unless hp.keepalive? && G.alive + headers[CONNECTION] = env ? KEEP_ALIVE : CLOSE client.write(response_header(status, headers)) end - write_body(client, body) + write_body(client, body, range) end while env && env.clear && hp.reset.nil? rescue ::Revactor::TCP::ReadError rescue => e @@ -83,7 +80,8 @@ module Rainbows::Revactor # given a INT, QUIT, or TERM signal) def worker_loop(worker) #:nodoc: init_worker_process(worker) - self.class.__send__(:alias_method, :write_body, :write_body_each) + require 'rainbows/revactor/body' + self.class.__send__(:include, Rainbows::Revactor::Body) RD_ARGS[:timeout] = G.kato if G.kato > 0 nr = 0 limit = worker_connections diff --git a/lib/rainbows/revactor/body.rb b/lib/rainbows/revactor/body.rb new file mode 100644 index 0000000..1dd01f2 --- /dev/null +++ b/lib/rainbows/revactor/body.rb @@ -0,0 +1,42 @@ +# -*- encoding: binary -*- +# :enddoc: +module Rainbows::Revactor::Body + # TODO non-blocking splice(2) under Linux + ALIASES = { + :write_body_stream => :write_body_each + } + + if IO.method_defined?(:sendfile_nonblock) + def write_body_file(client, body, range) + sock = client.instance_variable_get(:@_io) + pfx = ::Revactor::TCP::Socket === client ? :tcp : :unix + 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 + # 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 + client.controller = Actor.current + client.__send__(:enable_write_watcher) + Actor.receive do |filter| + filter.when(write_complete) {} + filter.when(closed) { raise Errno::EPIPE } + end + retry + rescue EOFError + break + end while (count -= n) > 0 + end + else + ALIASES[:write_body] = :write_body_each + end + + def self.included(klass) + ALIASES.each do |new_method, orig_method| + klass.__send__(:alias_method, new_method, orig_method) + end + end +end diff --git a/t/t0023-sendfile-byte-range.sh b/t/t0023-sendfile-byte-range.sh index 63fceee..419d89a 100755 --- a/t/t0023-sendfile-byte-range.sh +++ b/t/t0023-sendfile-byte-range.sh @@ -10,7 +10,7 @@ ruby) ;; esac case $model in -EventMachine|NeverBlock|Revactor) +EventMachine|NeverBlock) t_info "skipping $T since it's not compatible with $model" exit 0 ;; -- cgit v1.2.3-24-ge0c7