Created
May 24, 2013 07:50
-
-
Save anonymous/5641961 to your computer and use it in GitHub Desktop.
This file contains 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 'goliath' | |
class Echo < Goliath::API | |
use Goliath::Rack::Params | |
use Goliath::Rack::Validation::RequiredParam, {:key => 'delay'} | |
def response(env) | |
EM::Synchrony.sleep params['delay'] | |
[200, {}, params['delay']] | |
end | |
end |
This file contains 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 'em-http-request' | |
EM.run do | |
conn = EM::HttpRequest.new('http://localhost:9000/') | |
start = Time.now | |
r1 = conn.get :query => {delay: 1.5}, :keepalive => true | |
r2 = conn.get :query => {delay: 1.0} | |
r2.callback do | |
p Time.now - start # => 1.5 - keep-alive + pipelining | |
EM.stop | |
end | |
end |
This file contains 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 "em-synchrony" | |
require "em-synchrony/em-http" | |
EventMachine.synchrony do | |
start = Time.now | |
multi = EventMachine::Synchrony::Multi.new | |
multi.add :a, EventMachine::HttpRequest.new("http://localhost:9000?delay=1").aget | |
multi.add :b, EventMachine::HttpRequest.new("http://localhost:9000?delay=1.5").aget | |
res = multi.perform | |
p "Look ma, no callbacks, and parallel HTTP requests!" | |
p Time.now - start # => 1.5 - keep-alive + pipelining | |
EventMachine.stop | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment