Skip to content

Instantly share code, notes, and snippets.

@jbaudanza
Created February 8, 2011 21:55
Show Gist options
  • Save jbaudanza/817346 to your computer and use it in GitHub Desktop.
Save jbaudanza/817346 to your computer and use it in GitHub Desktop.
Example of an asynchronous rack based app on heroku
# config.ru
#
# The Heroku architecture is based on Thin, which is based on EventMachine. This means
# you can access the EventMachine API and make use of Thin's asynchronous extension to
# the rack API.
#
# This is a simple asynchronous rack backed app that will hold a request for 4 seconds
# and respond with "Hello".
#
# In theory an app structured like this should be able to handle a huge number of
# simulatenous requests, however, the heroku routing mesh seems to throttle your
# dyno back to 2 or 3 requests at a time.
#
# You could potentially use the EventMachine API as a free alternative to scheduling
# a job on a worker, although it's probably not a good idea.
#
# This can be run like any other heroku app. Throw this file into config.ru and
# "git push heroku master"
run lambda { |env|
EM.add_timer(4) do
# Complete the response asychronously
env['async.callback'].call([200, {'Content-Type' => 'text/plain'}, "hello\r\n"])
end
# This informs Thin that the request will be completed asynchronously
throw :async
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment