public
Created — forked from yob/gist:967934

Testing the combination of Async Sinatra with Em::HttpRequest

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
require 'rack'
require 'sinatra'
require 'sinatra/async'
require 'em-http-request'
 
# Trying out sinatra in async request mode.
#
# Run it like so:
#
# thin -R test.rb -p 4000 start
#
# Test it like so:
#
# ab -n 10 -c 10 http://127.0.0.1:4000/twitter.json
#
class Test < Sinatra::Base
 
# DNS queries aren't async on my system for some reason, so avoid them
TWITTER_HOST = "199.59.148.83"
 
register Sinatra::Async
 
aget '/' do
body "hello async"
end
 
aget '/twitter.json' do
http = EM::HttpRequest.new("http://#{TWITTER_HOST}/statuses/public_timeline.json").get
http.errback {
body {
"oh oh, error"
}
}
http.callback {
body {
http.response
}
}
end
 
end
 
Test.run

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.