Monthly Archives: February 2008

Staying Alive with Thin!

TravoltaThin was the first Ruby server to be able to connect to UNIX domain sockets, giving you a little more speed, so you could spend less time browsing and more time dancing and eating ice creams with the people you love. But only Nginx (that I know of) supports UNIX domain sockets. Non-Nginx users might like to have time to dance and eat ice creams with the people they love too!

So that’s why Thin new release (0.7.0 codename Spherical Cow) supports persistent connections (aka Keep-Alive).

Apache & mod_proxy

Under Apache, if a client requests a persistent connection, the connection to the backend server (through mod_proxy) will also be persistent. That means the time spent opening the connection is saved for subsequent requests because all requests will be sent through the same connection. Even cooler is that you have nothing to do to set that up, unless you’ve turned Keep-Alive off.

I ran some benchmarks and I got an average gain of 200 req/sec with Keep-Alive on.
Keep-Alive benchmark

Sadly Nginx doesn’t support persistent connections to backends yet, but it seems to be a highly requested feature, and Igor Syseov (author of Nginx) said he’s working on it several times on the mailing list. Imagine that: UNIX sockets + persistent connections, oooh man!

Hey we develop too, sometimes!

Of course you’ll take advantage of that feature when it’s just you, Thin and your browser. And we can also benchmark it, just for fun, to get numbers that are sure to give you goose bumps: 7800 req/sec is just, ooooh, aaahh, hummm, yeah, that was good!

Keep-Alive

Swiftiply

If you’re looking for even more speed. Thin can now be used as a Swiftiply client. And it’s very easy to use:

thin start --servers 3 --port 8000 --swiftiply

Just add the --swiftiply option. This also means that any Rack adapter can be run through Swiftiply (using the --rackup option) including Ramaze, Camping, Merb, YourCrazyLittleFrameworkThatIsSoooBetterThenAllTheOtherAndThatSupportRack etc.

Speed with control

But what is speed with no control, right?

The main reason why Mongrel couldn’t support persistent connections was because of Ruby 1024 file/socket descriptors limitation. If you don’t close the connection (keep them alive) it’s one less descriptor you can use to process another connection or open a file.

Although EventMachine doesn’t have an infinite number of file descriptors it was reported to handle more then 20 000 concurrent connections.

You can now tune the number of connections a Thin server can handle.

--max-conns: This sets the maximum number of concurrent connections your Thin server can handle. Setting higher then 1024 might require superuser privileges on some system.

--max-persistent-conns: This sets the maximum number of persistent connections your Thin server can handle at the same time. If resource usage is important, you might want to turn that down. You can turn Keep-Alive support off by setting to 0.

Get it!

Spherical Cow also comes with a couple bug fixes and tweak.
As usual, you can get the latest version from RubyForge:

sudo gem install thin

If you have any question, join the Google Group or the #thin channel on freenode.

I hope you like it!

16 Comments

Filed under rails, ruby, thin

MoR7 Presentation

ImageHere’s the code and slides of my presentation : http://github.com/macournoyer/mor7/

Hope you liked it!

1 Comment

Filed under montreal, rails, StandoutJobs, thin

Presenting Thin at Montreal on Rails

MoRIf you’re in Montreal next Tuesday, be sure to come to the next Montreal on Rails. I’ll be presenting Thin with a fanfare, dancers and pizza.

RSVP if not already done and do it fast, places are limited.

I’ll present some new features in the upcoming Spherical Cow release and recode Thin like I did at DempCampCUSEC, but this time, with one hand tied being my back and someone yelling at me in Russian. All this in 2 minutes (I don’t know yet what I’ll do with the remaining time).

4 Comments

Filed under conference, montreal, ruby, thin

The Flexible Thin Anorexic Gymnast that Democratized Deployment

ImageThin is fast, I don’t think I need to prove that again. But what I’d like to showcase now is Thin extensibility. Most of it is due to Thin being based on Rack. It’s also why lots of framework are supporting Thin already.

Can Thin replace all images with LOLCAT pics on my site when it’s my birthday of a leap year, plz, plz, plz? Cause it should!

Yeah, you’re not the first one to ask.

config.ru file

require 'open-uri'
require 'hpricot'

class LolCater
  def initialize(app)
    @app = app
  end
  
  def call(env)
    status, headers, body = @app.call(env)
    
    if iz_ma_burdae? && leap_year?
      doc = Hpricot(body)
      doc.search('img').set('src', lolcat_pic_url)
      body = doc.to_html
    end
    
    [status, headers, body]
  end
  
  private
    def iz_ma_burdae?
      Date.today.month == 11 && Date.today.day == 7
    end
    
    def leap_year?
      Date.today.leap?
    end
  
    def lolcat_pic_url
      doc = Hpricot(open('http://icanhascheezburger.com/?random#top'))
      pic = (doc/'div.snap_preview img').first.attributes['src']
    end
end

use LolCater
run Rack::Adapter::Rails.new(:root => '/path/to/my/app')

That's called a Rack middleware. It must have a call method and receive a Rack app as the first argument of new. You then tell Thin to use this middleware when running your Rails application. Save the file as config.ru. .ru is for Rackup config file.

You can now launch your application with the thin script:

thin start --rackup config.ru

Wai-wai-wait that means I can built my own framework in like 30 LOC and use all of Thin goodness?

Right! You can start a cluster of your lolcat image replacer app like you would for a Rails app, but specify the --rackup option, which tell Thin to load your application from this file instead to go with the default Rails adapter.

thin start --rackup config.ru --servers 3

In fact, here's a lil' framework I built in 35 LOC:

# Start w/ thin start -r invisible.rb
require 'tenjin'

module ::Invisible
  class Adapter
    def initialize
      @template = Tenjin::Engine.new(:postfix=>'.rbhtml', :layout=>'../layout.rbhtml', :path=>'home')
      @file = Rack::File.new('public')
    end  
    def call(env)
      path = env['PATH_INFO']
      if path.include?('.')
        @file.call(env)
      else
        _, controller, action = env['PATH_INFO'].split('/')
        Invisible.const_get("#{(controller || 'home').capitalize}Controller").new(@template, env).call(action || 'index')
      end
    end
  end
  class Controller
    def initialize(template, env)
      @template, @status, @env, @headers = template, 200, env, {'Content-Type' => 'text/html'}
    end
    def call(action)
      send(action)
      render(action) unless @body
      [@status, @headers.merge('Content-Length'=>@body.size.to_s), [@body]]
    end
    protected
      def render(action=nil)
        @body = @template.render(action.to_sym, instance_variables.inject({}) {|h,v| h[v[1..-1]] = instance_variable_get(v); h})
      end
  end
end

require 'app/controllers'
run Invisible::Adapter.new

Full code on my github repo: http://github.com/macournoyer/invisible/.

And you know I can't help myself but benchmark it:
merb-core: 1865.19 req/sec
invisible: 2428.17 req/sec

(Disclamer: it's just for fun, don't use that framework ever or I will come in your house when you sleep and steel all your left foot socks).

Democratizing deployment

I don't know if you get what this means? It means, deploying a Rails or Merb, Ramaze, etc, app is just a matter of writing a simple Rack config file and playing w/ the --rackup option. It's all the same script and tools now!

For example, there's a Ramaze Rack config file in example/ramaze.ru, so to deploy your Ramaze application you would.

thin start --servers 3 --rackup ramaze.ru

Or create a config file and install it as a runlevel script:

thin config --rackup ramaze.ru -C myapp.yml
edit myapp.yml
sudo thin install # Installs the runlevel script
mv myapp.yml /etc/thin/myapp.yml

Or if you prefer to monitor your clusters with God, check out the sample God config file that is a drop-in replacement for Thin's runlevel script.

Happy hacking & deploying!

12 Comments

Filed under ruby, thin, tutorial