Created
June 4, 2012 16:39
-
-
Save tenderlove/2869461 to your computer and use it in GitHub Desktop.
An adapter for Rack that makes it look like node
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
## | |
# Make Rack look like node. | |
# | |
# http://youtu.be/Zp91yUH-zAw | |
# | |
# Run like this: | |
# | |
# $ unicorn -I. app.ru | |
require 'node_adapter' | |
use Rack::Chunked | |
run createServer { |req, res| | |
res.writeHead(200, {'Content-Type' => 'text/plain'}) | |
10.times do | |
res.chunk "hi mom!\n" | |
sleep 0.5 | |
end | |
res.end "Hello World\n" | |
} |
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 'thread' | |
class Adapter | |
class Latch # :nodoc: | |
def initialize | |
@mutex = Mutex.new | |
@cond = ConditionVariable.new | |
@released = false | |
end | |
def release | |
@mutex.synchronize { | |
@released = true | |
@cond.broadcast | |
} | |
end | |
def await | |
@mutex.synchronize { | |
@cond.wait @mutex unless @released | |
} | |
end | |
end | |
class Buffer < Struct.new(:q) | |
def each | |
while part = q.pop | |
yield part | |
end | |
end | |
end | |
class << self; attr_accessor :block; end | |
def self.call env | |
new(block).call env | |
end | |
def initialize block | |
@block = block | |
@code = 200 | |
@header = {} | |
@body_written = Latch.new | |
@q = ::Queue.new | |
end | |
def call env | |
Thread.new { @block.call(env, self) } | |
# Wait for the child thread to write to the body, | |
# effectively "closing" the response. | |
@body_written.await | |
[@code, @header, Buffer.new(@q)] | |
end | |
def writeHead code, header | |
@code = code | |
@header = header | |
end | |
def chunk string | |
@body_written.release | |
@q.push string | |
end | |
def end string | |
@body_written.release | |
@q.push string | |
@q.push nil | |
end | |
end | |
def createServer &block | |
Adapter.block = block | |
Adapter | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment