diff --git a/lib/mongrel2/connection.rb b/lib/mongrel2/connection.rb index 36cca90..387e418 100644 --- a/lib/mongrel2/connection.rb +++ b/lib/mongrel2/connection.rb @@ -6,8 +6,8 @@ module Mongrel2 class Connection CTX = ZMQ::Context.new(1) - def initialize(uuid, sub, pub, block = true) - @uuid, @sub, @pub, @block = uuid, sub, pub, block + def initialize(uuid, sub, pub) + @uuid, @sub, @pub = uuid, sub, pub # Connect to receive requests @reqs = CTX.socket(ZMQ::PULL) @@ -20,7 +20,7 @@ def initialize(uuid, sub, pub, block = true) end def recv - msg = @reqs.recv_string(@block ? 0 : ZMQ::NOBLOCK) + msg = @reqs.recv_string(0) msg.nil? ? nil : Request.parse(msg) end diff --git a/lib/rack/handler/mongrel2.rb b/lib/rack/handler/mongrel2.rb index 635a6e2..0b17a1f 100644 --- a/lib/rack/handler/mongrel2.rb +++ b/lib/rack/handler/mongrel2.rb @@ -9,29 +9,27 @@ def run(app, options = {}) options = { :recv => 'tcp://127.0.0.1:9997' || ENV['RACK_MONGREL2_RECV'], :send => 'tcp://127.0.0.1:9996' || ENV['RACK_MONGREL2_SEND'], - :uuid => ENV['RACK_MONGREL2_UUID'], - :block => ENV['RACK_MONGREL2_NONBLOCK'].to_s.match(/1|t(?:rue)?|y(?:es)/i).nil? + :uuid => ENV['RACK_MONGREL2_UUID'] }.merge(options) raise ArgumentError.new('Must specify an :uuid or set RACK_MONGREL2_UUID') if options[:uuid].nil? - conn = ::Mongrel2::Connection.new(options[:uuid], options[:recv], options[:send], options[:block]) + conn = ::Mongrel2::Connection.new(options[:uuid], options[:recv], options[:send]) running = true - # This doesn't work at all until zmq fixes their shit - # %w(INT TERM KILL).each do |sig| - # trap(sig) do - # conn.close - # running = false - # end - # end + # This doesn't work at all until zmq fixes their shit (in 2.1.x I think), but trap it now anyway. + %w(INT TERM KILL).each do |sig| + trap(sig) do + conn.close + running = false + end + end while running - req = conn.recv - sleep(1) and next if req.nil? && options[:block] + req = conn.recv rescue nil next if req.nil? || req.disconnect? - return if !running + break if !running script_name = ENV['RACK_RELATIVE_URL_ROOT'] || req.headers['PATTERN'].split('(', 2).first.gsub(/\/$/, '') env = {