From e21939d776673b2f8887adf7a5c64812b7d2e98e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 30 Dec 2010 08:33:15 +0000 Subject: globally refactor Range handling for responses Rack::Utils::HeaderHash is still very expensive in Rack 1.2, especially for simple things that we want to run as fast as possible with minimal interference. HeaderHash is unnecessary for most requests that do not send Content-Range in responses. --- lib/rainbows/writer_thread_pool/client.rb | 45 ++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'lib/rainbows/writer_thread_pool') diff --git a/lib/rainbows/writer_thread_pool/client.rb b/lib/rainbows/writer_thread_pool/client.rb index 3cc3335..526a623 100644 --- a/lib/rainbows/writer_thread_pool/client.rb +++ b/lib/rainbows/writer_thread_pool/client.rb @@ -4,6 +4,49 @@ # this is compatible with IO.select class Rainbows::WriterThreadPool::Client < Struct.new(:to_io, :q) include Rainbows::SocketProxy + include Rainbows::ProcessClient + + module Methods + def write_body_each(body) + q << [ to_io, :write_body_each, body ] + end + + def write_response_close(status, headers, body, alive) + to_io.instance_variable_set(:@hp, @hp) # XXX ugh + Rainbows::SyncClose.new(body) { |sync_body| + q << [ to_io, :write_response, status, headers, sync_body, alive ] + } + end + + if IO.respond_to?(:copy_stream) || IO.method_defined?(:sendfile_nonblock) + def write_response(status, headers, body, alive) + if body.respond_to?(:close) + write_response_close(status, headers, body, alive) + elsif body.respond_to?(:to_path) + write_response_path(status, headers, body, alive) + else + super + end + end + + def write_body_file(body, range) + q << [ to_io, :write_body_file, body, range ] + end + + def write_body_stream(body) + q << [ to_io, :write_body_stream, body ] + end + else # each-only body response + def write_response(status, headers, body, alive) + if body.respond_to?(:close) + write_response_close(status, headers, body, alive) + else + super + end + end + end # each-only body response + end # module Methods + include Methods def write(buf) q << [ to_io, buf ] @@ -14,6 +57,6 @@ class Rainbows::WriterThreadPool::Client < Struct.new(:to_io, :q) end def closed? - false + to_io.closed? end end -- cgit v1.2.3-24-ge0c7