From 5d2284afdc2d4f4ff122394ae5fd78a32cb8c09e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 10 Jun 2011 23:54:47 +0000 Subject: [PATCH] runtime stack size reductions This reduces the size of `caller` by 5 frames, which should make backtraces easier-to-read, raising exceptions less expensive, and reduce GC runtime. --- bin/unicorn | 2 +- bin/unicorn_rails | 2 +- lib/unicorn.rb | 3 --- lib/unicorn/http_server.rb | 18 +++++++++++++----- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/bin/unicorn b/bin/unicorn index f8c20dc7..f476a334 100755 --- a/bin/unicorn +++ b/bin/unicorn @@ -117,4 +117,4 @@ if $DEBUG end Unicorn::Launcher.daemonize!(options) if rackup_opts[:daemonize] -Unicorn.run(app, options) +Unicorn::HttpServer.new(app, options).start.join diff --git a/bin/unicorn_rails b/bin/unicorn_rails index 58c232b2..860a9ce0 100755 --- a/bin/unicorn_rails +++ b/bin/unicorn_rails @@ -205,4 +205,4 @@ if rackup_opts[:daemonize] options[:pid] = "tmp/pids/unicorn.pid" Unicorn::Launcher.daemonize!(options) end -Unicorn.run(app, options) +Unicorn::HttpServer.new(app, options).start.join diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 8a5fdcc5..f9aa73a8 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -26,9 +26,6 @@ module Unicorn end # :stopdoc: - def self.run(app, options = {}) - Unicorn::HttpServer.new(app, options).start.join - end # This returns a lambda to pass in as the app, this does not "build" the # app (which we defer based on the outcome of "preload_app" in the diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb index 604854a4..66b137fb 100644 --- a/lib/unicorn/http_server.rb +++ b/lib/unicorn/http_server.rb @@ -497,14 +497,22 @@ class Unicorn::HttpServer end def spawn_missing_workers - (0...worker_processes).each do |worker_nr| + worker_nr = -1 + until (worker_nr += 1) == @worker_processes WORKERS.values.include?(worker_nr) and next worker = Worker.new(worker_nr, Unicorn::TmpIO.new) before_fork.call(self, worker) - WORKERS[fork { - after_fork_internal - worker_loop(worker) - }] = worker + if pid = fork + WORKERS[pid] = worker + else + begin + after_fork_internal + worker_loop(worker) + exit(0) + rescue Object + exit!(1) + end + end end end -- 2.11.4.GIT