From 3d147e9bcd8f99c94900a00181692c2a09c3c3c9 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 5 Oct 2010 07:54:13 +0000 Subject: Unicorn::Util.tmpio => Unicorn::TmpIO.new This is slightly shorter and hopefully easier to find. --- lib/unicorn.rb | 3 ++- lib/unicorn/app/exec_cgi.rb | 4 ++-- lib/unicorn/tee_input.rb | 2 +- lib/unicorn/tmpio.rb | 29 +++++++++++++++++++++++++++++ lib/unicorn/util.rb | 28 ---------------------------- 5 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 lib/unicorn/tmpio.rb diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 3ee827f..adb3a01 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -9,6 +9,7 @@ require 'unicorn/socket_helper' require 'unicorn/const' require 'unicorn/http_request' require 'unicorn/configurator' +require 'unicorn/tmpio' require 'unicorn/util' require 'unicorn/tee_input' require 'unicorn/http_response' @@ -548,7 +549,7 @@ module Unicorn def spawn_missing_workers (0...worker_processes).each do |worker_nr| WORKERS.values.include?(worker_nr) and next - worker = Worker.new(worker_nr, Unicorn::Util.tmpio) + worker = Worker.new(worker_nr, Unicorn::TmpIO.new) before_fork.call(self, worker) WORKERS[fork { ready_pipe.close if ready_pipe diff --git a/lib/unicorn/app/exec_cgi.rb b/lib/unicorn/app/exec_cgi.rb index 412c1d9..fea22f6 100644 --- a/lib/unicorn/app/exec_cgi.rb +++ b/lib/unicorn/app/exec_cgi.rb @@ -43,7 +43,7 @@ module Unicorn::App # Calls the app def call(env) - out, err = Unicorn::Util.tmpio, Unicorn::Util.tmpio + out, err = Unicorn::TmpIO.new, Unicorn::TmpIO.new inp = force_file_input(env) pid = fork { run_child(inp, out, err, env) } inp.close @@ -124,7 +124,7 @@ module Unicorn::App if inp.respond_to?(:size) && inp.size == 0 ::File.open('/dev/null', 'rb') else - tmp = Unicorn::Util.tmpio + tmp = Unicorn::TmpIO.new buf = inp.read(CHUNK_SIZE) begin diff --git a/lib/unicorn/tee_input.rb b/lib/unicorn/tee_input.rb index 7e508fc..0dbfff6 100644 --- a/lib/unicorn/tee_input.rb +++ b/lib/unicorn/tee_input.rb @@ -32,7 +32,7 @@ class Unicorn::TeeInput < Struct.new(:socket, :req, :parser, self.buf = request.buf self.len = parser.content_length self.tmp = len && len < @@client_body_buffer_size ? - StringIO.new("") : Unicorn::Util.tmpio + StringIO.new("") : Unicorn::TmpIO.new self.buf2 = "" if buf.size > 0 parser.filter_body(buf2, buf) and finalize_input diff --git a/lib/unicorn/tmpio.rb b/lib/unicorn/tmpio.rb new file mode 100644 index 0000000..cce89ac --- /dev/null +++ b/lib/unicorn/tmpio.rb @@ -0,0 +1,29 @@ +# -*- encoding: binary -*- +# :stopdoc: +require 'tmpdir' + +# some versions of Ruby had a broken Tempfile which didn't work +# well with unlinked files. This one is much shorter, easier +# to understand, and slightly faster. +class Unicorn::TmpIO < File + + # creates and returns a new File object. The File is unlinked + # immediately, switched to binary mode, and userspace output + # buffering is disabled + def self.new + fp = begin + open("#{Dir::tmpdir}/#{rand}", RDWR|CREAT|EXCL, 0600) + rescue Errno::EEXIST + retry + end + unlink(fp.path) + fp.binmode + fp.sync = true + fp + end + + # for easier env["rack.input"] compatibility with Rack <= 1.1 + def size + stat.size + end +end diff --git a/lib/unicorn/util.rb b/lib/unicorn/util.rb index e9dd57f..8ebdf05 100644 --- a/lib/unicorn/util.rb +++ b/lib/unicorn/util.rb @@ -1,19 +1,9 @@ # -*- encoding: binary -*- require 'fcntl' -require 'tmpdir' module Unicorn - class TmpIO < ::File - - # for easier env["rack.input"] compatibility - def size - # flush if sync - stat.size - end - end - module Util class << self @@ -78,24 +68,6 @@ module Unicorn nr end - - # creates and returns a new File object. The File is unlinked - # immediately, switched to binary mode, and userspace output - # buffering is disabled - def tmpio - fp = begin - TmpIO.open("#{Dir::tmpdir}/#{rand}", - File::RDWR|File::CREAT|File::EXCL, 0600) - rescue Errno::EEXIST - retry - end - File.unlink(fp.path) - fp.binmode - fp.sync = true - fp - end - end - end end -- cgit v1.2.3-24-ge0c7