Class Mongrel::HttpServer
In: lib/mongrel.rb
Parent: Object

This is the main driver of Mongrel, while the Mognrel::HttpParser and Mongrel::URIClassifier make up the majority of how the server functions. It’s a very simple class that just has a thread accepting connections and a simple HttpServer.process_client function to do the heavy lifting with the IO and Ruby.

You use it by doing the following:

  server = HttpServer.new("0.0.0.0", 3000)
  server.register("/stuff", MyNifterHandler.new)
  server.run.join

The last line can be just server.run if you don’t want to join the thread used. If you don’t though Ruby will mysteriously just exit on you.

Ruby’s thread implementation is "interesting" to say the least. Experiments with many different types of IO processing simply cannot make a dent in it. Future releases of Mongrel will find other creative ways to make threads faster, but don’t hold your breath until Ruby 1.9 is actually finally useful.

Methods

new   process_client   register   run   unregister  

Attributes

acceptor  [R] 

Public Class methods

Creates a working server on host:port (strange things happen if port isn’t a Number). Use HttpServer::run to start the server.

The num_processors variable has varying affects on how requests are processed. You‘d think adding more processing threads (processors) would make the server faster, but that’s just not true. There’s actually an effect of how Ruby does threads such that the more processors waiting on the request queue, the slower the system is to handle each request. But, the lower the number of processors the fewer concurrent responses the server can make.

20 is the default number of processors and is based on experimentation on a few systems. If you find that you overload Mongrel too much try changing it higher. If you find that responses are way too slow try lowering it (after you’ve tuned your stuff of course). Future versions of Mongrel will make this more dynamic (hopefully).

Public Instance methods

Does the majority of the IO processing. It has been written in Ruby using about 7 different IO processing strategies and no matter how it’s done the performance just does not improve. It is currently carefully constructed to make sure that it gets the best possible performance, but anyone who thinks they can make it faster is more than welcome to take a crack at it.

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.

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.

Removes any handler registered at the given URI. See Mongrel::URIClassifier#unregister for more information.

[Validate]