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.rb | 36 +++++++++++------------------------- 1 file changed, 11 insertions(+), 25 deletions(-) (limited to 'lib/rainbows/writer_thread_pool.rb') diff --git a/lib/rainbows/writer_thread_pool.rb b/lib/rainbows/writer_thread_pool.rb index 67c8e83..558827f 100644 --- a/lib/rainbows/writer_thread_pool.rb +++ b/lib/rainbows/writer_thread_pool.rb @@ -19,30 +19,14 @@ module Rainbows::WriterThreadPool # :stopdoc: include Rainbows::Base + autoload :Client, 'rainbows/writer_thread_pool/client' @@nr = 0 @@q = nil - def async_write_body(qclient, body, range) - if body.respond_to?(:close) - Rainbows::SyncClose.new(body) do |body| - qclient.q << [ qclient.to_io, :body, body, range ] - end - else - qclient.q << [ qclient.to_io, :body, body, range ] - end - end - def process_client(client) # :nodoc: @@nr += 1 - super(Client.new(client, @@q[@@nr %= @@q.size])) - end - - def init_worker_process(worker) - super - self.class.__send__(:alias_method, :sync_write_body, :write_body) - Rainbows::WriterThreadPool.__send__( - :alias_method, :write_body, :async_write_body) + Client.new(client, @@q[@@nr %= @@q.size]).process_loop end def worker_loop(worker) # :nodoc: @@ -51,12 +35,16 @@ module Rainbows::WriterThreadPool qp = (1..worker_connections).map do |n| Rainbows::QueuePool.new(1) do |response| begin - io, arg1, arg2, arg3 = response - case arg1 - when :body then sync_write_body(io, arg2, arg3) - when :close then io.close unless io.closed? + io, arg, *rest = response + case arg + when String + io.kgio_write(arg) + when :close + warn "#{Thread.current} #{io} close" + io.close unless io.closed? else - io.write(arg1) + warn "#{Thread.current} #{io} #{arg}" + io.__send__(arg, *rest) end rescue => err Rainbows::Error.write(io, err) @@ -70,5 +58,3 @@ module Rainbows::WriterThreadPool end # :startdoc: end -# :enddoc: -require 'rainbows/writer_thread_pool/client' -- cgit v1.2.3-24-ge0c7