about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorIan Ownbey <ian@inspir.es>2009-01-31 14:18:08 -0800
committerIan Ownbey <ian@inspir.es>2009-01-31 14:18:08 -0800
commitfa82e8e3fb52e0107163e57a705cc652f276e1ab (patch)
tree8bf4e995294379699cefec179a77f711d079ad2b
parentdda58fa2d58c4ab2bda0a9580841e3c4e09bb30c (diff)
parentcb3b9862a5fef4f3fd197e0319bbea0de562f9da (diff)
downloadunicorn-fa82e8e3fb52e0107163e57a705cc652f276e1ab.tar.gz
* 'master' of git@github.com:fauna/mongrel:
  Merge pivotal code.
  Moving toward using a logger instead of dumping to STDERR all over the place.
  TODO been did.
  No commands.
-rw-r--r--TODO3
-rw-r--r--bin/mongrel_rails3
-rw-r--r--lib/mongrel.rb110
-rw-r--r--lib/mongrel/cgi.rb5
-rw-r--r--lib/mongrel/command.rb219
-rw-r--r--lib/mongrel/configurator.rb385
-rw-r--r--lib/mongrel/handlers.rb199
-rw-r--r--lib/mongrel/http_request.rb4
-rw-r--r--lib/mongrel/rails.rb180
-rw-r--r--lib/mongrel/semaphore.rb46
-rw-r--r--lib/mongrel/stats.rb89
-rw-r--r--test/test_suite.rb3
-rw-r--r--test/unit/test_semaphore.rb118
-rw-r--r--test/unit/test_threading.rb82
-rw-r--r--test/unit/test_ws.rb7
15 files changed, 873 insertions, 580 deletions
diff --git a/TODO b/TODO
index 2a65957..5a6c620 100644
--- a/TODO
+++ b/TODO
@@ -1,4 +1,5 @@
-Ruby 1.9 compat
+
+X Ruby 1.9 compat
 QA Rack compat
 - put Mongrel.run somewhere
 PID-aware mongrel binary (like thin)
diff --git a/bin/mongrel_rails b/bin/mongrel_rails
index 42ecf31..c9eac8f 100644
--- a/bin/mongrel_rails
+++ b/bin/mongrel_rails
@@ -31,6 +31,7 @@ module Mongrel
         ['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
         ['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
         ['-n', '--num-processors INT', "Number of processors active before clients denied", :@num_processors, 1024],
+        ['-N', '--num-threads INT', "Maximum number of requests to process concurrently", :@max_concurrent_threads, 1024],
         ['-o', '--timeout TIME', "Time to wait (in seconds) before killing a stalled thread", :@timeout, 60],
         ['-t', '--throttle TIME', "Time to pause (in hundredths of a second) between accepting clients", :@throttle, 0],
         ['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
@@ -181,7 +182,7 @@ module Mongrel
 
     def config_keys
       @config_keys ||=
-        %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors timeout throttle user group prefix)
+        %w(address host port cwd log_file pid_file environment docroot mime_map daemon debug includes config_script num_processors timeout throttle user group prefix max_concurrent_threads)
     end
 
     def settings
diff --git a/lib/mongrel.rb b/lib/mongrel.rb
index 990a8eb..424b7f0 100644
--- a/lib/mongrel.rb
+++ b/lib/mongrel.rb
@@ -8,6 +8,7 @@ require 'etc'
 require 'uri'
 require 'stringio'
 require 'fcntl'
+require 'logger'
 
 # Compiled Mongrel extension
 require 'http11'
@@ -25,11 +26,21 @@ require 'mongrel/const'
 require 'mongrel/http_request'
 require 'mongrel/header_out'
 require 'mongrel/http_response'
+require 'mongrel/semaphore'
 
 # Mongrel module containing all of the classes (include C extensions) for running
 # a Mongrel web server.  It contains a minimalist HTTP server with just enough
 # functionality to service web application requests fast as possible.
 module Mongrel
+  class << self
+    # A logger instance that conforms to the API of stdlib's Logger.
+    attr_accessor :logger
+    
+    # By default, will return an instance of stdlib's Logger logging to STDERR
+    def logger
+      @logger ||= Logger.new STDERR
+    end
+  end
 
   # Used to stop the HttpServer via Thread.raise.
   class StopServer < Exception; end
@@ -69,13 +80,20 @@ module Mongrel
     attr_reader :port
     attr_reader :throttle
     attr_reader :timeout
-    attr_reader :num_processors
+    attr_reader :max_queued_threads
+    
+    DEFAULTS = {
+      :max_queued_threads => 20,
+      :max_concurrent_threads => 20,
+      :throttle => 0,
+      :timeout => 60
+    }
 
     # Creates a working server on host:port (strange things happen if port isn't a Number).
     # Use HttpServer::run to start the server and HttpServer.acceptor.join to
     # join the thread that's processing incoming requests on the socket.
     #
-    # The num_processors optional argument is the maximum number of concurrent
+    # The max_queued_threads optional argument is the maximum number of concurrent
     # processors to accept, anything over this is closed immediately to maintain
     # server processing performance.  This may seem mean but it is the most efficient
     # way to deal with overload.  Other schemes involve still parsing the client's request
@@ -84,20 +102,21 @@ module Mongrel
     # The throttle parameter is a sleep timeout (in hundredths of a second) that is placed between
     # socket.accept calls in order to give the server a cheap throttle time.  It defaults to 0 and
     # actually if it is 0 then the sleep is not done at all.
-    def initialize(host, port, app, opts = {})
+    def initialize(host, port, app, options = {})
+      options = DEFAULTS.merge(options)
+
       tries = 0
       @socket = TCPServer.new(host, port)
       if defined?(Fcntl::FD_CLOEXEC)
         @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
       end
-      @host = host
-      @port = port
+      @host, @port, @app = host, port, app
       @workers = ThreadGroup.new
-      # Set default opts
-      @app = app
-      @num_processors = opts.delete(:num_processors) || 950
-      @throttle       = (opts.delete(:throttle) || 0) / 100
-      @timeout        = opts.delete(:timeout) || 60
+
+      @throttle = options[:throttle] / 100.0
+      @timeout = options[:timeout]
+      @max_queued_threads = options[:max_queued_threads]
+      @max_concurrent_threads = options[:max_concurrent_threads]
     end
 
     # Does the majority of the IO processing.  It has been written in Ruby using
@@ -162,21 +181,21 @@ module Mongrel
       rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
         client.close rescue nil
       rescue HttpParserError => e
-        STDERR.puts "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}"
-        STDERR.puts "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n"
+        Mongrel.logger.error "#{Time.now}: HTTP parse error, malformed request (#{params[Const::HTTP_X_FORWARDED_FOR] || client.peeraddr.last}): #{e.inspect}"
+        Mongrel.logger.error "#{Time.now}: REQUEST DATA: #{data.inspect}\n---\nPARAMS: #{params.inspect}\n---\n"
       rescue Errno::EMFILE
         reap_dead_workers('too many files')
       rescue Object => e
-        STDERR.puts "#{Time.now}: Read error: #{e.inspect}"
-        STDERR.puts e.backtrace.join("\n")
+        Mongrel.logger.error "#{Time.now}: Read error: #{e.inspect}"
+        Mongrel.logger.error e.backtrace.join("\n")
       ensure
         begin
           client.close
         rescue IOError
           # Already closed
         rescue Object => e
-          STDERR.puts "#{Time.now}: Client error: #{e.inspect}"
-          STDERR.puts e.backtrace.join("\n")
+          Mongrel.logger.error "#{Time.now}: Client error: #{e.inspect}"
+          Mongrel.logger.error e.backtrace.join("\n")
         end
         request.body.close! if request and request.body.class == Tempfile
       end
@@ -188,14 +207,14 @@ module Mongrel
     # after the reap is done.  It only runs if there are workers to reap.
     def reap_dead_workers(reason='unknown')
       if @workers.list.length > 0
-        STDERR.puts "#{Time.now}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'"
+        Mongrel.logger.info "#{Time.now}: Reaping #{@workers.list.length} threads for slow workers because of '#{reason}'"
         error_msg = "Mongrel timed out this thread: #{reason}"
         mark = Time.now
         @workers.list.each do |worker|
           worker[:started_on] = Time.now if not worker[:started_on]
 
           if mark - worker[:started_on] > @timeout + @throttle
-            STDERR.puts "Thread #{worker.inspect} is too old, killing."
+            Mongrel.logger.info "Thread #{worker.inspect} is too old, killing."
             worker.raise(TimeoutError.new(error_msg))
           end
         end
@@ -210,7 +229,7 @@ module Mongrel
     # that much longer.
     def graceful_shutdown
       while reap_dead_workers("shutdown") > 0
-        STDERR.puts "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds."
+        Mongrel.logger.info "Waiting for #{@workers.list.length} requests to finish, could take #{@timeout + @throttle} seconds."
         sleep @timeout / 10
       end
     end
@@ -235,6 +254,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!
+      semaphore = Semaphore.new(@max_concurrent_threads)
       BasicSocket.do_not_reverse_lookup=true
 
       configure_socket_options
@@ -254,12 +274,12 @@ module Mongrel
               end
   
               worker_list = @workers.list
-              if worker_list.length >= @num_processors
-                STDERR.puts "Server overloaded with #{worker_list.length} processors (#@num_processors max). Dropping connection."
+              if worker_list.length >= @max_queued_threads
+                Mongrel.logger.error "Server overloaded with #{worker_list.length} processors (#@max_queued_threads max). Dropping connection."
                 client.close rescue nil
                 reap_dead_workers("max processors")
               else
-                thread = Thread.new(client) {|c| process_client(c) }
+                thread = Thread.new(client) {|c| semaphore.synchronize { process_client(c) } }
                 thread[:started_on] = Time.now
                 @workers.add(thread)
   
@@ -274,63 +294,25 @@ module Mongrel
               # client closed the socket even before accept
               client.close rescue nil
             rescue Object => e
-              STDERR.puts "#{Time.now}: Unhandled listen loop exception #{e.inspect}."
-              STDERR.puts e.backtrace.join("\n")
+              Mongrel.logger.error "#{Time.now}: Unhandled listen loop exception #{e.inspect}."
+              Mongrel.logger.error e.backtrace.join("\n")
             end
           end
           graceful_shutdown
         ensure
           @socket.close
-          # STDERR.puts "#{Time.now}: Closed socket."
+          # Mongrel.logger.info "#{Time.now}: Closed socket."
         end
       end
 
       return @acceptor
     end
 
-    # Simply registers a handler with the internal URIClassifier.  When the URI is
-    # found in the prefix of a request then your handler's HttpHandler::process method
-    # is called.  See Mongrel::URIClassifier#register for more information.
-    #
-    # If you set in_front=true then the passed in handler will be put in the front of the list
-    # for that particular URI. Otherwise it's placed at the end of the list.
-    def register(uri, handler, in_front=false)
-      begin
-        @classifier.register(uri, [handler])
-      rescue URIClassifier::RegistrationError => e
-        handlers = @classifier.resolve(uri)[2]
-        if handlers
-          # Already registered
-          method_name = in_front ? 'unshift' : 'push'
-          handlers.send(method_name, handler)
-        else
-          raise
-        end
-      end
-      handler.listener = self
-    end
-
-    # Removes any handlers registered at the given URI.  See Mongrel::URIClassifier#unregister
-    # for more information.  Remember this removes them *all* so the entire
-    # processing chain goes away.
-    def unregister(uri)
-      @classifier.unregister(uri)
-    end
-
     # Stops the acceptor thread and then causes the worker threads to finish
     # off the request queue before finally exiting.
     def stop(synchronous=false)
       @acceptor.raise(StopServer.new)
-
-      if synchronous
-        sleep(0.5) while @acceptor.alive?
-      end
+      (sleep(0.5) while @acceptor.alive?) if synchronous
     end
-
   end
 end
-
-# Load experimental library, if present. We put it here so it can override anything
-# in regular Mongrel.
-
-$LOAD_PATH.unshift 'projects/mongrel_experimental/lib/'
diff --git a/lib/mongrel/cgi.rb b/lib/mongrel/cgi.rb
index 3957611..ed6fcf0 100644
--- a/lib/mongrel/cgi.rb
+++ b/lib/mongrel/cgi.rb
@@ -173,9 +173,8 @@ module Mongrel
     
     # The stdoutput should be completely bypassed but we'll drop a warning just in case
     def stdoutput
-      STDERR.puts "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: Your program is doing something not expected.  Please tell Zed that stdoutput was used and what software you are running.  Thanks."
       @response.body
-    end    
-
+    end
   end
 end
diff --git a/lib/mongrel/command.rb b/lib/mongrel/command.rb
deleted file mode 100644
index c1fcca1..0000000
--- a/lib/mongrel/command.rb
+++ /dev/null
@@ -1,219 +0,0 @@
-# Copyright (c) 2005 Zed A. Shaw
-# You can redistribute it and/or modify it under the same terms as Ruby.
-#
-# Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html
-# for more information.
-
-require 'singleton'
-require 'optparse'
-
-module Mongrel
-
-  # Contains all of the various commands that are used with
-  # Mongrel servers.
-
-  module Command
-
-    BANNER = "Usage: mongrel_rails <command> [options]"
-
-    # A Command pattern implementation used to create the set of command available to the user
-    # from Mongrel.  The script uses objects which implement this interface to do the
-    # user's bidding.
-    module Base
-
-      attr_reader :valid, :done_validating, :original_args
-
-      # Called by the implemented command to set the options for that command.
-      # Every option has a short and long version, a description, a variable to
-      # set, and a default value.  No exceptions.
-      def options(opts)
-        # process the given options array
-        opts.each do |short, long, help, variable, default|
-          self.instance_variable_set(variable, default)
-          @opt.on(short, long, help) do |arg|
-            self.instance_variable_set(variable, arg)
-          end
-        end
-      end
-
-      # Called by the subclass to setup the command and parse the argv arguments.
-      # The call is destructive on argv since it uses the OptionParser#parse! function.
-      def initialize(options={})
-        argv = options[:argv] || []
-        @opt = OptionParser.new
-        @opt.banner = Mongrel::Command::BANNER
-        @valid = true
-        # this is retarded, but it has to be done this way because -h and -v exit
-        @done_validating = false
-        @original_args = argv.dup
-
-        configure
-
-        # I need to add my own -h definition to prevent the -h by default from exiting.
-        @opt.on_tail("-h", "--help", "Show this message") do
-          @done_validating = true
-          puts @opt
-        end
-
-        # I need to add my own -v definition to prevent the -v from exiting by default as well.
-        @opt.on_tail("--version", "Show version") do
-          @done_validating = true
-          if VERSION
-            puts "Version #{Mongrel::Const::MONGREL_VERSION}"
-          end
-        end
-
-        @opt.parse! argv
-      end
-
-      def configure
-        options []
-      end
-
-      # Returns true/false depending on whether the command is configured properly.
-      def validate
-        return @valid
-      end
-
-      # Returns a help message.  Defaults to OptionParser#help which should be good.
-      def help
-        @opt.help
-      end
-
-      # Runs the command doing it's job.  You should implement this otherwise it will
-      # throw a NotImplementedError as a reminder.
-      def run
-        raise NotImplementedError
-      end
-
-
-      # Validates the given expression is true and prints the message if not, exiting.
-      def valid?(exp, message)
-        if not @done_validating and (not exp)
-          failure message
-          @valid = false
-          @done_validating = true
-        end
-      end
-
-      # Validates that a file exists and if not displays the message
-      def valid_exists?(file, message)
-        valid?(file != nil && File.exist?(file), message)
-      end
-
-
-      # Validates that the file is a file and not a directory or something else.
-      def valid_file?(file, message)
-        valid?(file != nil && File.file?(file), message)
-      end
-
-      # Validates that the given directory exists
-      def valid_dir?(file, message)
-        valid?(file != nil && File.directory?(file), message)
-      end
-
-      def valid_user?(user)
-        valid?(@group, "You must also specify a group.")
-        begin
-          Etc.getpwnam(user)
-        rescue
-          failure "User does not exist: #{user}"
-          @valid = false
-        end
-      end
-
-      def valid_group?(group)
-        valid?(@user, "You must also specify a user.")
-        begin
-          Etc.getgrnam(group)
-        rescue
-          failure "Group does not exist: #{group}"
-          @valid = false
-        end
-      end
-
-      # Just a simple method to display failure until something better is developed.
-      def failure(message)
-        STDERR.puts "!!! #{message}"
-      end
-    end
-
-    # A Singleton class that manages all of the available commands
-    # and handles running them.
-    class Registry
-      include Singleton
-
-      # Builds a list of possible commands from the Command derivates list
-      def commands
-        pmgr = GemPlugin::Manager.instance
-        list = pmgr.plugins["/commands"].keys
-        return list.sort
-      end
-
-      # Prints a list of available commands.
-      def print_command_list
-        puts "#{Mongrel::Command::BANNER}\nAvailable commands are:\n\n"
-
-        self.commands.each do |name|
-          if /mongrel::/ =~ name
-            name = name[9 .. -1]
-          end
-
-          puts " - #{name[1 .. -1]}\n"
-        end
-
-        puts "\nEach command takes -h as an option to get help."
-
-      end
-
-
-      # Runs the args against the first argument as the command name.
-      # If it has any errors it returns a false, otherwise it return true.
-      def run(args)
-        # find the command
-        cmd_name = args.shift
-
-        if !cmd_name or cmd_name == "?" or cmd_name == "help"
-          print_command_list
-          return true
-        elsif cmd_name == "--version"
-          puts "Mongrel Web Server #{Mongrel::Const::MONGREL_VERSION}"
-          return true
-        end
-
-        begin
-          # quick hack so that existing commands will keep working but the Mongrel:: ones can be moved
-          if ["start", "stop", "restart"].include? cmd_name
-            cmd_name = "mongrel::" + cmd_name
-          end
-
-          command = GemPlugin::Manager.instance.create("/commands/#{cmd_name}", :argv => args)
-        rescue OptionParser::InvalidOption
-          STDERR.puts "#$! for command '#{cmd_name}'"
-          STDERR.puts "Try #{cmd_name} -h to get help."
-          return false
-        rescue
-          STDERR.puts "ERROR RUNNING '#{cmd_name}': #$!"
-          STDERR.puts "Use help command to get help"
-          return false
-        end
-
-        # Normally the command is NOT valid right after being created
-        # but sometimes (like with -h or -v) there's no further processing
-        # needed so the command is already valid so we can skip it.
-        if not command.done_validating
-          if not command.validate
-            STDERR.puts "#{cmd_name} reported an error. Use mongrel_rails #{cmd_name} -h to get help."
-            return false
-          else
-            command.run
-          end
-        end
-
-        return true
-      end
-
-    end
-  end
-end
-
diff --git a/lib/mongrel/configurator.rb b/lib/mongrel/configurator.rb
new file mode 100644
index 0000000..2442152
--- /dev/null
+++ b/lib/mongrel/configurator.rb
@@ -0,0 +1,385 @@
+require 'yaml'
+require 'etc'
+
+module Mongrel
+  # Implements a simple DSL for configuring a Mongrel server for your
+  # purposes.  More used by framework implementers to setup Mongrel
+  # how they like, but could be used by regular folks to add more things
+  # to an existing mongrel configuration.
+  #
+  # It is used like this:
+  #
+  #   require 'mongrel'
+  #   config = Mongrel::Configurator.new :host => "127.0.0.1" do
+  #     listener :port => 3000 do
+  #       uri "/app", :handler => Mongrel::DirHandler.new(".", load_mime_map("mime.yaml"))
+  #     end
+  #     run
+  #   end
+  #
+  # This will setup a simple DirHandler at the current directory and load additional
+  # mime types from mimy.yaml.  The :host => "127.0.0.1" is actually not
+  # specific to the servers but just a hash of default parameters that all
+  # server or uri calls receive.
+  #
+  # When you are inside the block after Mongrel::Configurator.new you can simply
+  # call functions that are part of Configurator (like server, uri, daemonize, etc)
+  # without having to refer to anything else.  You can also call these functions on
+  # the resulting object directly for additional configuration.
+  #
+  # A major thing about Configurator is that it actually lets you configure
+  # multiple listeners for any hosts and ports you want.  These are kept in a
+  # map config.listeners so you can get to them.
+  #
+  # * :pid_file => Where to write the process ID.
+  class Configurator
+    attr_reader :listeners
+    attr_reader :defaults
+    attr_reader :needs_restart
+
+    # You pass in initial defaults and then a block to continue configuring.
+    def initialize(defaults={}, &block)
+      @listener = nil
+      @listener_name = nil
+      @listeners = {}
+      @defaults = defaults
+      @needs_restart = false
+      @pid_file = defaults[:pid_file]
+
+      if block
+        cloaker(&block).bind(self).call
+      end
+    end
+
+    # Change privileges of the process to specified user and group.
+    def change_privilege(user, group)
+      begin
+        uid, gid = Process.euid, Process.egid
+        target_uid = Etc.getpwnam(user).uid if user
+        target_gid = Etc.getgrnam(group).gid if group
+
+        if uid != target_uid or gid != target_gid
+          log "Initiating groups for #{user.inspect}:#{group.inspect}."
+          Process.initgroups(user, target_gid)
+        
+          log "Changing group to #{group.inspect}."
+          Process::GID.change_privilege(target_gid)
+
+          log "Changing user to #{user.inspect}."
+          Process::UID.change_privilege(target_uid)
+        end
+      rescue Errno::EPERM => e
+        log "Couldn't change user and group to #{user.inspect}:#{group.inspect}: #{e.to_s}."
+        log "Mongrel failed to start."
+        exit 1
+      end
+    end
+
+    def remove_pid_file
+      File.unlink(@pid_file) if @pid_file and File.exists?(@pid_file)
+    end
+
+    # Writes the PID file if we're not on Windows.
+    def write_pid_file
+      if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/
+        log "Writing PID file to #{@pid_file}"
+        open(@pid_file,"w") {|f| f.write(Process.pid) }
+        open(@pid_file,"w") do |f|
+          f.write(Process.pid)
+          File.chmod(0644, @pid_file)
+        end      
+      end
+    end
+
+    # Generates a class for cloaking the current self and making the DSL nicer.
+    def cloaking_class
+      class << self
+        self
+      end
+    end
+
+    # Do not call this.  You were warned.
+    def cloaker(&block)
+      cloaking_class.class_eval do
+        define_method :cloaker_, &block
+        meth = instance_method( :cloaker_ )
+        remove_method :cloaker_
+        meth
+      end
+    end
+
+    # This will resolve the given options against the defaults.
+    # Normally just used internally.
+    def resolve_defaults(options)
+      options.merge(@defaults)
+    end
+
+    # Starts a listener block.  This is the only one that actually takes
+    # a block and then you make Configurator.uri calls in order to setup
+    # your URIs and handlers.  If you write your Handlers as GemPlugins
+    # then you can use load_plugins and plugin to load them.
+    #
+    # It expects the following options (or defaults):
+    #
+    # * :host => Host name to bind.
+    # * :port => Port to bind.
+    # * :max_queued_threads => The maximum number of concurrent threads allowed.
+    # * :throttle => Time to pause (in hundredths of a second) between accepting clients.
+    # * :timeout => Time to wait (in seconds) before killing a stalled thread.
+    # * :user => User to change to, must have :group as well.
+    # * :group => Group to change to, must have :user as well.
+    #
+    def listener(options={},&block)
+      raise "Cannot call listener inside another listener block." if (@listener or @listener_name)
+      ops = resolve_defaults(options)
+
+      @listener = Mongrel::HttpServer.new(ops[:host], ops[:port].to_i, ops)
+      @listener_name = "#{ops[:host]}:#{ops[:port]}"
+      @listeners[@listener_name] = @listener
+
+      if ops[:user] and ops[:group]
+        change_privilege(ops[:user], ops[:group])
+      end
+
+      # Does the actual cloaking operation to give the new implicit self.
+      if block
+        cloaker(&block).bind(self).call
+      end
+
+      # all done processing this listener setup, reset implicit variables
+      @listener = nil
+      @listener_name = nil
+    end
+
+
+    # Called inside a Configurator.listener block in order to
+    # add URI->handler mappings for that listener.  Use this as
+    # many times as you like.  It expects the following options
+    # or defaults:
+    #
+    # * :handler => HttpHandler -- Handler to use for this location.
+    # * :in_front => true/false -- Rather than appending, it prepends this handler.
+    def uri(location, options={})
+      ops = resolve_defaults(options)
+      @listener.register(location, ops[:handler], ops[:in_front])
+    end
+
+
+    # Daemonizes the current Ruby script turning all the
+    # listeners into an actual "server" or detached process.
+    # You must call this *before* frameworks that open files
+    # as otherwise the files will be closed by this function.
+    #
+    # Does not work for Win32 systems (the call is silently ignored).
+    #
+    # Requires the following options or defaults:
+    #
+    # * :cwd => Directory to change to.
+    # * :log_file => Where to write STDOUT and STDERR.
+    #
+    # It is safe to call this on win32 as it will only require the daemons
+    # gem/library if NOT win32.
+    def daemonize(options={})
+      ops = resolve_defaults(options)
+      # save this for later since daemonize will hose it
+      if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/
+        require 'daemons/daemonize'
+
+        logfile = ops[:log_file]
+        if logfile[0].chr != "/"
+          logfile = File.join(ops[:cwd],logfile)
+          if not File.exist?(File.dirname(logfile))
+            log "!!! Log file directory not found at full path #{File.dirname(logfile)}.  Update your configuration to use a full path."
+            exit 1
+          end
+        end
+
+        Daemonize.daemonize(logfile)
+
+        # change back to the original starting directory
+        Dir.chdir(ops[:cwd])
+
+      else
+        log "WARNING: Win32 does not support daemon mode."
+      end
+    end
+
+
+    # Uses the GemPlugin system to easily load plugins based on their
+    # gem dependencies.  You pass in either an :includes => [] or
+    # :excludes => [] setting listing the names of plugins to include
+    # or exclude from the determining the dependencies.
+    def load_plugins(options={})
+      ops = resolve_defaults(options)
+
+      load_settings = {}
+      if ops[:includes]
+        ops[:includes].each do |plugin|
+          load_settings[plugin] = GemPlugin::INCLUDE
+        end
+      end
+
+      if ops[:excludes]
+        ops[:excludes].each do |plugin|
+          load_settings[plugin] = GemPlugin::EXCLUDE
+        end
+      end
+
+      GemPlugin::Manager.instance.load(load_settings)
+    end
+
+
+    # Easy way to load a YAML file and apply default settings.
+    def load_yaml(file, default={})
+      default.merge(YAML.load_file(file))
+    end
+
+
+    # Loads the MIME map file and checks that it is correct
+    # on loading.  This is commonly passed to Mongrel::DirHandler
+    # or any framework handler that uses DirHandler to serve files.
+    # You can also include a set of default MIME types as additional
+    # settings.  See Mongrel::DirHandler for how the MIME types map
+    # is organized.
+    def load_mime_map(file, mime={})
+      # configure any requested mime map
+      mime = load_yaml(file, mime)
+
+      # check all the mime types to make sure they are the right format
+      mime.each {|k,v| log "WARNING: MIME type #{k} must start with '.'" if k.index(".") != 0 }
+
+      return mime
+    end
+
+
+    # Loads and creates a plugin for you based on the given
+    # name and configured with the selected options.  The options
+    # are merged with the defaults prior to passing them in.
+    def plugin(name, options={})
+      ops = resolve_defaults(options)
+      GemPlugin::Manager.instance.create(name, ops)
+    end
+
+    # Lets you do redirects easily as described in Mongrel::RedirectHandler.
+    # You use it inside the configurator like this:
+    #
+    #   redirect("/test", "/to/there") # simple
+    #   redirect("/to", /t/, 'w') # regexp
+    #   redirect("/hey", /(w+)/) {|match| ...}  # block
+    #
+    def redirect(from, pattern, replacement = nil, &block)
+      uri from, :handler => Mongrel::RedirectHandler.new(pattern, replacement, &block)
+    end
+
+    # Works like a meta run method which goes through all the
+    # configured listeners.  Use the Configurator.join method
+    # to prevent Ruby from exiting until each one is done.
+    def run
+      @listeners.each {|name,s|
+        s.run
+      }
+
+      $mongrel_sleeper_thread = Thread.new { loop { sleep 1 } }
+    end
+
+    # Calls .stop on all the configured listeners so they
+    # stop processing requests (gracefully).  By default it
+    # assumes that you don't want to restart.
+    def stop(needs_restart=false, synchronous=false)  
+      @listeners.each do |name,s|
+        s.stop(synchronous)      
+      end      
+      @needs_restart = needs_restart
+    end
+
+
+    # This method should actually be called *outside* of the
+    # Configurator block so that you can control it.  In other words
+    # do it like:  config.join.
+    def join
+      @listeners.values.each {|s| s.acceptor.join }
+    end
+
+
+    # Calling this before you register your URIs to the given location
+    # will setup a set of handlers that log open files, objects, and the
+    # parameters for each request.  This helps you track common problems
+    # found in Rails applications that are either slow or become unresponsive
+    # after a little while.
+    #
+    # You can pass an extra parameter *what* to indicate what you want to
+    # debug.  For example, if you just want to dump rails stuff then do:
+    #
+    #   debug "/", what = [:rails]
+    #
+    # And it will only produce the log/mongrel_debug/rails.log file.
+    # Available options are: :access, :files, :objects, :threads, :rails
+    #
+    # NOTE: Use [:files] to get accesses dumped to stderr like with WEBrick.
+    def debug(location, what = [:access, :files, :objects, :threads, :rails])
+      require 'mongrel/debug'
+      handlers = {
+        :access => "/handlers/requestlog::access",
+        :files => "/handlers/requestlog::files",
+        :objects => "/handlers/requestlog::objects",
+        :threads => "/handlers/requestlog::threads",
+        :rails => "/handlers/requestlog::params"
+      }
+
+      # turn on the debugging infrastructure, and ObjectTracker is a pig
+      MongrelDbg.configure
+
+      # now we roll through each requested debug type, turn it on and load that plugin
+      what.each do |type|
+        MongrelDbg.begin_trace type
+        uri location, :handler => plugin(handlers[type])
+      end
+    end
+
+    # Used to allow you to let users specify their own configurations
+    # inside your Configurator setup.  You pass it a script name and
+    # it reads it in and does an eval on the contents passing in the right
+    # binding so they can put their own Configurator statements.
+    def run_config(script)
+      open(script) {|f| eval(f.read, proc {self}) }
+    end
+
+    # Sets up the standard signal handlers that are used on most Ruby
+    # It only configures if the platform is not win32 and doesn't do
+    # a HUP signal since this is typically framework specific.
+    #
+    # Requires a :pid_file option given to Configurator.new to indicate a file to delete.  
+    # It sets the MongrelConfig.needs_restart attribute if
+    # the start command should reload.  It's up to you to detect this
+    # and do whatever is needed for a "restart".
+    #
+    # This command is safely ignored if the platform is win32 (with a warning)
+    def setup_signals(options={})
+      ops = resolve_defaults(options)
+
+      # forced shutdown, even if previously restarted (actually just like TERM but for CTRL-C)
+      trap("INT") { log "INT signal received."; stop(false) }
+
+      # clean up the pid file always
+      at_exit { remove_pid_file }
+
+      if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/
+        # graceful shutdown
+        trap("TERM") { log "TERM signal received."; stop }
+        trap("USR1") { log "USR1 received, toggling $mongrel_debug_client to #{!$mongrel_debug_client}"; $mongrel_debug_client = !$mongrel_debug_client }
+        # restart
+        trap("USR2") { log "USR2 signal received."; stop(true) }
+
+        log "Signals ready.  TERM => stop.  USR2 => restart.  INT => stop (no restart)."
+      else
+        log "Signals ready.  INT => stop (no restart)."
+      end
+    end
+
+    # Logs a simple message to STDERR (or the mongrel log if in daemon mode).
+    def log(msg)
+      STDERR.print "** ", msg, "\n"
+    end
+
+  end
+end
diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb
index ce24628..0ae3af6 100644
--- a/lib/mongrel/handlers.rb
+++ b/lib/mongrel/handlers.rb
@@ -4,21 +4,16 @@
 # Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html
 # for more information.
 
-require 'mongrel/stats'
 require 'zlib'
 require 'yaml'
 
 module Mongrel
-
+  #
   # You implement your application handler with this.  It's very light giving
   # just the minimum necessary for you to handle a request and shoot back
   # a response.  Look at the HttpRequest and HttpResponse objects for how
   # to use them.
   #
-  # This is used for very simple handlers that don't require much to operate.
-  # More extensive plugins or those you intend to distribute as GemPlugins
-  # should be implemented using the HttpHandlerPlugin mixin.
-  #
   class HttpHandler
     attr_reader :request_notify
     attr_accessor :listener
@@ -38,195 +33,5 @@ module Mongrel
 
     def process(request, response)
     end
-
-  end
-
-
-  # This is used when your handler is implemented as a GemPlugin.
-  # The plugin always takes an options hash which you can modify
-  # and then access later.  They are stored by default for
-  # the process method later.
-  module HttpHandlerPlugin
-    attr_reader :options
-    attr_reader :request_notify
-    attr_accessor :listener
-
-    def request_begins(params)
-    end
-
-    def request_progress(params, clen, total)
-    end
-
-    def initialize(options={})
-      @options = options
-      @header_only = false
-    end
-
-    def process(request, response)
-    end
-
-  end
-
-
-  #
-  # The server normally returns a 404 response if an unknown URI is requested, but it
-  # also returns a lame empty message.  This lets you do a 404 response
-  # with a custom message for special URIs.
-  #
-  class Error404Handler < HttpHandler
-
-    # Sets the message to return.  This is constructed once for the handler
-    # so it's pretty efficient.
-    def initialize(msg)
-      @response = Const::ERROR_404_RESPONSE + msg
-    end
-
-    # Just kicks back the standard 404 response with your special message.
-    def process(request, response)
-      response.socket.write(@response)
-    end
-
-  end
-
-  # When added to a config script (-S in mongrel_rails) it will
-  # look at the client's allowed response types and then gzip
-  # compress anything that is going out.
-  #
-  # Valid option is :always_deflate => false which tells the handler to
-  # deflate everything even if the client can't handle it.
-  class DeflateFilter < HttpHandler
-    include Zlib
-    HTTP_ACCEPT_ENCODING = "HTTP_ACCEPT_ENCODING"
-
-    def initialize(ops={})
-      @options = ops
-      @always_deflate = ops[:always_deflate] || false
-    end
-
-    def process(request, response)
-      accepts = request.params[HTTP_ACCEPT_ENCODING]
-      # only process if they support compression
-      if @always_deflate or (accepts and (accepts.include? "deflate" and not response.body_sent))
-        response.header["Content-Encoding"] = "deflate"
-        response.body = deflate(response.body)
-      end
-    end
-
-    private
-      def deflate(stream)
-        deflater = Deflate.new(
-          DEFAULT_COMPRESSION,
-          # drop the zlib header which causes both Safari and IE to choke
-          -MAX_WBITS,
-          DEF_MEM_LEVEL,
-          DEFAULT_STRATEGY)
-
-        stream.rewind
-        gzout = StringIO.new(deflater.deflate(stream.read, FINISH))
-        stream.close
-        gzout.rewind
-        gzout
-      end
-  end
-
-
-  # Implements a few basic statistics for a particular URI.  Register it anywhere
-  # you want in the request chain and it'll quickly gather some numbers for you
-  # to analyze.  It is pretty fast, but don't put it out in production.
-  #
-  # You should pass the filter to StatusHandler as StatusHandler.new(:stats_filter => stats).
-  # This lets you then hit the status URI you want and get these stats from a browser.
-  #
-  # StatisticsFilter takes an option of :sample_rate.  This is a number that's passed to
-  # rand and if that number gets hit then a sample is taken.  This helps reduce the load
-  # and keeps the statistics valid (since sampling is a part of how they work).
-  #
-  # The exception to :sample_rate is that inter-request time is sampled on every request.
-  # If this wasn't done then it wouldn't be accurate as a measure of time between requests.
-  class StatisticsFilter < HttpHandler
-    attr_reader :stats
-
-    def initialize(ops={})
-      @sample_rate = ops[:sample_rate] || 300
-
-      @processors = Mongrel::Stats.new("processors")
-      @reqsize = Mongrel::Stats.new("request Kb")
-      @headcount = Mongrel::Stats.new("req param count")
-      @respsize = Mongrel::Stats.new("response Kb")
-      @interreq = Mongrel::Stats.new("inter-request time")
-    end
-
-
-    def process(request, response)
-      if rand(@sample_rate)+1 == @sample_rate
-        @processors.sample(listener.workers.list.length)
-        @headcount.sample(request.params.length)
-        @reqsize.sample(request.body.length / 1024.0)
-        @respsize.sample((response.body.length + response.header.out.length) / 1024.0)
-      end
-      @interreq.tick
-    end
-
-    def dump
-      "#{@processors.to_s}\n#{@reqsize.to_s}\n#{@headcount.to_s}\n#{@respsize.to_s}\n#{@interreq.to_s}"
-    end
-  end
-
-
-  # The :stats_filter is basically any configured stats filter that you've added to this same
-  # URI.  This lets the status handler print out statistics on how Mongrel is doing.
-  class StatusHandler < HttpHandler
-    def initialize(ops={})
-      @stats = ops[:stats_filter]
-    end
-
-    def table(title, rows)
-      results = "<table border=\"1\"><tr><th colspan=\"#{rows[0].length}\">#{title}</th></tr>"
-      rows.each do |cols|
-        results << "<tr>"
-        cols.each {|col| results << "<td>#{col}</td>" }
-        results << "</tr>"
-      end
-      results + "</table>"
-    end
-
-    def describe_listener
-      results = ""
-      results << "<h1>Listener #{listener.host}:#{listener.port}</h1>"
-      results << table("settings", [
-                       ["host",listener.host],
-                       ["port",listener.port],
-                       ["throttle",listener.throttle],
-                       ["timeout",listener.timeout],
-                       ["workers max",listener.num_processors],
-      ])
-
-      if @stats
-        results << "<h2>Statistics</h2><p>N means the number of samples, pay attention to MEAN, SD, MIN and MAX."
-        results << "<pre>#{@stats.dump}</pre>"
-      end
-
-      results << "<h2>Registered Handlers</h2>"
-      handler_map = listener.classifier.handler_map
-      results << table("handlers", handler_map.map {|uri,handlers|
-        [uri,
-            "<pre>" +
-            handlers.map {|h| h.class.to_s }.join("\n") +
-            "</pre>"
-        ]
-      })
-
-      results
-    end
-
-    def process(request, response)
-      response.start do |head,out|
-        out.write <<-END
-        <html><body><title>Mongrel Server Status</title>
-        #{describe_listener}
-        </body></html>
-        END
-      end
-    end
-  end
+  end  
 end
diff --git a/lib/mongrel/http_request.rb b/lib/mongrel/http_request.rb
index 2416b04..70f236f 100644
--- a/lib/mongrel/http_request.rb
+++ b/lib/mongrel/http_request.rb
@@ -78,8 +78,8 @@ module Mongrel
           remain -= @body.write(@params.http_body)
         end
       rescue Object => e
-        STDERR.puts "#{Time.now}: Error reading HTTP body: #{e.inspect}"
-        STDERR.puts e.backtrace.join("\n")
+        Mongrel.logger.error "#{Time.now}: Error reading HTTP body: #{e.inspect}"
+        Mongrel.logger.error e.backtrace.join("\n")
         # any errors means we should delete the file, including if the file is dumped
         @socket.close rescue nil
         @body.close! if @body.class == Tempfile
diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb
new file mode 100644
index 0000000..2cc5e41
--- /dev/null
+++ b/lib/mongrel/rails.rb
@@ -0,0 +1,180 @@
+# Copyright (c) 2005 Zed A. Shaw
+# You can redistribute it and/or modify it under the same terms as Ruby.
+#
+# Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html
+# for more information.
+
+require 'mongrel'
+require 'cgi'
+
+
+module Mongrel
+  module Rails
+    # Implements a handler that can run Rails and serve files out of the
+    # Rails application's public directory.  This lets you run your Rails
+    # application with Mongrel during development and testing, then use it
+    # also in production behind a server that's better at serving the
+    # static files.
+    #
+    # The RailsHandler takes a mime_map parameter which is a simple suffix=mimetype
+    # mapping that it should add to the list of valid mime types.
+    #
+    # It also supports page caching directly and will try to resolve a request
+    # in the following order:
+    #
+    # * If the requested exact PATH_INFO exists as a file then serve it.
+    # * If it exists at PATH_INFO+".html" exists then serve that.
+    # * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispatch to have Rails go.
+    #
+    # This means that if you are using page caching it will actually work with Mongrel
+    # and you should see a decent speed boost (but not as fast as if you use a static
+    # server like Apache or Litespeed).
+    class RailsHandler < Mongrel::HttpHandler
+      attr_reader :files
+      attr_reader :guard
+      @@file_only_methods = ["GET","HEAD"]
+
+      def initialize(dir, mime_map = {})
+        @files = Mongrel::DirHandler.new(dir,false)
+        @guard = Mutex.new
+
+        # Register the requested MIME types
+        mime_map.each {|k,v| Mongrel::DirHandler::add_mime_type(k,v) }
+      end
+
+      # Attempts to resolve the request as follows:
+      #
+      # * If the requested exact PATH_INFO exists as a file then serve it.
+      # * If it exists at PATH_INFO+".html" exists then serve that.
+      # * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispatch to have Rails go.
+      def process(request, response)
+        return if response.socket.closed?
+        
+        path_info = request.params[Mongrel::Const::PATH_INFO]
+        rest_operator = request.params[Mongrel::Const::REQUEST_URI][/^#{Regexp.escape path_info}(;[^\?]+)/, 1].to_s
+        path_info.chomp!("/")
+        
+        page_cached = path_info + rest_operator + ActionController::Base.page_cache_extension
+        get_or_head = @@file_only_methods.include? request.params[Mongrel::Const::REQUEST_METHOD]
+
+        if get_or_head and @files.can_serve(path_info)
+          # File exists as-is so serve it up
+          @files.process(request,response)
+        elsif get_or_head and @files.can_serve(page_cached)
+          # Possible cached page, serve it up
+          request.params[Mongrel::Const::PATH_INFO] = page_cached
+          @files.process(request,response)
+        else
+          begin
+            cgi = Mongrel::CGIWrapper.new(request, response)
+            # We don't want the output to be really final until the dispatch returns a response.
+            cgi.default_really_final = false
+
+            Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body)
+
+            # This finalizes the output using the proper HttpResponse way
+            cgi.out("text/html",true) {""}
+          rescue Errno::EPIPE
+            response.socket.close
+          rescue Object => rails_error
+            STDERR.puts "#{Time.now}: Error calling Dispatcher.dispatch #{rails_error.inspect}"
+            STDERR.puts rails_error.backtrace.join("\n")
+          end
+        end
+      end
+
+      # Does the internal reload for Rails.  It might work for most cases, but
+      # sometimes you get exceptions.  In that case just do a real restart.
+      def reload!
+        begin
+          @guard.synchronize {
+            $".replace $orig_dollar_quote
+            GC.start
+            Dispatcher.reset_application!
+            ActionController::Routing::Routes.reload
+          }
+        end
+      end
+    end
+
+    # Creates Rails specific configuration options for people to use
+    # instead of the base Configurator.
+    class RailsConfigurator < Mongrel::Configurator
+
+      # Creates a single rails handler and returns it so you
+      # can add it to a URI. You can actually attach it to
+      # as many URIs as you want, but this returns the
+      # same RailsHandler for each call.
+      #
+      # Requires the following options:
+      #
+      # * :docroot => The public dir to serve from.
+      # * :environment => Rails environment to use.
+      # * :cwd => The change to working directory
+      #
+      # And understands the following optional settings:
+      #
+      # * :mime => A map of mime types.
+      #
+      # Because of how Rails is designed you can only have
+      # one installed per Ruby interpreter (talk to them
+      # about thread safety).  Because of this the first
+      # time you call this function it does all the config
+      # needed to get your Rails working.  After that
+      # it returns the one handler you've configured.
+      # This lets you attach Rails to any URI(s) you want,
+      # but it still protects you from threads destroying
+      # your handler.
+      def rails(options={})
+
+        return @rails_handler if @rails_handler
+
+        ops = resolve_defaults(options)
+
+        # fix up some defaults
+        ops[:environment] ||= "development"
+        ops[:docroot] ||= "public"
+        ops[:mime] ||= {}
+
+        $orig_dollar_quote = $".clone
+        ENV['RAILS_ENV'] = ops[:environment]
+        env_location = "#{ops[:cwd]}/config/environment"
+        require env_location
+        require 'dispatcher'
+        require 'mongrel/rails'
+
+        ActionController::AbstractRequest.relative_url_root = ops[:prefix] if ops[:prefix]
+
+        @rails_handler = RailsHandler.new(ops[:docroot], ops[:mime])
+      end
+
+      # Reloads Rails.  This isn't too reliable really, but it
+      # should work for most minimal reload purposes.  The only reliable
+      # way to reload properly is to stop and then start the process.
+      def reload!
+        if not @rails_handler
+          raise "Rails was not configured.  Read the docs for RailsConfigurator."
+        end
+
+        log "Reloading Rails..."
+        @rails_handler.reload!
+        log "Done reloading Rails."
+
+      end
+
+      # Takes the exact same configuration as Mongrel::Configurator (and actually calls that)
+      # but sets up the additional HUP handler to call reload!.
+      def setup_rails_signals(options={})
+        ops = resolve_defaults(options)
+        setup_signals(options)
+
+        if RUBY_PLATFORM !~ /djgpp|(cyg|ms|bcc)win|mingw/
+          # rails reload
+          trap("HUP") { log "HUP signal received."; reload!          }
+
+          log "Rails signals registered.  HUP => reload (without restart).  It might not work well."
+        end
+      end
+    end
+  end
+end
diff --git a/lib/mongrel/semaphore.rb b/lib/mongrel/semaphore.rb
new file mode 100644
index 0000000..1c0b87c
--- /dev/null
+++ b/lib/mongrel/semaphore.rb
@@ -0,0 +1,46 @@
+class Semaphore
+  def initialize(resource_count = 0)
+    @available_resource_count = resource_count
+    @mutex = Mutex.new
+    @waiting_threads = []
+  end
+  
+  def wait
+    make_thread_wait unless resource_is_available
+  end
+  
+  def signal
+    schedule_waiting_thread if thread_is_waiting
+  end
+  
+  def synchronize
+    self.wait
+    yield
+  ensure
+    self.signal
+  end
+  
+  private
+  
+  def resource_is_available
+    @mutex.synchronize do
+      return (@available_resource_count -= 1) >= 0
+    end
+  end
+  
+  def make_thread_wait
+    @waiting_threads << Thread.current
+    Thread.stop  
+  end
+  
+  def thread_is_waiting
+    @mutex.synchronize do
+      return (@available_resource_count += 1) <= 0
+    end
+  end
+  
+  def schedule_waiting_thread
+    thread = @waiting_threads.shift
+    thread.wakeup if thread
+  end
+end
diff --git a/lib/mongrel/stats.rb b/lib/mongrel/stats.rb
deleted file mode 100644
index f6cf5ab..0000000
--- a/lib/mongrel/stats.rb
+++ /dev/null
@@ -1,89 +0,0 @@
-# Copyright (c) 2005 Zed A. Shaw
-# You can redistribute it and/or modify it under the same terms as Ruby.
-#
-# Additional work donated by contributors.  See http://mongrel.rubyforge.org/attributions.html
-# for more information.
-
-# A very simple little class for doing some basic fast statistics sampling.
-# You feed it either samples of numeric data you want measured or you call
-# Stats.tick to get it to add a time delta between the last time you called it.
-# When you're done either call sum, sumsq, n, min, max, mean or sd to get
-# the information.  The other option is to just call dump and see everything.
-#
-# It does all of this very fast and doesn't take up any memory since the samples
-# are not stored but instead all the values are calculated on the fly.
-module Mongrel
-  class Stats
-    attr_reader :sum, :sumsq, :n, :min, :max
-
-    def initialize(name)
-      @name = name
-      reset
-    end
-
-    # Resets the internal counters so you can start sampling again.
-    def reset
-      @sum = 0.0
-      @sumsq = 0.0
-      @last_time = Time.new
-      @n = 0.0
-      @min = 0.0
-      @max = 0.0
-    end
-
-    # Adds a sampling to the calculations.
-    def sample(s)
-      @sum += s
-      @sumsq += s * s
-      if @n == 0
-        @min = @max = s
-      else
-        @min = s if @min > s
-        @max = s if @max < s
-      end
-      @n+=1
-    end
-
-    # Dump this Stats object with an optional additional message.
-    def dump(msg = "", out=STDERR)
-      out.puts "#{msg}: #{self.to_s}"
-    end
-
-    # Returns a common display (used by dump)
-    def to_s  
-    "[#{@name}]: SUM=%0.4f, SUMSQ=%0.4f, N=%0.4f, MEAN=%0.4f, SD=%0.4f, MIN=%0.4f, MAX=%0.4f" % [@sum, @sumsq, @n, mean, sd, @min, @max]
-    end
-
-
-    # Calculates and returns the mean for the data passed so far.
-    def mean
-      @sum / @n
-    end
-
-    # Calculates the standard deviation of the data so far.
-    def sd
-      # (sqrt( ((s).sumsq - ( (s).sum * (s).sum / (s).n)) / ((s).n-1) ))
-      begin
-        return Math.sqrt( (@sumsq - ( @sum * @sum / @n)) / (@n-1) )
-      rescue Errno::EDOM
-        return 0.0
-      end
-    end
-
-
-    # Adds a time delta between now and the last time you called this.  This
-    # will give you the average time between two activities.
-    #
-    # An example is:
-    #
-    #  t = Stats.new("do_stuff")
-    #  10000.times { do_stuff(); t.tick }
-    #  t.dump("time")
-    #
-    def tick
-      now = Time.now
-      sample(now - @last_time)
-      @last_time = now
-    end
-  end
-end
diff --git a/test/test_suite.rb b/test/test_suite.rb
new file mode 100644
index 0000000..e3bb0dc
--- /dev/null
+++ b/test/test_suite.rb
@@ -0,0 +1,3 @@
+Dir.glob('test/unit/*').select { |path| path =~ /^test\/unit\/test_.*\.rb$/ }.each do |test_path|
+  require test_path
+end
diff --git a/test/unit/test_semaphore.rb b/test/unit/test_semaphore.rb
new file mode 100644
index 0000000..5ce70f7
--- /dev/null
+++ b/test/unit/test_semaphore.rb
@@ -0,0 +1,118 @@
+root_dir = File.join(File.dirname(__FILE__), "../..")
+require File.join(root_dir, "test/test_helper")
+require File.join(root_dir, "lib/mongrel/semaphore")
+
+class TestSemaphore < Test::Unit::TestCase
+  def setup
+    super
+    
+    @semaphore = Semaphore.new
+  end
+  
+  def test_wait_prevents_thread_from_running
+    thread = Thread.new { @semaphore.wait }
+    give_up_my_time_slice
+    
+    assert thread.stop?
+  end
+  
+  def test_signal_allows_waiting_thread_to_run
+    ran = false
+    thread = Thread.new { @semaphore.wait; ran = true }
+    give_up_my_time_slice
+    
+    @semaphore.signal
+    give_up_my_time_slice
+    
+    assert ran
+  end
+  
+  def test_wait_allows_only_specified_number_of_resources
+    @semaphore = Semaphore.new(1)
+    
+    run_count = 0
+    thread1 = Thread.new { @semaphore.wait; run_count += 1 }
+    thread2 = Thread.new { @semaphore.wait; run_count += 1 }
+    give_up_my_time_slice
+    
+    assert_equal 1, run_count
+  end
+  
+  def test_semaphore_serializes_threads
+    @semaphore = Semaphore.new(1)
+    
+    result = ""
+    thread1 = Thread.new do
+      @semaphore.wait
+      4.times do |i|
+        give_up_my_time_slice
+        result << i.to_s
+      end
+      @semaphore.signal
+    end
+    
+    thread2 = Thread.new do
+      @semaphore.wait
+      ("a".."d").each do |char|
+        give_up_my_time_slice
+        result << char
+      end
+      @semaphore.signal
+    end
+    
+    give_up_my_time_slice
+    @semaphore.wait
+    
+    assert_equal "0123abcd", result
+  end
+  
+  def test_synchronize_many_threads
+    @semaphore = Semaphore.new(1)
+    
+    result = []
+    5.times do |i|
+      Thread.new do
+        @semaphore.wait
+        2.times { |j| result << [i, j] }
+        @semaphore.signal
+      end
+    end
+    
+    give_up_my_time_slice
+    @semaphore.wait
+    
+    5.times do |i|
+      2.times do |j|
+        assert_equal i, result[2 * i + j][0]
+        assert_equal j, result[2 * i + j][1]
+      end
+    end
+  end
+  
+  def test_synchronize_ensures_signal
+    @semaphore = Semaphore.new(1)
+    threads = []
+    run_count = 0
+    threads << Thread.new do
+      @semaphore.synchronize { run_count += 1 }
+    end
+    threads << Thread.new do
+      @semaphore.synchronize { run_count += 1; raise "I'm throwing an error." }
+    end
+    threads << Thread.new do
+      @semaphore.synchronize { run_count += 1 }
+    end
+    
+    give_up_my_time_slice
+    @semaphore.wait
+    
+    assert !threads.any? { |thread| thread.alive? }
+    assert_equal 3, run_count
+  end
+  
+  private
+  
+  def give_up_my_time_slice
+    sleep(0)
+  end
+end \ No newline at end of file
diff --git a/test/unit/test_threading.rb b/test/unit/test_threading.rb
new file mode 100644
index 0000000..e577bbb
--- /dev/null
+++ b/test/unit/test_threading.rb
@@ -0,0 +1,82 @@
+root_dir = File.join(File.dirname(__FILE__), "../..")
+require File.join(root_dir, "test/test_helper")
+
+include Mongrel
+
+class FakeHandler < Mongrel::HttpHandler
+  @@concurrent_threads = 0
+  @@max_concurrent_threads = 0
+  
+  def self.max_concurrent_threads
+    @@max_concurrent_threads ||= 0
+  end
+  
+  def initialize
+    super
+    @@mutex = Mutex.new
+  end
+  
+  def process(request, response)
+    @@mutex.synchronize do
+      @@concurrent_threads += 1 # !!! same for += and -=
+      @@max_concurrent_threads = [@@concurrent_threads, @@max_concurrent_threads].max
+    end
+    
+    sleep(0.1)
+    response.socket.write("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nhello!\n")
+  ensure
+    @@mutex.synchronize { @@concurrent_threads -= 1 }
+  end
+end
+
+class ThreadingTest < Test::Unit::TestCase
+  def setup
+    @valid_request = "GET / HTTP/1.1\r\nHost: www.google.com\r\nContent-Type: text/plain\r\n\r\n"
+    @port = process_based_port
+    
+    @max_concurrent_threads = 4
+    redirect_test_io do
+      @server = HttpServer.new("127.0.0.1", @port, :max_concurrent_threads => @max_concurrent_threads)
+    end
+    
+    @server.register("/test", FakeHandler.new)
+    redirect_test_io do
+      @server.run
+    end
+  end
+
+  def teardown
+    redirect_test_io do
+      @server.stop(true)
+    end
+  end
+
+  def test_server_respects_max_current_threads_option
+    threads = []
+    (@max_concurrent_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
+    end
+    while threads.any? { |thread| thread.alive? }
+      sleep(0)
+    end
+    assert_equal @max_concurrent_threads, FakeHandler.max_concurrent_threads
+  end
+  
+  private
+  
+  def send_data_over_socket(string)
+    socket = TCPSocket.new("127.0.0.1", @port)
+    request = StringIO.new(string)
+
+    while data = request.read(8)
+      socket.write(data)
+      socket.flush
+      sleep(0)
+    end
+    sleep(0)
+    socket.write(" ") # Some platforms only raise the exception on attempted write
+    socket.flush
+  end
+end \ No newline at end of file
diff --git a/test/unit/test_ws.rb b/test/unit/test_ws.rb
index 7508c7f..2510c3a 100644
--- a/test/unit/test_ws.rb
+++ b/test/unit/test_ws.rb
@@ -27,9 +27,8 @@ class WebServerTest < Test::Unit::TestCase
     @tester = TestHandler.new
     @app = Rack::URLMap.new('/test' => @tester)
     redirect_test_io do
-      # We set num_processors=1 so that we can test the reaping code
-      @server = HttpServer.new("127.0.0.1", @port, @app, :num_processors => 1)
-      @server.start!
+      # 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)
     end
   end
 
@@ -90,7 +89,7 @@ class WebServerTest < Test::Unit::TestCase
     end
   end
 
-  def test_num_processors_overload
+  def test_max_queued_threads_overload
     redirect_test_io do
       assert_raises Errno::ECONNRESET, Errno::EPIPE, Errno::ECONNABORTED, Errno::EINVAL, IOError do
         tests = [