From 14ce889cc12628405b0f6a1842b7e1ad09ca4b7b Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 27 Jun 2009 15:58:45 -0700 Subject: Make TeeInput easier to use The complexity of making the object persistent isn't worth the potential performance gain here. --- lib/unicorn.rb | 2 +- lib/unicorn/http_request.rb | 3 +-- lib/unicorn/tee_input.rb | 24 +++++------------------- 3 files changed, 7 insertions(+), 22 deletions(-) diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 13da564..343f762 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -1,4 +1,5 @@ require 'fcntl' +require 'tempfile' require 'unicorn/socket_helper' autoload :Rack, 'rack' @@ -471,7 +472,6 @@ module Unicorn worker.tempfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) @after_fork.call(self, worker) # can drop perms @timeout /= 2.0 # halve it for select() - HttpRequest::TEE.setup build_app! unless @preload_app end diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb index d9cb8b2..e25517b 100644 --- a/lib/unicorn/http_request.rb +++ b/lib/unicorn/http_request.rb @@ -27,7 +27,6 @@ module Unicorn } NULL_IO = StringIO.new(Z) - TEE = TeeInput.new DECHUNKER = ChunkedReader.new LOCALHOST = '127.0.0.1'.freeze @@ -104,7 +103,7 @@ module Unicorn end end - inp = TEE.reopen(socket, length, body) + inp = TeeInput.new(socket, length, body) DEFAULTS[Const::STREAM_INPUT] ? inp : inp.consume else NULL_IO.closed? ? NULL_IO.reopen(Z) : NULL_IO diff --git a/lib/unicorn/tee_input.rb b/lib/unicorn/tee_input.rb index 9e66837..ac9638d 100644 --- a/lib/unicorn/tee_input.rb +++ b/lib/unicorn/tee_input.rb @@ -15,30 +15,16 @@ require 'tempfile' module Unicorn class TeeInput - def initialize - @rd = @wr = @size = @input = nil - setup - end - - def setup - @tmp = tmp = Tempfile.new(nil) - @rd.close if @rd - @rd = File.open(tmp.path, 'wb+') - @wr.close if @wr - @wr = File.open(tmp.path, 'wb') + def initialize(input, size = nil, buffer = nil) + @wr = Tempfile.new(nil) + @wr.binmode + @rd = File.open(@wr.path, 'rb') + @wr.unlink @rd.sync = @wr.sync = true - tmp.close! - end - - def reopen(input, size = nil, buffer = nil) - @rd.seek(0) - @wr.seek(0) - @rd.truncate(0) # truncate read to flush luserspace read buffers @wr.write(buffer) if buffer @input = input @size = size # nil if chunked - self end def consume -- cgit v1.2.3-24-ge0c7