I’ve been playing with Thin, the Ruby web server which packages the awesomeness of Rack, Mongrel and Eventmachine (\m/). However, the thing that blew me away completely was the James Tucker’s Async Rack that was integrated to Thin. The combination opens a whole new world of realtime responses, something that I’ve been constantly switching to NodeJS and SocketIO for.
Async Rack
A simple rack application would look like this
# my_rack_app.ru
class MyRackApp
def call(env)
[200, {'Content-Type' => 'text/html'}, ["Hello World"]]
end
end
run MyRackApp.new
# thin start -R my_rack_app.ru -D -p3000
This simple rack app on hitting port 3000 responds with the a status code (200), the content-type and some body. Nothing special. With async rack we’d be able to send the response head back while we build the response. Once the response is built we send the body and close the connection.
A quick glance at Patrick’s Blog (apologies for not being able to find the last name of the author of this blog) should give you an excellent understanding of what I’m trying to explain
A simple async app
# my_async_app.ru
AsyncResponse = [-1, {}, []].freeze
class MyAsyncApp
def call(env)
Thread.new do
sleep(10)
body = [200, {"Content-Type" => "text/html"}, ["<em > This is Async </em >"]]
env['async.callback'].call(body)
end
AsyncResponse
end
end
The AsyncResponse is a constant which returns a -1 status which tells the server that the response will be coming later asynchronously. It, as defined in the examples provided in thin an “Async Template” Async Template
On a request the code initially returns an AsyncResponse while the thread waits on the sleep request. Once the thread is active again it builds the response and sends the response keeping the connection alive.
An async app where we build the response and close the connection
From here on, it would really help to have the thin code open in your text editor. We would be looking at the request.rb, response.rb and the connection.rb methods from /thin folder.
require 'rubygems'
class DeferrableBody
include EventMachine::Deferrable
def each &blk
puts "Blocks: #{blk.inspect}"
@callback_blk = blk
end
def append(data)
puts " -- appending data --"
data.each do |data_chunk|
puts " -- triggering callback --"
puts @callback_blk.call(data_chunk)
end
end
end
class AsyncLife
AsyncResponse = [-1, {}, []].freeze
def call(env)
body = DeferrableBody.new
EM.next_tick {
puts "Next tick before async"
sleep(3)
puts "#{env['async.callback']}"
env['async.callback'].call([200, {'Content-Type' => 'text/plain'}, body])
sleep(5)
}
puts "-- activated 5 second timer --"
EM.add_timer(5) {
puts "--5 second timer done --"
body.append(['Mary had a little lamb'])
puts "-- activated 2 second timer --"
EM.add_timer(2){
puts "--7 second timer done --"
body.append(['This is the house that jack built'])
puts "-- succeed called -- "
body.succeed
}
}
AsyncResponse
end
end
run AsyncLife.new
This example uses Eventmachine Deferrable which allow us to determine when we’re done building our response. But the tricky part I struggled to get my head around was the strangle looking each method and how it uses the append method to build our responses.
Walking through the code
When the request comes in from the browser the application renders the AsyncResponse constant which tells the server that the response body is going to be built over time. Now on request eventmachine’s ( checkout thin/connection.rb ) post_init method creates a @request and @response object. This triggers a post_process method which on the first call returns without setting the header, status or body and prepares for the asynchronous response.
On next_tick we begin to create our header. We initialize a EM::Deferrable object which is assigned as the response body and this ensures the header is sent ahead of the body (because we don’t have anything to iterate over in the each block where the response is sent) The env[‘async.callback’] is a closure for the method post_process which is created by method(:post_process) checkout the pre_process method in the thin/connection.rb
The each method in our Deferrable class overrides the default each implementation defined by the response object. So now our each block is saved in an instance variable @callback_blk which is call ed when we call the append method. So essentially we are calling send_data on each of the data blocks we’re sending back when we call the append method.
Once that’s done we call succeed which tells Eventmachine to trigger the callback to denote we’re done building the body. It ensures the request response connection is closed.
The default each implementation
# Send the response
@response.each do |chunk|
trace { chunk }
send_data chunk
end
Thats pretty much what I gathered by going through the code on async response with thin and rack. Another useful module is the thin_async bit written by the creator of thin @macournoyer available here . This pretty much abstracts the trouble of overriding the each block.
Here’s an example of a simple long polling server I built using thin available here
Hope this is helpful