summary refs log tree commit homepage
path: root/lib/rainbows/event_machine.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-04 22:16:52 +0000
committerEric Wong <normalperson@yhbt.net>2010-07-04 22:34:09 +0000
commit39b178cdebe275cbc8ce19cf269bea7cd15ff4ca (patch)
treeb7628ed278895fcf70ea3206956be586ac9e1ac5 /lib/rainbows/event_machine.rb
parent75f5aa9a0d6b37a94afbea3121fc2c16e70a2b1d (diff)
This hopefully allows the "sendfile" gem to be required
anywhere in the Rainbows!/Unicorn config file, and not
have to be required via RUBYOPT or the '-r' command-line
switch.

We also modularize HttpResponse and avoids singleton methods
in the response path.  This (hopefully) makes it easier for
individual concurrency models to share code and override
individual methods.
Diffstat (limited to 'lib/rainbows/event_machine.rb')
-rw-r--r--lib/rainbows/event_machine.rb21
1 files changed, 13 insertions, 8 deletions
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index 6ba536b..0ad604e 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -50,6 +50,7 @@ module Rainbows
 
     class Client < EM::Connection
       include Rainbows::EvCore
+      include Rainbows::HttpResponse
       G = Rainbows::G
 
       def initialize(io)
@@ -103,23 +104,23 @@ module Rainbows
         if body.respond_to?(:errback) && body.respond_to?(:callback)
           body.callback { quit }
           body.errback { quit }
-          HttpResponse.write(self, response, out)
+          write_header(self, response, out)
+          write_body_each(self, body)
           return
         elsif ! body.respond_to?(:to_path)
-          HttpResponse.write(self, response, out)
+          write_response(self, response, out)
           quit unless alive
           return
         end
 
         headers = Rack::Utils::HeaderHash.new(response[1])
-        io = Rainbows.body_to_io(body)
+        io = body_to_io(body)
         st = io.stat
 
         if st.file?
           headers.delete('Transfer-Encoding')
           headers['Content-Length'] ||= st.size.to_s
-          response = [ response[0], headers, [] ]
-          HttpResponse.write(self, response, out)
+          write_header(self, [ response[0], headers ], out)
           stream = stream_file_data(body.to_path)
           stream.callback { quit } unless alive
         elsif st.socket? || st.pipe?
@@ -130,15 +131,14 @@ module Rainbows
           else
             out[0] = CONN_CLOSE
           end
-          response = [ response[0], headers, [] ]
-          HttpResponse.write(self, response, out)
+          write_header(self, [ response[0], headers ], out)
           if do_chunk
             EM.watch(io, ResponseChunkPipe, self).notify_readable = true
           else
             EM.enable_proxy(EM.attach(io, ResponsePipe, self), self, 16384)
           end
         else
-          HttpResponse.write(self, response, out)
+          write_response(self, response, out)
         end
       end
 
@@ -226,6 +226,11 @@ module Rainbows
       end
     end
 
+    def init_worker_process(worker)
+      Rainbows::HttpResponse.setup(Rainbows::EventMachine::Client)
+      super
+    end
+
     # runs inside each forked worker, this sits around and waits
     # for connections and doesn't die until the parent dies (or is
     # given a INT, QUIT, or TERM signal)