about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEvan Weaver <eweaver@twitter.com>2009-01-31 19:30:58 -0800
committerEvan Weaver <eweaver@twitter.com>2009-01-31 19:30:58 -0800
commitd3c75a987596a3249f0bbaa8de50b8a6046cb864 (patch)
treedf54f31b51e5631a928ccbfd3e33285be1bc27e5
parent914481b635b0c7baefaf7d955c3cd59af2fafeb0 (diff)
downloadunicorn-d3c75a987596a3249f0bbaa8de50b8a6046cb864.tar.gz
-rw-r--r--lib/mongrel.rb33
-rw-r--r--lib/mongrel/cgi.rb2
-rw-r--r--test/unit/test_server.rb4
-rw-r--r--test/unit/test_threading.rb16
4 files changed, 29 insertions, 26 deletions
diff --git a/lib/mongrel.rb b/lib/mongrel.rb
index 0b90057..07fc36f 100644
--- a/lib/mongrel.rb
+++ b/lib/mongrel.rb
@@ -39,6 +39,10 @@ module Mongrel
     def logger
       @logger ||= Logger.new(STDERR)
     end
+    
+    def run(app, options = {})
+      HttpServer.new(app, options).start.join
+    end
   end
 
   # Used to stop the HttpServer via Thread.raise.
@@ -71,9 +75,11 @@ module Mongrel
     attr_reader :max_concurrent_threads
     
     DEFAULTS = {
-      :max_queued_threads => 20,
-      :max_concurrent_threads => 20,
-      :timeout => 60
+      :Max_queued_threads => 20,
+      :Max_concurrent_threads => 20,
+      :Timeout => 60,
+      :Host => '0.0.0.0',
+      :Port => 8080
     }
 
     # Creates a working server on host:port (strange things happen if port isn't a Number).
@@ -86,19 +92,16 @@ module Mongrel
     # way to deal with overload.  Other schemes involve still parsing the client's request
     # which defeats the point of an overload handling system.
     #
-    def initialize(host, port, app, options = {})
-      options = DEFAULTS.merge(options)
-
-      @socket = TCPServer.new(host, port)
-      if defined?(Fcntl::FD_CLOEXEC)
-        @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
-      end
-      @host, @port, @app = host, port, app
+    def initialize(app, options = {})
+      @app = app      
       @workers = ThreadGroup.new
+      
+      (DEFAULTS.to_a + options.to_a).each do |key, value|
+        instance_variable_set("@#{key.to_s.downcase}", value)
+      end
 
-      @timeout = options[:timeout]
-      @max_queued_threads = options[:max_queued_threads]
-      @max_concurrent_threads = options[:max_concurrent_threads]
+      @socket = TCPServer.new(@host, @port)      
+      @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined?(Fcntl::FD_CLOEXEC)
     end
 
     # Does the majority of the IO processing.  It has been written in Ruby using
@@ -234,7 +237,7 @@ module Mongrel
     
     # Runs the thing.  It returns the thread used so you can "join" it.  You can also
     # access the HttpServer::acceptor attribute to get the thread later.
-    def start!
+    def start
       semaphore = Semaphore.new(@max_concurrent_threads)
       BasicSocket.do_not_reverse_lookup = true
 
diff --git a/lib/mongrel/cgi.rb b/lib/mongrel/cgi.rb
index ed6fcf0..05d2d54 100644
--- a/lib/mongrel/cgi.rb
+++ b/lib/mongrel/cgi.rb
@@ -173,7 +173,7 @@ module Mongrel
     
     # The stdoutput should be completely bypassed but we'll drop a warning just in case
     def stdoutput
-      Mongrel.logger.warn "WARNING: Your program is doing something not expected.  Please tell Zed that stdoutput was used and what software you are running.  Thanks."
+      Mongrel.logger.warn "WARNING: stdoutput used."
       @response.body
     end
   end
diff --git a/test/unit/test_server.rb b/test/unit/test_server.rb
index a44b927..4ce728d 100644
--- a/test/unit/test_server.rb
+++ b/test/unit/test_server.rb
@@ -28,9 +28,9 @@ class WebServerTest < Test::Unit::TestCase
     @app = Rack::URLMap.new('/test' => @tester)
     redirect_test_io do
       # We set max_queued_threads=1 so that we can test the reaping code
-      @server = HttpServer.new("127.0.0.1", @port, @app, :max_queued_threads => 1)
+      @server = HttpServer.new(@app, :Host => "127.0.0.1", :Port => @port, :Max_queued_threads => 1)
     end
-    @server.start!
+    @server.start
   end
 
   def teardown
diff --git a/test/unit/test_threading.rb b/test/unit/test_threading.rb
index 21f6c42..d4ad683 100644
--- a/test/unit/test_threading.rb
+++ b/test/unit/test_threading.rb
@@ -5,10 +5,10 @@ include Mongrel
 
 class FakeHandler
   @@concurrent_threads = 0
-  @@max_concurrent_threads = 0
+  @@threads = 0
   
   def self.max_concurrent_threads
-    @@max_concurrent_threads ||= 0
+    @@threads ||= 0
   end
   
   def initialize
@@ -19,7 +19,7 @@ class FakeHandler
   def call(env)
     @@mutex.synchronize do
       @@concurrent_threads += 1 # !!! same for += and -=
-      @@max_concurrent_threads = [@@concurrent_threads, @@max_concurrent_threads].max
+      @@threads = [@@concurrent_threads, @@threads].max
     end
     
     sleep(0.1)
@@ -34,9 +34,9 @@ class ThreadingTest < Test::Unit::TestCase
     @valid_request = "GET / HTTP/1.1\r\nHost: www.google.com\r\nContent-Type: text/plain\r\n\r\n"
     @port = process_based_port
     @app = Rack::URLMap.new('/test' => FakeHandler.new)
-    @max_concurrent_threads = 4
-    redirect_test_io { @server = HttpServer.new("127.0.0.1", @port, @app, :max_concurrent_threads => @max_concurrent_threads) }    
-    redirect_test_io { @server.start! }
+    @threads = 4
+    redirect_test_io { @server = HttpServer.new(@app, :Host => "127.0.0.1", :Port => @port, :Max_concurrent_threads => @threads) }    
+    redirect_test_io { @server.start }
   end
 
   def teardown
@@ -45,7 +45,7 @@ class ThreadingTest < Test::Unit::TestCase
 
   def test_server_respects_max_concurrent_threads_option
     threads = []
-    (@max_concurrent_threads * 3).times do
+    (@threads * 3).times do
       threads << Thread.new do
         send_data_over_socket("GET /test HTTP/1.1\r\nHost: localhost\r\nContent-Type: text/plain\r\n\r\n")
       end
@@ -53,7 +53,7 @@ class ThreadingTest < Test::Unit::TestCase
     while threads.any? { |thread| thread.alive? }
       sleep(0)
     end
-    assert_equal @max_concurrent_threads, FakeHandler.max_concurrent_threads
+    assert_equal @threads, FakeHandler.max_concurrent_threads
   end
   
   private