-
-
Save zeeshanlakhani/1604879 to your computer and use it in GitHub Desktop.
Testing the combination of Async Sinatra with Em::HttpRequest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment