summary refs log tree commit homepage
path: root/lib/rainbows/never_block.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-26 23:16:41 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-26 23:33:22 +0000
commit6733af0546a759b73fd63880e8ef2549caf4c4f2 (patch)
tree80e9887968a53b25f6f7c6f645d8a43200801408 /lib/rainbows/never_block.rb
parent20e8d57127f16da8e4242582dee3b99d54cbb729 (diff)
alias_method is a mess, super is superb!
Diffstat (limited to 'lib/rainbows/never_block.rb')
-rw-r--r--lib/rainbows/never_block.rb107
1 files changed, 37 insertions, 70 deletions
diff --git a/lib/rainbows/never_block.rb b/lib/rainbows/never_block.rb
index 308398e..bcdaa3c 100644
--- a/lib/rainbows/never_block.rb
+++ b/lib/rainbows/never_block.rb
@@ -1,75 +1,42 @@
 # -*- encoding: binary -*-
 
-module Rainbows
-
-  # {NeverBlock}[www.espace.com.eg/neverblock/] library that combines
-  # the EventMachine library with Ruby Fibers.  This includes use of
-  # Thread-based Fibers under Ruby 1.8.  It currently does NOT support
-  # a streaming "rack.input" but is compatible with everything else
-  # EventMachine supports.
-  #
-  # In your Rainbows! config block, you may specify a Fiber pool size
-  # to limit your application concurrency (without using Rainbows::AppPool)
-  #
-  #   Rainbows! do
-  #     use :NeverBlock, :pool_size => 50
-  #     worker_connections 100
-  #   end
-  #
-  module NeverBlock
-
-    # :stopdoc:
-    DEFAULTS = {
-      :pool_size => 20, # same default size used by NB
-      :backend => :EventMachine, # NeverBlock doesn't support Rev yet
-    }
-
-    # same pool size NB core itself uses
-    def self.setup # :nodoc:
-      DEFAULTS.each { |k,v| O[k] ||= v }
-      Integer === O[:pool_size] && O[:pool_size] > 0 or
-        raise ArgumentError, "pool_size must a be an Integer > 0"
-      mod = Rainbows.const_get(O[:backend])
-      require "never_block" # require EM first since we need a higher version
-    end
-
-    def self.extended(klass)
-      klass.extend(Rainbows.const_get(O[:backend])) # EventMachine
-      klass.extend(Core)
-    end
-
-    module Core  # :nodoc: all
-      def self.setup
-        self.const_set(:POOL, ::NB::Pool::FiberPool.new(O[:pool_size]))
-        base = O[:backend].to_s.gsub!(/([a-z])([A-Z])/, '\1_\2').downcase!
-        require "rainbows/never_block/#{base}"
-        Rainbows::NeverBlock.const_get(:Client).class_eval do
-          self.superclass.const_set(:APP, G.server.app)
-          include Rainbows::NeverBlock::Core
-          alias _app_call app_call
-          undef_method :app_call
-          alias app_call nb_app_call
-        end
-      end
-
-      def nb_app_call
-        POOL.spawn do
-          begin
-            _app_call
-          rescue => e
-            handle_error(e)
-          end
-        end
-      end
-
-      def init_worker_process(worker)
-        super
-        Core.setup
-        logger.info "NeverBlock/#{O[:backend]} pool_size=#{O[:pool_size]}"
-      end
-    end
-
-    # :startdoc:
+# {NeverBlock}[www.espace.com.eg/neverblock/] library that combines
+# the EventMachine library with Ruby Fibers.  This includes use of
+# Thread-based Fibers under Ruby 1.8.  It currently does NOT support
+# a streaming "rack.input" but is compatible with everything else
+# EventMachine supports.
+#
+# In your Rainbows! config block, you may specify a Fiber pool size
+# to limit your application concurrency (without using Rainbows::AppPool)
+#
+#   Rainbows! do
+#     use :NeverBlock, :pool_size => 50
+#     worker_connections 100
+#   end
+#
+module Rainbows::NeverBlock
+
+  # :stopdoc:
+  DEFAULTS = {
+    :pool_size => 20, # same default size used by NB
+    :backend => :EventMachine, # NeverBlock doesn't support Rev yet
+  }
+
+  # same pool size NB core itself uses
+  def self.setup # :nodoc:
+    o = Rainbows::O
+    DEFAULTS.each { |k,v| o[k] ||= v }
+    Integer === o[:pool_size] && o[:pool_size] > 0 or
+      raise ArgumentError, "pool_size must a be an Integer > 0"
+    mod = Rainbows.const_get(o[:backend])
+    require "never_block" # require EM first since we need a higher version
+  end
 
+  def self.extended(klass)
+    klass.extend(Rainbows.const_get(Rainbows::O[:backend])) # EventMachine
+    klass.extend(Rainbows::NeverBlock::Core)
   end
+  # :startdoc:
 end
+# :enddoc:
+require 'rainbows/never_block/core'