-
-
Save Epictetus/1054359 to your computer and use it in GitHub Desktop.
goliath demo
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
1. gem bundler install | |
2. bundle install | |
3. start the server - ruby srv.rb | |
4. run client - ruby client.rb |
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
#!/usr/bin/env ruby | |
require 'bundler' | |
Bundler.setup | |
# require 'eventmachine' | |
require "em-synchrony" | |
require 'em-synchrony/em-http' | |
EM.synchrony do | |
host = "169.254.134.208"#"localhost" | |
# perform 4 http requests in parallel, and collect responses | |
multi = EM::Synchrony::Multi.new | |
multi.add :page1, EM::HttpRequest.new("http://#{host}:9000?echo=WHOA").aget | |
multi.add :page2, EM::HttpRequest.new("http://#{host}:9000?echo=THERE").aget | |
multi.add :page3, EM::HttpRequest.new("http://#{host}:9000?echo=PARTNER").aget | |
multi.add :page4, EM::HttpRequest.new("http://#{host}:9000?echo=----").aget | |
data = multi.perform#.responses[:callback].values | |
puts "All done! Stopping event loop." | |
EM.stop | |
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
source 'http://rubygems.org' | |
gem 'goliath', :git => 'https://github.com/postrank-labs/goliath.git' | |
gem 'yajl-ruby' | |
gem 'eventmachine', :git => 'git://github.com/eventmachine/eventmachine.git', :require => 'eventmachine' | |
gem 'em-http-request', :git => 'https://github.com/igrigorik/em-http-request.git' | |
gem "em-synchrony", :git => 'https://github.com/igrigorik/em-synchrony.git' |
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
#!/usr/bin/env ruby | |
require 'bundler' | |
Bundler.setup | |
require 'goliath' | |
require 'goliath/plugins/latency' | |
# Goliath uses multi-json, so pick your favorite JSON serializer | |
# require 'json' | |
require 'yajl' | |
class Srv < Goliath::API | |
# use Goliath::Rack::Tracer # log trace statistics | |
use Goliath::Rack::DefaultMimeType # cleanup accepted media types | |
use Goliath::Rack::Render, 'json' # auto-negotiate response format | |
use Goliath::Rack::Params # parse & merge query and body parameters | |
# use Goliath::Rack::Heartbeat # respond to /status with 200, OK (monitoring, etc) | |
# If you are using Golaith version <=0.9.1 you need to Goliath::Rack::ValidationError | |
# to prevent the request from remaining open after an error occurs | |
#use Goliath::Rack::ValidationError | |
use Goliath::Rack::Validation::RequestMethod, %w(GET POST) # allow GET and POST requests only | |
use Goliath::Rack::Validation::RequiredParam, {:key => 'echo'} # must provide ?echo= query or body param | |
plugin Goliath::Plugin::Latency # output reactor latency every second | |
def process_request | |
logger.info env.params['echo'] | |
{response: env.params['echo']} | |
end | |
def response(env) | |
[200, {}, process_request] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment