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!!

Comments

Leave a response

  1. Avatar
    macournoyer about 3 hours later:
    Hey Justin, thx for the great review of my project. I'm glad you like it. If you have any problem or question about Thin, feel free to join the Google group http://groups.google.com/group/thin-ruby/topics or come chat on #thin on freenode. And stay tuned for the next big release (0.7) for 2 big surprises (well not real surprises if you spy on me on the git repo, but act like you were surprised ok?).
  2. Avatar
    Justin about 13 hours later:
    I'm not 100% sure if Ruby is all that great for my ADD, seeing as there is just great projects scattered across the community and its hard to stay focused on one! Thin is definitely turning out to be a lovely lil server and its definitely got me hooked in... Hope to contribute back at some point, thanks!
  3. Avatar
    Geoffrey Grosenbach about 15 hours later:

    Yes, how IS this going to help me? That's what I'd like to know.

    Seriously, it looks promising. From my benchmarks, the benefit is more noticeable on leaner actions that don't do much processing.

    However, having a speedy Ruby application server starts to open up the possibility of skipping the webserver altogether in some situations. If you can run Peter Cooper's SwitchPipe on the front, you may be able to get away with some thins on the backend and not need Nginx at all.

  4. Avatar
    macournoyer about 19 hours later:
    @Geoffrey: exactly! I'm already getting performances similar to Nginx w/ Swifitiply (even a tab faster, 4-5 req/sec!) which is coming in next release of Thin.
  5. Avatar
    Justin about 21 hours later:
    Yooo topfunky, thanks for letting me use your name there. I thought it was kinda funny, more then the typical "how's this going to help you out?". Yes, I agree on how promising this is. I am very eager to see what kinds of smaller, middle ware scripts/apps I can place in between my Rails/Merb applications and our clients. Interested in possibly starting to write my own statistical back ends to the apps I develop, as well as better real time visitor stats. Hoping that this lightweight framework ease some of that (which I'm pretty sure it can). I'm also EXTREMELY happy to hear that this could possibly remove the need for a web server all together. Though I do enjoy the classical rewrite rules and configurations of Nginx. Though, those are easily replaced, ;) Awww, poor web servers... sending out static content for the rest of their lives... Until mod_rubinius! ;) Speaking of mod_rubinius, I wonder if Thin will support that in any way (or rubinius in general).
  6. Avatar
    Justin about 21 hours later:
    I guess nobody really knows yet what mod_rubinius is going to be like for implementing, since its just now being started... :)
Comments