Created
May 15, 2012 16:15
-
-
Save lian/2702979 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 'bundler/setup' | |
require 'reel' | |
require 'rack' | |
class RequestActor | |
include Celluloid | |
def initialize(app, connection); @app, @connection = app, connection; end | |
def run | |
env = @connection.request.headers | |
# TODO prepare real env. below is just enough to get this simple case work. | |
env['QUERY_STRING'] = "/" | |
status, headers, body = @app.call(env) | |
out = ""; body.each{|chunk| out << chunk }; body.close | |
# TODO pass status and headers | |
@connection.respond :ok, out | |
end | |
end | |
module Rack::Handler | |
class Reel | |
include Celluloid::IO | |
def initialize(host, port, app) | |
@app = Rack::CommonLogger.new(app, STDOUT) | |
@server = TCPServer.new(host, port) | |
run! | |
end | |
def finalize; @server.close; end | |
def run; loop { handle_connection! @server.accept }; end | |
def handle_connection(socket) | |
connection = ::Reel::Connection.new(socket) | |
begin | |
connection.read_request | |
next unless connection.request | |
RequestActor.new(@app.dup, connection).run | |
end while connection.alive? | |
rescue EOFError | |
p [EOFError] | |
end | |
end | |
end | |
app = proc{|env| | |
sleep 10 | |
[200, {}, [env.inspect]] | |
} | |
n = 50 | |
n.times{ app = Rack::CommonLogger.new(app, STDOUT) } | |
addr, port = '127.0.0.1', 1234 | |
puts "*** Starting server on #{addr}:#{port}" | |
Rack::Handler::Reel.new(addr, port, app) | |
sleep 2 | |
3.times{ system("curl -v localhost:1234/ &") } | |
sleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment