about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-10-22 02:51:18 +0000
committerEric Wong <normalperson@yhbt.net>2010-10-22 18:37:45 +0000
commit6d46978bdc8d2ee4263431ecdcada53389b12597 (patch)
tree29e1fb6ce60d84c3b3cf587e3faf4412b9a59a3c
parent03806d2b44c2d3cee75258ee9e83d671e751baeb (diff)
downloadrainbows-6d46978bdc8d2ee4263431ecdcada53389b12597.tar.gz
Reduces confusion for constant resolution/scoping rules
and lowers LoC.
-rw-r--r--lib/rainbows/fiber_pool.rb67
-rw-r--r--lib/rainbows/fiber_spawn.rb47
2 files changed, 52 insertions, 62 deletions
diff --git a/lib/rainbows/fiber_pool.rb b/lib/rainbows/fiber_pool.rb
index 18f43de..e178154 100644
--- a/lib/rainbows/fiber_pool.rb
+++ b/lib/rainbows/fiber_pool.rb
@@ -1,44 +1,39 @@
 # -*- encoding: binary -*-
 require 'rainbows/fiber'
 
-module Rainbows
+# A Fiber-based concurrency model for Ruby 1.9.  This uses a pool of
+# Fibers to handle client IO to run the application and the root Fiber
+# for scheduling and connection acceptance.  The pool size is equal to
+# the number of +worker_connections+.  Compared to the ThreadPool
+# model, Fibers are very cheap in terms of memory usage so you can
+# have more active connections.  This model supports a streaming
+# "rack.input" with lightweight concurrency.  Applications are
+# strongly advised to wrap all slow IO objects (sockets, pipes) using
+# the Rainbows::Fiber::IO class whenever possible.
+module Rainbows::FiberPool
+  include Rainbows::Fiber::Base
 
-  # A Fiber-based concurrency model for Ruby 1.9.  This uses a pool of
-  # Fibers to handle client IO to run the application and the root Fiber
-  # for scheduling and connection acceptance.  The pool size is equal to
-  # the number of +worker_connections+.  Compared to the ThreadPool
-  # model, Fibers are very cheap in terms of memory usage so you can
-  # have more active connections.  This model supports a streaming
-  # "rack.input" with lightweight concurrency.  Applications are
-  # strongly advised to wrap all slow IO objects (sockets, pipes) using
-  # the Rainbows::Fiber::IO class whenever possible.
+  def worker_loop(worker) # :nodoc:
+    init_worker_process(worker)
+    pool = []
+    worker_connections.times {
+      Fiber.new {
+        process(Fiber.yield) while pool << Fiber.current
+      }.resume # resume to hit ::Fiber.yield so it waits on a client
+    }
+    Rainbows::Fiber::Base.setup(self.class, app)
 
-  module FiberPool
-    include Fiber::Base
-
-    def worker_loop(worker) # :nodoc:
-      init_worker_process(worker)
-      pool = []
-      worker_connections.times {
-        ::Fiber.new {
-          process(::Fiber.yield) while pool << ::Fiber.current
-        }.resume # resume to hit ::Fiber.yield so it waits on a client
-      }
-      Fiber::Base.setup(self.class, app)
-
-      begin
-        schedule do |l|
-          fib = pool.shift or break # let another worker process take it
-          if io = l.kgio_tryaccept
-            fib.resume(io)
-          else
-            pool << fib
-          end
+    begin
+      schedule do |l|
+        fib = pool.shift or break # let another worker process take it
+        if io = l.kgio_tryaccept
+          fib.resume(io)
+        else
+          pool << fib
         end
-      rescue => e
-        Error.listen_loop(e)
-      end while G.alive || G.cur > 0
-    end
-
+      end
+    rescue => e
+      Rainbows::Error.listen_loop(e)
+    end while G.alive || G.cur > 0
   end
 end
diff --git a/lib/rainbows/fiber_spawn.rb b/lib/rainbows/fiber_spawn.rb
index 1a0da04..17bd884 100644
--- a/lib/rainbows/fiber_spawn.rb
+++ b/lib/rainbows/fiber_spawn.rb
@@ -1,33 +1,28 @@
 # -*- encoding: binary -*-
 require 'rainbows/fiber'
 
-module Rainbows
+# Simple Fiber-based concurrency model for 1.9.  This spawns a new
+# Fiber for every incoming client connection and the root Fiber for
+# scheduling and connection acceptance.  This exports a streaming
+# "rack.input" with lightweight concurrency.  Applications are
+# strongly advised to wrap all slow IO objects (sockets, pipes) using
+# the Rainbows::Fiber::IO class whenever possible.
+module Rainbows::FiberSpawn
+  include Rainbows::Fiber::Base
 
-  # Simple Fiber-based concurrency model for 1.9.  This spawns a new
-  # Fiber for every incoming client connection and the root Fiber for
-  # scheduling and connection acceptance.  This exports a streaming
-  # "rack.input" with lightweight concurrency.  Applications are
-  # strongly advised to wrap all slow IO objects (sockets, pipes) using
-  # the Rainbows::Fiber::IO class whenever possible.
-
-  module FiberSpawn
-    include Fiber::Base
-
-    def worker_loop(worker) # :nodoc:
-      init_worker_process(worker)
-      Fiber::Base.setup(self.class, app)
-      limit = worker_connections
-
-      begin
-        schedule do |l|
-          break if G.cur >= limit
-          io = l.kgio_tryaccept or next
-          ::Fiber.new { process(io) }.resume
-        end
-      rescue => e
-        Error.listen_loop(e)
-      end while G.alive || G.cur > 0
-    end
+  def worker_loop(worker) # :nodoc:
+    init_worker_process(worker)
+    Rainbows::Fiber::Base.setup(self.class, app)
+    limit = worker_connections
 
+    begin
+      schedule do |l|
+        break if G.cur >= limit
+        io = l.kgio_tryaccept or next
+        Fiber.new { process(io) }.resume
+      end
+    rescue => e
+      Rainbows::Error.listen_loop(e)
+    end while G.alive || G.cur > 0
   end
 end