about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-19 10:09:58 +0000
committerEric Wong <normalperson@yhbt.net>2010-07-19 17:04:28 -0700
commit5451f0c20d2382ac126e923179c096357d20cf0a (patch)
treeaa0c53e1574db980a1769053d20dd7063c909482 /lib
parent53b04c96d38bc6bb5fb3b4874fbf59aae81eb6f0 (diff)
downloadrainbows-5451f0c20d2382ac126e923179c096357d20cf0a.tar.gz
Some applications may not use Response*Pipe and TryDefer at all,
so there's no reason to pollute the runtime with extra nodes to
mark during GC.
Diffstat (limited to 'lib')
-rw-r--r--lib/rainbows/event_machine.rb80
-rw-r--r--lib/rainbows/event_machine/response_chunk_pipe.rb25
-rw-r--r--lib/rainbows/event_machine/response_pipe.rb28
-rw-r--r--lib/rainbows/event_machine/try_defer.rb27
4 files changed, 83 insertions, 77 deletions
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index d6d41a0..9cfe481 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -47,6 +47,9 @@ module Rainbows
   module EventMachine
 
     include Base
+    autoload :ResponsePipe, 'rainbows/event_machine/response_pipe'
+    autoload :ResponseChunkPipe, 'rainbows/event_machine/response_chunk_pipe'
+    autoload :TryDefer, 'rainbows/event_machine/try_defer'
 
     class Client < EM::Connection # :nodoc: all
       include Rainbows::EvCore
@@ -143,59 +146,6 @@ module Rainbows
       end
     end
 
-    module ResponsePipe # :nodoc: all
-      # garbage avoidance, EM always uses this in a single thread,
-      # so a single buffer for all clients will work safely
-      BUF = ''
-
-      def initialize(client, alive)
-        @client, @alive = client, alive
-      end
-
-      def notify_readable
-        begin
-          @client.write(@io.read_nonblock(16384, BUF))
-        rescue Errno::EINTR
-          retry
-        rescue Errno::EAGAIN
-          return
-        rescue EOFError
-          detach
-          return
-        end while true
-      end
-
-      def unbind
-        @client.quit unless @alive
-        @io.close
-      end
-    end
-
-    module ResponseChunkPipe # :nodoc: all
-      include ResponsePipe
-
-      def unbind
-        @client.write("0\r\n\r\n")
-        super
-      end
-
-      def notify_readable
-        begin
-          data = @io.read_nonblock(16384, BUF)
-          @client.write(sprintf("%x\r\n", data.size))
-          @client.write(data)
-          @client.write("\r\n")
-        rescue Errno::EINTR
-          retry
-        rescue Errno::EAGAIN
-          return
-        rescue EOFError
-          detach
-          return
-        end while true
-      end
-    end
-
     module Server # :nodoc: all
 
       def close
@@ -211,30 +161,6 @@ module Rainbows
       end
     end
 
-    # Middleware that will run the app dispatch in a separate thread.
-    # This middleware is automatically loaded by Rainbows! when using
-    # EventMachine and if the app responds to the +deferred?+ method.
-    class TryDefer < Struct.new(:app) # :nodoc: all
-
-      def initialize(app)
-        # the entire app becomes multithreaded, even the root (non-deferred)
-        # thread since any thread can share processes with others
-        Const::RACK_DEFAULTS['rack.multithread'] = true
-        super
-      end
-
-      def call(env)
-        if app.deferred?(env)
-          EM.defer(proc { catch(:async) { app.call(env) } },
-                   env[EvCore::ASYNC_CALLBACK])
-          # all of the async/deferred stuff breaks Rack::Lint :<
-          nil
-        else
-          app.call(env)
-        end
-      end
-    end
-
     def init_worker_process(worker) # :nodoc:
       Rainbows::Response.setup(Rainbows::EventMachine::Client)
       super
diff --git a/lib/rainbows/event_machine/response_chunk_pipe.rb b/lib/rainbows/event_machine/response_chunk_pipe.rb
new file mode 100644
index 0000000..ee8d260
--- /dev/null
+++ b/lib/rainbows/event_machine/response_chunk_pipe.rb
@@ -0,0 +1,25 @@
+# -*- encoding: binary -*-
+# :enddoc:
+module Rainbows::EventMachine::ResponseChunkPipe
+  include Rainbows::EventMachine::ResponsePipe
+
+  def unbind
+    @client.write("0\r\n\r\n")
+    super
+  end
+
+  def notify_readable
+    begin
+      data = @io.read_nonblock(16384, BUF)
+      @client.write(sprintf("%x\r\n", data.size))
+      @client.write(data)
+      @client.write("\r\n")
+    rescue Errno::EINTR
+    rescue Errno::EAGAIN
+      return
+    rescue EOFError
+      detach
+      return
+    end while true
+  end
+end
diff --git a/lib/rainbows/event_machine/response_pipe.rb b/lib/rainbows/event_machine/response_pipe.rb
new file mode 100644
index 0000000..88d6e5a
--- /dev/null
+++ b/lib/rainbows/event_machine/response_pipe.rb
@@ -0,0 +1,28 @@
+# -*- encoding: binary -*-
+# :enddoc:
+module Rainbows::EventMachine::ResponsePipe
+  # garbage avoidance, EM always uses this in a single thread,
+  # so a single buffer for all clients will work safely
+  BUF = ''
+
+  def initialize(client, alive)
+    @client, @alive = client, alive
+  end
+
+  def notify_readable
+    begin
+      @client.write(@io.read_nonblock(16384, BUF))
+    rescue Errno::EINTR
+    rescue Errno::EAGAIN
+      return
+    rescue EOFError
+      detach
+      return
+    end while true
+  end
+
+  def unbind
+    @client.quit unless @alive
+    @io.close
+  end
+end
diff --git a/lib/rainbows/event_machine/try_defer.rb b/lib/rainbows/event_machine/try_defer.rb
new file mode 100644
index 0000000..97e384d
--- /dev/null
+++ b/lib/rainbows/event_machine/try_defer.rb
@@ -0,0 +1,27 @@
+# -*- encoding: binary -*-
+# :enddoc:
+
+# Middleware that will run the app dispatch in a separate thread.
+# This middleware is automatically loaded by Rainbows! when using
+# EventMachine and if the app responds to the +deferred?+ method.
+class Rainbows::EventMachine::TryDefer < Struct.new(:app)
+  # shortcuts
+  ASYNC_CALLBACK = Rainbows::EvCore::ASYNC_CALLBACK
+
+  def initialize(app)
+    # the entire app becomes multithreaded, even the root (non-deferred)
+    # thread since any thread can share processes with others
+    Rainbows::Const::RACK_DEFAULTS['rack.multithread'] = true
+    super
+  end
+
+  def call(env)
+    if app.deferred?(env)
+      EM.defer(proc { catch(:async) { app.call(env) } }, env[ASYNC_CALLBACK])
+      # all of the async/deferred stuff breaks Rack::Lint :<
+      nil
+    else
+      app.call(env)
+    end
+  end
+end