Starting to Get Thin, v0.6.3 6

Posted by Justin Reagor Wed, 13 Feb 2008 03:16:00 GMT

When I first heard about Thin I was slightly intrigued, but less enthusiastic when I attempted to run it (not sure what version). Anyway, now that Thin has matured a bit more, and Merb 0.9 is bringing the love of Rack to the masses… I’m really really really starting to see what all the fuss is about. 3000+ requests a second of fuss.

The Skinny

Thin is actually a mash-up of current back-end web serving technologies.

  1. the Mongrel parser, I’m speculating its the Ragel executable state machine portion
  2. Event Machine, and the tough, concurrent service request handling of the Reactor Pattern
  3. and Rack, our new hero which brings a common web server gateway interface to all of our favorite Ruby web frameworks.

Fancy technologies (and citations), but how’s this going to help good’ole Geoffrey Grosenbach?

Installation

$ sudo gem install thin

...or if you know what your doing…

$ git clone git://github.com/macournoyer/thin.git

If not, go directly to FAIL

Show’em Some Ruby

Lets take a quick glimpse at the adapter example that is included with the gem (examples/adapter.rb).

I’ll try not to ramble on about Rack too long but… This really shows you how damn simple it can be to plug and play applications into a Rack stack, and the power that Thin brings along with its speed!

require File.dirname(__FILE__) + '/../lib/thin'

class SimpleAdapter
  def call(env)
    body = ["hello!"]
      [      200,  
        {
          'Content-Type'   => 'text/plain',
          'Content-Length' => body.join.size.to_s,
        }, body
      ]  
   end
end 

Thin::Server.start('0.0.0.0', 3000) do  

  use Rack::CommonLogger

  map '/test' do    
    run SimpleAdapter.new  
  end  

  map '/files' do    
    run Rack::File.new('.')  
  end

  run Rack::Adapter::Rails.new(:root =>/Users/justin/apps/whatsyomammabeensmokin.com‘)
end

Very briefly, this example is a thin/Thin adapter specification and server initialization. Its also an insanely small example of a Rack adapter (which may, or may not work; reference the real examples in the gem).

We can see, after loading our necessary library file, we create a class used as an adapter, called SimpleAdapter.

This holds in it a means to handle status code 200 requests. With a simple output of plain text content-type, set in its header hash. It also includes a simple body of text, “hello!” to be returned as content. Literally the Rack adapter, handling a request, need only return this sort of array as a response. [status, header, body].

Finishing up the script is Thin’s server stack initialization. Resembling a Rack config.ru configuration file, Thin initializes a server for our localhost on port 3000. As well as some middle ware setups using our SimpleAdapter.

First, inside the setup block you should quickly notice a few “map” method calls that resemble Rails routes. “map” is actually just that, a simple way to set Rack::URLMap’s, or web URI paths, inside the main server start block.

Second, the “use” method actually adds middle ware to the stack.

Finally, notice “run”. This literally dispatches middle ware logic to the server.

I’ve taken the liberty of adding a call to a local Rails application to point out how easy it is to include new applications straight into a running Rack/Thin server stack. Pretty much just as easy as it is to “include” a mix-in into a class in Ruby.

If you would like to work more closely with some real code, take a look here…

Thin Rails

Ok… woosh, back to the easy stuff…

Since Thin supports Rails natively through what I believe is an included Rack adapter… you can start playing with it right out of the box on any of your current Rails projects.

Once you have the Thin gem installed, simply do a…

$ thin start

If you’d like to explicitly set your Rails environment…

$ thin start -e development

“—help” provides more information as usual. Thin’s main site has a lovely amount of info with a beautiful web design. Be it tiny lil text (get it, thin).

You can also try out a middle ware script that comes with Thin called “stats”.

$ thin --stats=/public_thin/stats start

This will give you a technical details of your Thin server stats, map’d to “localhost:3000/public_thin/stats”.

To Deployment, and Beyond

I have yet to really attempt a deployment with Thin, but I have one coming up shortly to assist with and will definitely be trying this out.

From what I’ve read you can just as easily replace mongrel_cluster with thin like so.

# thin config -C config/thin.yml --servers 3 --port 5000 --chdir ...
# thin start -C config/thin.yml

If anyone has tried this yet please let me know how it went.

The Year 2000

Hopefully your interested now and will check into this speedy fast Mongrel replacement. Did I even mention it was fast…? >;)

In the near future I’ll attempt to report back on deployments and middle ware scripts. I’m eager to see what useful little apps I can come up with that sit between the web server and my actual Rails application. But I can imagine 8 bazillion possibilities (especially logging and transparent statistics harvesting).

So yeah… cheers to the author!!