Skip to content

Instantly share code, notes, and snippets.

@tinomen
Created June 29, 2011 01:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tinomen/1052710 to your computer and use it in GitHub Desktop.
Save tinomen/1052710 to your computer and use it in GitHub Desktop.
goliath demo
1. gem bundler install
2. bundle install
3. start the server - ruby srv.rb
4. run client - ruby client.rb
#!/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
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'
#!/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