about summary refs log tree commit homepage
path: root/lib/rainbows/event_machine.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-10-26 01:57:56 -0700
committerEric Wong <normalperson@yhbt.net>2009-10-26 02:26:22 -0700
commit8e9672d13ed07cc262894b4770c0b9c016359712 (patch)
treeaafa88a841ade6f076c46af19aa203b5bd8f5d26 /lib/rainbows/event_machine.rb
parent9248ead0ae16091ea0c362930391834c0f32891c (diff)
downloadrainbows-8e9672d13ed07cc262894b4770c0b9c016359712.tar.gz
This is should be compatible with how the Thin webserver
provides async callback support.

See http://github.com/raggi/async_sinatra for the details
Diffstat (limited to 'lib/rainbows/event_machine.rb')
-rw-r--r--lib/rainbows/event_machine.rb15
1 files changed, 13 insertions, 2 deletions
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index 678a771..7009fce 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -31,6 +31,9 @@ module Rainbows
       include Rainbows::EvCore
       G = Rainbows::G
 
+      # Apps may return this Rack response: AsyncResponse = [ -1, {}, [] ]
+      ASYNC_CALLBACK = 'async.callback'.freeze
+
       def initialize(io)
         @_io = io
       end
@@ -48,7 +51,15 @@ module Rainbows
           (@env[RACK_INPUT] = @input).rewind
           alive = @hp.keepalive?
           @env[REMOTE_ADDR] = @remote_addr
-          response = G.app.call(@env.update(RACK_DEFAULTS))
+          @env[ASYNC_CALLBACK] = @response_write ||= method(:response_write)
+
+          response = catch(:async) { G.app.call(@env.update(RACK_DEFAULTS)) }
+
+          # too tricky to support pipelining with :async since the
+          # second (pipelined) request could be a stuck behind a
+          # long-running async response
+          (response.nil? || -1 == response.first) and return @state = :close
+
           alive &&= G.alive
           out = [ alive ? CONN_ALIVE : CONN_CLOSE ] if @hp.headers?
           response_write(response, out, alive)
@@ -64,7 +75,7 @@ module Rainbows
         end while true
       end
 
-      def response_write(response, out, alive)
+      def response_write(response, out = [], alive = false)
         body = response.last
         unless body.respond_to?(:to_path)
           HttpResponse.write(self, response, out)