When Rails speed matters

This is also the type of thing where you could write a custom Mongrel handler class to do this part of your app. Its a bad idea tying up rails for 2 seconds while doing an http request to another server precisely because of the rails only dispatches one request at a time and queues the rest. This happens in mongrel because there is a big mutex around the rails dispatcher so it only does one req at a time. But mongrel itself is multi threaded and perfectly capable of serving multiple requests at the same time. You just have to write your own handler for it. Maybe this will get your started:

http://brainspl.at/merb.zip

Merb == Mongrel + Erb. Its just a simple mongrel handler that maps urls to class names that can return erb templates. Very lightweight but capable of serving 600 dynamic req/sec for those times when you need it.

A Merb controller looks like this:

class Test < Merb::Controller
   template_dir :test

   def hello(*names)
     # Assign the parameter to an instance variable
     @name = names.join(', ')
     render
   end
end

-- Template
<html>
<head>
   <title>Hello, <%= @name %></title>
</head>
<body>
<h1>Hello, <%= @name %>!</h1>
</body>
</html>

When you run the server.rb and hit http://localhost:4000/test/hello/david this will call this:

Test.new.hello('david') which will fill out and return the template to the browser. Since this is not rails, mongrel can serve requests more then one at a time so its much faster. This is a really lightweight simple project but you can mount it at a certain url in your app to integrate it with your rails app.

-Ezra

Damn, I don't get to answer any questions anymore. You guys are just too smart.