From 9a59450a8f8cb415fa11ddbf5bf375c9cb7829d3 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sat, 21 Mar 2009 13:31:55 -0700 Subject: Simplify code for sleeping/waking up the master Only sleep if our signal queue is empty. Remove redundant exception handling and go back to just consuming the entire pipe since that's more efficient if we're slammed with signals for whatever reason. --- lib/unicorn.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/unicorn.rb b/lib/unicorn.rb index 44d1b6d..2f86de2 100644 --- a/lib/unicorn.rb +++ b/lib/unicorn.rb @@ -159,7 +159,7 @@ module Unicorn # are trapped. See trap_deferred @rd_sig, @wr_sig = IO.pipe unless (@rd_sig && @wr_sig) @rd_sig.nonblock = @wr_sig.nonblock = true - ready = mode = nil + mode = nil respawn = true QUEUE_SIGS.each { |sig| trap_deferred(sig) } @@ -173,6 +173,7 @@ module Unicorn when nil murder_lazy_workers spawn_missing_workers if respawn + master_sleep when 'QUIT' # graceful shutdown break when 'TERM', 'INT' # immediate shutdown @@ -206,18 +207,6 @@ module Unicorn else logger.error "master process in unknown mode: #{mode}" end - reap_all_workers - - ready = begin - IO.select([@rd_sig], nil, nil, 1) or next - rescue Errno::EINTR # next - end - ready[0] && ready[0][0] or next - begin - @rd_sig.sysread(1) - rescue Errno::EAGAIN, Errno::EINTR - # spurious wakeup? ignore it - end end rescue Errno::EINTR retry @@ -265,6 +254,17 @@ module Unicorn end end + # wait for a signal hander to wake us up and then consume the pipe + # Wake up every second anyways to run murder_lazy_workers + def master_sleep + begin + ready = IO.select([@rd_sig], nil, nil, 1) + ready && ready[0] && ready[0][0] or return + loop { @rd_sig.sysread(Const::CHUNK_SIZE) } + rescue Errno::EAGAIN, Errno::EINTR + end + end + def awaken_master begin @wr_sig.syswrite('.') # wakeup master process from IO.select -- cgit v1.2.3-24-ge0c7