From bb3d776d8c07f2492ec5a3ef22184f5f59e116c7 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 28 May 2009 11:40:33 -0700 Subject: Make our HttpRequest object a global constant This should be faster/cheaper than using an instance variable since it's accessed in a critical code path. Unicorn was never designed to be reentrant or thread-safe at all, either. --- lib/unicorn.rb | 7 +++++-- lib/unicorn/http_request.rb | 4 +++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 044fed2..d56cb6f 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -41,6 +41,9 @@ module Unicorn # signal queue used for self-piping SIG_QUEUE = [] + # constant lookups are faster and we're single-threaded/non-reentrant + REQUEST = HttpRequest.new + # We populate this at startup so we can figure out how to reexecute # and upgrade the currently running instance of Unicorn START_CTX = { @@ -71,7 +74,6 @@ module Unicorn @config = Configurator.new(options.merge(:use_defaults => true)) @listener_opts = {} @config.commit!(self, :skip => [:listeners, :pid]) - @request = HttpRequest.new(@logger) @orig_app = app end @@ -424,7 +426,7 @@ module Unicorn # once a client is accepted, it is processed in its entirety here # in 3 easy steps: read request, call app, write app response def process_client(client) - HttpResponse.write(client, @app.call(@request.read(client))) + HttpResponse.write(client, @app.call(REQUEST.read(client))) # if we get any error, try to write something back to the client # assuming we haven't closed the socket, but don't get hung up # if the socket is already closed or broken. We'll always ensure @@ -577,6 +579,7 @@ module Unicorn @config.commit!(self) kill_each_worker(:QUIT) Unicorn::Util.reopen_logs + REQUEST.logger = @logger @app = @orig_app build_app! if @preload_app logger.info "done reloading config_file=#{@config.config_file}" diff --git a/lib/unicorn/http_request.rb b/lib/unicorn/http_request.rb index 368305f..d7078a3 100644 --- a/lib/unicorn/http_request.rb +++ b/lib/unicorn/http_request.rb @@ -12,6 +12,8 @@ module Unicorn # class HttpRequest + attr_accessor :logger + # default parameters we merge into the request env for Rack handlers DEFAULTS = { "rack.errors" => $stderr, @@ -36,7 +38,7 @@ module Unicorn PARSER = HttpParser.new PARAMS = Hash.new - def initialize(logger) + def initialize(logger = Configurator::DEFAULT_LOGGER) @logger = logger end -- cgit v1.2.3-24-ge0c7