Friday, August 01, 2008

Use mongrel handler for slow requests

I work in a industry where almost everything is a API call to a remote and slow system usually written in java. Rails is not at all the way to implement such things. I think the easiest way to implement this from ruby world is to write a mongrel handler.


class SleepHandler < Mongrel::HttpHandler
def process(request, response)
response.start do |head,out|
head["Content-Type"] = "text/html"
sleep 300 # Long running task
out.write("test") # Push out the result
end
end
end

#Handler configs
config = Mongrel::Configurator.new :host => ARGV[0], :port => ARGV[1] do
listener(:num_processors => 80, :timeout => 400) do
uri "/sleep", :handler => SleepHandler.new
uri "/nosleep", :handler => NoSleepHandler.new
end

trap("INT") { stop }
run
end

puts "Mongrel running on #{ARGV[0]}:#{ARGV[1]} with docroot #{ARGV[2]}"
config.join




I stressed this mongrel handler with 50 concurrent requests and everything came back in 5 min and 45 secs and maximum memory comsumption was 32MB so in my view decent performance by mongrel.

No comments: