From ff0fc020fe30798e52d96cc11b445c76d9822422 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 3 Jul 2009 22:30:48 -0700 Subject: Favor Struct members to instance variables There's a small memory reduction to be had when forking oodles of processes and the Perl hacker in me still gets confused into thinking those are arrays... --- lib/unicorn/app/exec_cgi.rb | 47 +++++++++++++++--------------- lib/unicorn/app/inetd.rb | 58 ++++++++++++++++++------------------- lib/unicorn/app/old_rails/static.rb | 20 ++++++------- lib/unicorn/configurator.rb | 53 ++++++++++++++------------------- lib/unicorn/http_request.rb | 16 ++-------- 5 files changed, 86 insertions(+), 108 deletions(-) (limited to 'lib/unicorn') diff --git a/lib/unicorn/app/exec_cgi.rb b/lib/unicorn/app/exec_cgi.rb index 4d8e701..147b279 100644 --- a/lib/unicorn/app/exec_cgi.rb +++ b/lib/unicorn/app/exec_cgi.rb @@ -5,7 +5,7 @@ module Unicorn::App # This class is highly experimental (even more so than the rest of Unicorn) # and has never run anything other than cgit. - class ExecCgi + class ExecCgi < Struct.new(:args) CHUNK_SIZE = 16384 PASS_VARS = %w( @@ -32,12 +32,12 @@ module Unicorn::App # run Unicorn::App::ExecCgi.new("/path/to/cgit.cgi") # end def initialize(*args) - @args = args.dup - first = @args[0] or + self.args = args + first = args[0] or raise ArgumentError, "need path to executable" - first[0..0] == "/" or @args[0] = ::File.expand_path(first) - File.executable?(@args[0]) or - raise ArgumentError, "#{@args[0]} is not executable" + first[0..0] == "/" or args[0] = ::File.expand_path(first) + File.executable?(args[0]) or + raise ArgumentError, "#{args[0]} is not executable" end # Calls the app @@ -65,14 +65,14 @@ module Unicorn::App val = env[key] or next ENV[key] = val end - ENV['SCRIPT_NAME'] = @args[0] + ENV['SCRIPT_NAME'] = args[0] ENV['GATEWAY_INTERFACE'] = 'CGI/1.1' env.keys.grep(/^HTTP_/) { |key| ENV[key] = env[key] } a = IO.new(0).reopen(inp) b = IO.new(1).reopen(out) c = IO.new(2).reopen(err) - exec(*@args) + exec(*args) end # Extracts headers from CGI out, will change the offset of out. @@ -89,23 +89,24 @@ module Unicorn::App offset = 4 end offset += head.length - out.instance_variable_set('@unicorn_app_exec_cgi_offset', offset) - size -= offset # Allows +out+ to be used as a Rack body. - def out.each - sysseek(@unicorn_app_exec_cgi_offset) - - # don't use a preallocated buffer for sysread since we can't - # guarantee an actual socket is consuming the yielded string - # (or if somebody is pushing to an array for eventual concatenation - begin - yield(sysread(CHUNK_SIZE)) - rescue EOFError - return - end while true - end + out.instance_eval { class << self; self; end }.instance_eval { + define_method(:each) { |&blk| + sysseek(offset) + + # don't use a preallocated buffer for sysread since we can't + # guarantee an actual socket is consuming the yielded string + # (or if somebody is pushing to an array for eventual concatenation + begin + blk.call(sysread(CHUNK_SIZE)) + rescue EOFError + break + end while true + } + } + size -= offset prev = nil headers = Rack::Utils::HeaderHash.new head.split(/\r?\n/).each do |line| @@ -144,7 +145,7 @@ module Unicorn::App err.seek(0) dst = env['rack.errors'] pid = status.pid - dst.write("#{pid}: #{@args.inspect} status=#{status} stderr:\n") + dst.write("#{pid}: #{args.inspect} status=#{status} stderr:\n") err.each_line { |line| dst.write("#{pid}: #{line}") } dst.flush end diff --git a/lib/unicorn/app/inetd.rb b/lib/unicorn/app/inetd.rb index e22b308..580b456 100644 --- a/lib/unicorn/app/inetd.rb +++ b/lib/unicorn/app/inetd.rb @@ -4,65 +4,66 @@ # this class *must* be used with Rack::Chunked module Unicorn::App - class Inetd + class Inetd < Struct.new(:cmd) - class CatBody + class CatBody < Struct.new(:errors, :err_rd, :out_rd, :pid_map) def initialize(env, cmd) - @cmd = cmd - @input, @errors = env['rack.input'], env['rack.errors'] + self.errors = env['rack.errors'] in_rd, in_wr = IO.pipe - @err_rd, err_wr = IO.pipe - @out_rd, out_wr = IO.pipe + self.err_rd, err_wr = IO.pipe + self.out_rd, out_wr = IO.pipe - @cmd_pid = fork { + cmd_pid = fork { inp, out, err = (0..2).map { |i| IO.new(i) } inp.reopen(in_rd) out.reopen(out_wr) err.reopen(err_wr) - [ in_rd, in_wr, @err_rd, err_wr, @out_rd, out_wr ].each { |io| - io.close - } + [ in_rd, in_wr, err_rd, err_wr, out_rd, out_wr ].each { |i| i.close } exec(*cmd) } [ in_rd, err_wr, out_wr ].each { |io| io.close } - [ in_wr, @err_rd, @out_rd ].each { |io| io.binmode } + [ in_wr, err_rd, out_rd ].each { |io| io.binmode } in_wr.sync = true # Unfortunately, input here must be processed inside a seperate # thread/process using blocking I/O since env['rack.input'] is not # IO.select-able and attempting to make it so would trip Rack::Lint - @inp_pid = fork { - [ @err_rd, @out_rd ].each { |io| io.close } + inp_pid = fork { + input = env['rack.input'] + [ err_rd, out_rd ].each { |io| io.close } buf = Unicorn::Z.dup - # this is dependent on @input.read having readpartial semantics: - while @input.read(16384, buf) + # this is dependent on input.read having readpartial semantics: + while input.read(16384, buf) in_wr.write(buf) end in_wr.close } in_wr.close + self.pid_map = { + inp_pid => 'input streamer', + cmd_pid => cmd.inspect, + } end def each(&block) - buf = Unicorn::Z.dup begin - rd, = IO.select([@err_rd, @out_rd]) + rd, = IO.select([err_rd, out_rd]) rd && rd.first or next - if rd.include?(@err_rd) + if rd.include?(err_rd) begin - @errors.write(@err_rd.read_nonblock(16384, buf)) + errors.write(err_rd.read_nonblock(16384)) rescue Errno::EINTR rescue Errno::EAGAIN break end while true end - rd.include?(@out_rd) or next + rd.include?(out_rd) or next begin - yield @out_rd.read_nonblock(16384, buf) + yield out_rd.read_nonblock(16384) rescue Errno::EINTR rescue Errno::EAGAIN break @@ -75,15 +76,13 @@ module Unicorn::App end def close - @input = nil - [ [ @cmd.inspect, @cmd_pid ], [ 'input streamer', @inp_pid ] - ].each { |str, pid| + pid_map.each { |pid, str| begin pid, status = Process.waitpid2(pid) status.success? or - @errors.write("#{str}: #{status.inspect} (PID:#{pid})\n") + errors.write("#{str}: #{status.inspect} (PID:#{pid})\n") rescue Errno::ECHILD - @errors.write("Failed to reap #{str} (PID:#{pid})\n") + errors.write("Failed to reap #{str} (PID:#{pid})\n") end } end @@ -91,16 +90,15 @@ module Unicorn::App end def initialize(*cmd) - @cmd = cmd + self.cmd = cmd end def call(env) - expect = env[Unicorn::Const::HTTP_EXPECT] and - /\A100-continue\z/i =~ expect and + /\A100-continue\z/i =~ env[Unicorn::Const::HTTP_EXPECT] and return [ 100, {} , [] ] [ 200, { 'Content-Type' => 'application/octet-stream' }, - CatBody.new(env, @cmd) ] + CatBody.new(env, cmd) ] end end diff --git a/lib/unicorn/app/old_rails/static.rb b/lib/unicorn/app/old_rails/static.rb index 17c007c..51a0017 100644 --- a/lib/unicorn/app/old_rails/static.rb +++ b/lib/unicorn/app/old_rails/static.rb @@ -19,28 +19,28 @@ require 'rack/file' # This means that if you are using page caching it will actually work # with Unicorn and you should see a decent speed boost (but not as # fast as if you use a static server like nginx). -class Unicorn::App::OldRails::Static +class Unicorn::App::OldRails::Static < Struct.new(:app, :root, :file_server) FILE_METHODS = { 'GET' => true, 'HEAD' => true }.freeze REQUEST_METHOD = 'REQUEST_METHOD'.freeze REQUEST_URI = 'REQUEST_URI'.freeze PATH_INFO = 'PATH_INFO'.freeze def initialize(app) - @app = app - @root = "#{::RAILS_ROOT}/public" - @file_server = ::Rack::File.new(@root) + self.app = app + self.root = "#{::RAILS_ROOT}/public" + self.file_server = ::Rack::File.new(root) end def call(env) # short circuit this ASAP if serving non-file methods - FILE_METHODS.include?(env[REQUEST_METHOD]) or return @app.call(env) + FILE_METHODS.include?(env[REQUEST_METHOD]) or return app.call(env) # first try the path as-is path_info = env[PATH_INFO].chomp("/") - if File.file?("#@root/#{::Rack::Utils.unescape(path_info)}") + if File.file?("#{root}/#{::Rack::Utils.unescape(path_info)}") # File exists as-is so serve it up env[PATH_INFO] = path_info - return @file_server.call(env) + return file_server.call(env) end # then try the cached version: @@ -50,11 +50,11 @@ class Unicorn::App::OldRails::Static env[REQUEST_URI] =~ /^#{Regexp.escape(path_info)}(;[^\?]+)/ path_info << "#$1#{ActionController::Base.page_cache_extension}" - if File.file?("#@root/#{::Rack::Utils.unescape(path_info)}") + if File.file?("#{root}/#{::Rack::Utils.unescape(path_info)}") env[PATH_INFO] = path_info - return @file_server.call(env) + return file_server.call(env) end - @app.call(env) # call OldRails + app.call(env) # call OldRails end end if defined?(Unicorn::App::OldRails) diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb index bd0a198..0ecd0d5 100644 --- a/lib/unicorn/configurator.rb +++ b/lib/unicorn/configurator.rb @@ -14,14 +14,13 @@ module Unicorn # after_fork do |server,worker| # server.listen("127.0.0.1:#{9293 + worker.nr}") rescue nil # end - class Configurator + class Configurator < Struct.new(:set, :config_file) # The default logger writes its output to $stderr - DEFAULT_LOGGER = Logger.new($stderr) unless defined?(DEFAULT_LOGGER) + DEFAULT_LOGGER = Logger.new($stderr) # Default settings for Unicorn DEFAULTS = { :timeout => 60, - :listeners => [], :logger => DEFAULT_LOGGER, :worker_processes => 1, :after_fork => lambda { |server, worker| @@ -42,42 +41,32 @@ module Unicorn }, :pid => nil, :preload_app => false, - :stderr_path => nil, - :stdout_path => nil, } - attr_reader :config_file #:nodoc: - def initialize(defaults = {}) #:nodoc: - @set = Hash.new(:unset) + self.set = Hash.new(:unset) use_defaults = defaults.delete(:use_defaults) - @config_file = defaults.delete(:config_file) - @config_file.freeze - @set.merge!(DEFAULTS) if use_defaults + self.config_file = defaults.delete(:config_file) + set.merge!(DEFAULTS) if use_defaults defaults.each { |key, value| self.send(key, value) } reload end def reload #:nodoc: - instance_eval(File.read(@config_file)) if @config_file + instance_eval(File.read(config_file)) if config_file end def commit!(server, options = {}) #:nodoc: skip = options[:skip] || [] - @set.each do |key, value| + set.each do |key, value| value == :unset and next skip.include?(key) and next - setter = "#{key}=" - if server.respond_to?(setter) - server.send(setter, value) - else - server.instance_variable_set("@#{key}", value) - end + server.__send__("#{key}=", value) end end def [](key) # :nodoc: - @set[key] + set[key] end # sets object to the +new+ Logger-like object. The new logger-like @@ -89,7 +78,7 @@ module Unicorn raise ArgumentError, "logger=#{new} does not respond to method=#{m}" end - @set[:logger] = new + set[:logger] = new end # sets after_fork hook to a given block. This block will be called by @@ -151,7 +140,7 @@ module Unicorn "not numeric: timeout=#{seconds.inspect}" seconds >= 3 or raise ArgumentError, "too low: timeout=#{seconds.inspect}" - @set[:timeout] = seconds + set[:timeout] = seconds end # sets the current number of worker_processes to +nr+. Each worker @@ -161,7 +150,7 @@ module Unicorn "not an integer: worker_processes=#{nr.inspect}" nr >= 0 or raise ArgumentError, "not non-negative: worker_processes=#{nr.inspect}" - @set[:worker_processes] = nr + set[:worker_processes] = nr end # sets listeners to the given +addresses+, replacing or augmenting the @@ -172,7 +161,7 @@ module Unicorn def listeners(addresses) # :nodoc: Array === addresses or addresses = Array(addresses) addresses.map! { |addr| expand_addr(addr) } - @set[:listeners] = addresses + set[:listeners] = addresses end # adds an +address+ to the existing listener set. @@ -227,8 +216,8 @@ module Unicorn def listen(address, opt = {}) address = expand_addr(address) if String === address - Hash === @set[:listener_opts] or - @set[:listener_opts] = Hash.new { |hash,key| hash[key] = {} } + Hash === set[:listener_opts] or + set[:listener_opts] = Hash.new { |hash,key| hash[key] = {} } [ :backlog, :sndbuf, :rcvbuf ].each do |key| value = opt[key] or next Integer === value or @@ -239,11 +228,11 @@ module Unicorn TrueClass === value || FalseClass === value or raise ArgumentError, "not boolean: #{key}=#{value.inspect}" end - @set[:listener_opts][address].merge!(opt) + set[:listener_opts][address].merge!(opt) end - @set[:listeners] = [] unless Array === @set[:listeners] - @set[:listeners] << address + set[:listeners] = [] unless Array === set[:listeners] + set[:listeners] << address end # sets the +path+ for the PID file of the unicorn master process @@ -265,7 +254,7 @@ module Unicorn def preload_app(bool) case bool when TrueClass, FalseClass - @set[:preload_app] = bool + set[:preload_app] = bool else raise ArgumentError, "preload_app=#{bool.inspect} not a boolean" end @@ -298,7 +287,7 @@ module Unicorn else raise ArgumentError end - @set[var] = path + set[var] = path end def set_hook(var, my_proc, req_arity = 2) #:nodoc: @@ -314,7 +303,7 @@ module Unicorn else raise ArgumentError, "invalid type: #{var}=#{my_proc.inspect}" end - @set[var] = my_proc + set[var] = my_proc end # expands "unix:path/to/foo" to a socket relative to the current path diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb index 779cd32..b8df403 100644 --- a/lib/unicorn/http_request.rb +++ b/lib/unicorn/http_request.rb @@ -6,8 +6,6 @@ require 'unicorn/http11' module Unicorn class HttpRequest - attr_accessor :logger - # default parameters we merge into the request env for Rack handlers DEFAULTS = { "rack.errors" => $stderr, @@ -24,6 +22,9 @@ module Unicorn NULL_IO = StringIO.new(Z) LOCALHOST = '127.0.0.1'.freeze + def initialize + end + # Being explicitly single-threaded, we have certain advantages in # not having to worry about variables being clobbered :) BUFFER = ' ' * Const::CHUNK_SIZE # initial size, may grow @@ -31,10 +32,6 @@ module Unicorn PARSER = HttpParser.new PARAMS = Hash.new - def initialize(logger = Configurator::DEFAULT_LOGGER) - @logger = logger - end - # Does the majority of the IO processing. It has been written in # Ruby using about 8 different IO processing strategies. # @@ -74,13 +71,6 @@ module Unicorn data << socket.readpartial(Const::CHUNK_SIZE, BUFFER) PARSER.execute(PARAMS, data) and return handle_body(socket) end while true - rescue HttpParserError => e - @logger.error "HTTP parse error, malformed request " \ - "(#{PARAMS[Const::HTTP_X_FORWARDED_FOR] || - PARAMS[Const::REMOTE_ADDR]}): #{e.inspect}" - @logger.error "REQUEST DATA: #{data.inspect}\n---\n" \ - "PARAMS: #{PARAMS.inspect}\n---\n" - raise e end private -- cgit v1.2.3-24-ge0c7