about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/rainbows.rb6
-rw-r--r--lib/rainbows/actor_spawn.rb17
-rw-r--r--lib/rainbows/event_machine.rb9
-rw-r--r--lib/rainbows/fiber_pool.rb8
-rw-r--r--lib/rainbows/fiber_spawn.rb6
-rw-r--r--lib/rainbows/rev/core.rb5
-rw-r--r--lib/rainbows/thread_pool.rb7
-rw-r--r--lib/rainbows/thread_spawn.rb7
8 files changed, 25 insertions, 40 deletions
diff --git a/lib/rainbows.rb b/lib/rainbows.rb
index 8a41586..a252ba6 100644
--- a/lib/rainbows.rb
+++ b/lib/rainbows.rb
@@ -86,6 +86,12 @@ module Rainbows
   end
   autoload :Fiber, 'rainbows/fiber' # core class
 
+  # returns nil if accept fails
+  def self.accept(sock)
+    sock.accept_nonblock
+  rescue Errno::EAGAIN, Errno::ECONNABORTED
+  end
+
 end
 
 # inject the Rainbows! method into Unicorn::Configurator
diff --git a/lib/rainbows/actor_spawn.rb b/lib/rainbows/actor_spawn.rb
index 30e62a9..7603734 100644
--- a/lib/rainbows/actor_spawn.rb
+++ b/lib/rainbows/actor_spawn.rb
@@ -25,17 +25,14 @@ module Rainbows
 
       begin
         ret = IO.select(LISTENERS, nil, nil, 1) and ret.first.each do |l|
-          next if lock.synchronize { nr >= limit }
-          begin
-            Actor.spawn(l.accept_nonblock) do |c|
-              lock.synchronize { nr += 1 }
-              begin
-                process_client(c)
-              ensure
-                lock.synchronize { nr -= 1 }
-              end
+          lock.synchronize { nr >= limit } and break sleep(0.01)
+          c = Rainbows.accept(l) and Actor.spawn do
+            lock.synchronize { nr += 1 }
+            begin
+              process_client(c)
+            ensure
+              lock.synchronize { nr -= 1 }
             end
-          rescue Errno::EAGAIN, Errno::ECONNABORTED
           end
         end
       rescue => e
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index b099721..e28b232 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -174,12 +174,9 @@ module Rainbows
 
       def notify_readable
         return if CUR.size >= MAX
-        begin
-          io = @io.accept_nonblock
-          sig = EM.attach_fd(io.fileno, false)
-          CUR[sig] = Client.new(sig, io)
-        rescue Errno::EAGAIN, Errno::ECONNABORTED
-        end
+        io = Rainbows.accept(@io) or return
+        sig = EM.attach_fd(io.fileno, false)
+        CUR[sig] = Client.new(sig, io)
       end
     end
 
diff --git a/lib/rainbows/fiber_pool.rb b/lib/rainbows/fiber_pool.rb
index 25f086e..2a1c5f7 100644
--- a/lib/rainbows/fiber_pool.rb
+++ b/lib/rainbows/fiber_pool.rb
@@ -29,13 +29,11 @@ module Rainbows
       begin
         schedule do |l|
           fib = pool.shift or break # let another worker process take it
-          io = begin
-            l.accept_nonblock
-          rescue Errno::EAGAIN, Errno::ECONNABORTED
+          if io = Rainbows.accept(l)
+            fib.resume(Fiber::IO.new(io, fib))
+          else
             pool << fib
-            next
           end
-          fib.resume(Fiber::IO.new(io, fib))
         end
       rescue => e
         Error.listen_loop(e)
diff --git a/lib/rainbows/fiber_spawn.rb b/lib/rainbows/fiber_spawn.rb
index da3db01..6104a7b 100644
--- a/lib/rainbows/fiber_spawn.rb
+++ b/lib/rainbows/fiber_spawn.rb
@@ -22,11 +22,7 @@ module Rainbows
       begin
         schedule do |l|
           break if G.cur >= limit
-          io = begin
-            l.accept_nonblock
-          rescue Errno::EAGAIN, Errno::ECONNABORTED
-            next
-          end
+          io = Rainbows.accept(l) or next
           ::Fiber.new { process_client(fio.new(io, ::Fiber.current)) }.resume
         end
       rescue => e
diff --git a/lib/rainbows/rev/core.rb b/lib/rainbows/rev/core.rb
index 785990c..b78fe85 100644
--- a/lib/rainbows/rev/core.rb
+++ b/lib/rainbows/rev/core.rb
@@ -14,10 +14,7 @@ module Rainbows
 
       def on_readable
         return if CONN.size >= MAX
-        begin
-          CL.new(@_io.accept_nonblock).attach(LOOP)
-        rescue Errno::EAGAIN, Errno::ECONNABORTED
-        end
+        io = Rainbows.accept(@_io) and CL.new(io).attach(LOOP)
       end
     end # class Server
 
diff --git a/lib/rainbows/thread_pool.rb b/lib/rainbows/thread_pool.rb
index 949db6f..f398828 100644
--- a/lib/rainbows/thread_pool.rb
+++ b/lib/rainbows/thread_pool.rb
@@ -49,11 +49,8 @@ module Rainbows
             # problem.  On the other hand, a thundering herd may not
             # even incur as much overhead as an extra Mutex#synchronize
             ret = IO.select(LISTENERS, nil, nil, 1) and
-                  ret.first.each do |sock|
-                    begin
-                      process_client(sock.accept_nonblock)
-                    rescue Errno::EAGAIN, Errno::ECONNABORTED
-                    end
+                  ret.first.each do |s|
+                    s = Rainbows.accept(s) and process_client(s)
                   end
           rescue Errno::EINTR
           rescue Errno::EBADF, TypeError
diff --git a/lib/rainbows/thread_spawn.rb b/lib/rainbows/thread_spawn.rb
index 40b37aa..5afb91e 100644
--- a/lib/rainbows/thread_spawn.rb
+++ b/lib/rainbows/thread_spawn.rb
@@ -37,11 +37,8 @@ module Rainbows
               sleep(0.1) # hope another process took it
               break # back to IO.select
             end
-            c = begin
-              l.accept_nonblock
-            rescue Errno::EAGAIN, Errno::ECONNABORTED
-            end or next
-            threads.add(Thread.new { process_client(c) })
+            c = Rainbows.accept(l) and
+              threads.add(Thread.new { process_client(c) })
           end
       rescue Errno::EINTR
         retry